STM32F4 USART configuration

Publisher:CoboroLatest update time:2017-02-06 Source: eefocusKeywords:STM32F4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I won't go into details about the serial port. Although the serial port is an old interface with a slow speed and has long been eliminated in computers, it is still widely used in embedded systems due to its simple structure and ease of use, and perhaps also because it has a basic support for use in computers (the microcontroller is following the path that computers once took). 
In the STM32F429, there are a total of 8 serial ports, 4 USARTs and 4 UARTs.

The goal to be achieved today is to let STM32 send data to the computer through the serial port, so that it can really output "Hello World!"! We
still use the previous project, but the project structure is slightly changed, as shown below:

prj

tree

 

Unlike before, I now choose to copy the entire firmware library to the STM32F4xx_StdPeriph folder, and add the C file corresponding to a peripheral when using it.

This communication is implemented using the query method. When the baud rate is 115200bps. Although for the 168M CPU main frequency, using the query method to send data is too wasteful, but because the CPU has no other tasks, and I am eager to see the results, compared to configuring interrupts and writing a FIFO, I think using the query method is easier for me to accept:)

Initialization of USART1.
Like GPIO, USART also provides an initialization structure. Fill in the corresponding data and pass it as a parameter to USART_Init().

In addition, there are two points to note. One is to enable the USART clock (this is the same for all peripherals), and the other is to do relevant configuration because of pin multiplexing.

The STM32F4 series serial port pin multiplexing is different from the STM32F1 series:
the STM32F1 series only needs to set the pin mode to GPIO AF mode, while the STM32F4 series needs to call the GPIO_PinAFConfig() function to configure the corresponding multiplexed pins.

/**

 * @brief UART1 initialization, 115200, 8, N, 1

 */

void USART1_Init(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    USART_InitTypeDef USART_InitStructure;

 

    // Enable the corresponding clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

 

    // Pin multiplexing function

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

 

    // Initialize the pins used by the serial port

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

 

    GPIO_Init(GPIOA, &GPIO_InitStructure);

 

    // USART1 initialization

    USART_InitStructure.USART_BaudRate = 115200;

    USART_InitStructure.USART_WordLength = USART_WordLength_8b;

    USART_InitStructure.USART_StopBits = USART_StopBits_1;

    USART_InitStructure.USART_Parity = USART_Parity_No;

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 

    USART_Init(USART1, &USART_InitStructure);

    USART_Cmd(USART1, ENABLE);

 

    return;

}

 

 

/**

 * @brief Send a string from the serial port, using the query method

 *

 * @param str The string to be sent

 */

void USART1_Puts(uint8_t *str)

{

    while (*str != '\0')

    {

        USART_SendData(USART1, *str);

        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) != SET);

        str++;

    }

 

    return;

}

 


main function:


int main(void)

{

    RCC_Config();

    SysTick_Init();

    LED_GPIO_Config();

    USART1_Init();

 

    Delay_ms(1);

 

    while (1)

    {

        USART1_Puts("Hello World!");

        GPIO_ResetBits(GPIOG, GPIO_Pin_13);

        GPIO_SetBits(GPIOG, GPIO_Pin_14);

        Delay_ms(500);

        USART1_Puts("http://www.DevLabs.cn\n");

        GPIO_ResetBits(GPIOG, GPIO_Pin_14);

        GPIO_SetBits(GPIOG, GPIO_Pin_13);

        Delay_ms(500);

    }

}

Another point is that the STM32F429 Discovery cannot communicate directly with the computer. I use a USB-to-serial port CP2102 module. Connect the RX of the serial port module to PA9 of the STM32F429 and the TX to PA10. Then the computer can receive data correctly, as shown in the figure.

rcv

USED

There is a small problem. I found that the first character sent after reset will be swallowed, but it is normal when debugging. The reason is unknown and will be solved later.


Keywords:STM32F4 Reference address:STM32F4 USART configuration

Previous article:STM32 serial communication – using interrupt mode
Next article:Make LCD flash: Setting and using STM32F4SysTick

Recommended ReadingLatest update time:2024-11-16 16:48

Summary of several memory types of STM32F407 development board, SRAM, FLASH, EEPROM
Common memory concepts: RAM, SRAM, SDRAM, ROM, EPROM, EEPROM, Flash memory can be divided into many categories. According to whether the data is lost when power is off, it can be divided into RAM (random access memory) and ROM (read-only memory). RAM has a faster access speed, but the data will be lost after power off
[Microcontroller]
STM32F429 LTDC code template
A good memory is not as good as a bad pen. In order to facilitate quick code reference and improve development efficiency, the initialization code of the LTDC driver is posted here. This code is based on the official ST STM32F429 Discovery Demo board. When learning the LTDC driver, I referred to the official routine o
[Microcontroller]
STM32F429--Detailed explanation of LTDC registers
    1. LTDC Synchronization Size Configuration Register (LTDC_SSCR)  HSPW-1  VSPW-1  defines the number of horizontal synchronization pixels (HSPW) minus 1 and the number of vertical synchronization lines (VSPW) minus 1  bit 31:28 Reserved  bit 27:16 HSW : Horizontal Synchronization Width (in pixel clock cycles), thes
[Microcontroller]
STM32F4——NVIC interrupt priority and external interrupt
NVIC interrupt priority 1. Introduction:     The CM4 core can support 256 interrupts, including 16 core interrupts and 240 external interrupts, and 256 levels of programmable interrupt settings. For the STM32F4, not all of the CM4 core is used, only part of it is used. For the STM32F40 and 41 series, there are 92 in
[Microcontroller]
STM32F4——NVIC interrupt priority and external interrupt
stm32f405 HAL library serial port receiving error
Error Analysis The HAL library used has packet loss issues. 定义void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) {         if(huart == &huart1)            {         log_u("********************* huart1 err ********************************* \r\n");         switch(huart- ErrorCode)         {             case HAL_UART
[Microcontroller]
STM32f407 system timer clock configuration and calculation
STM32f407 system timer clock configuration and calculation 1. Selection of external crystal Determine the external crystal oscillator of your microcontroller The system default configuration is 25M, that is, the official library file parameter HSE Frequency (Hz) is generally 25000000  HSE Frequency(Hz) | 25000
[Microcontroller]
STM32f407 system timer clock configuration and calculation
STM32F407 study notes - GPIO_button control LED on and off
#include stm32f4xx.h #include "stm32f4xx_conf.h" #include "delay.h"   GPIO_TypeDef* io_led = GPIOC; //Define an io_led pointing to the structure GPIO_TypeDef   const u16 pin_led = GPIO_Pin_1; //pin GPIO_TypeDef* io_key=GPIOC; const u16 pin_key=GPIO_Pin_0;   void Led_Init() { GPIO_InitTypeDef GPIO_init_l; //Structur
[Microcontroller]
An analysis of anomalies when using the STM32F4 chip CCM RAM
Preface A customer used the STM32F427 chip , and the program placed the CSTACK in the CCM RAM. As a result, the boards tested for a period of time did not run normally. This phenomenon made us wonder if the CCM RAM was damaged during the test, which caused us to waste a lot of time on the road to solving the problem.
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号