STM32 USART usage notes

Publisher:代码律动Latest update time:2017-09-08 Source: eefocusKeywords:STM32  USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Finally, I can't help but complain about the students who translated the STM 32 function library manual. A lot of things were omitted in the middle, and they were very important things. . . . . My heart almost collapsed!!!

Now let's talk about the pitfalls of USART serial communication in STM 32 function library 3.5:

In some tutorials and Chinese manuals, the basic configuration source code of USART is as follows


  1. USART_InitTypeDef USART_InitStructure;  

  2. USART_InitStructure.USART_BaudRate = 9600;  

  3. USART_InitStructure.USART_WordLength = USART_WordLength_8b;  

  4. USART_InitStructure.USART_StopBits = USART_StopBits_1;  

  5. USART_InitStructure.USART_Parity = USART_Parity_Odd;  

  6. USART_InitStructure.USART_HardwareFlowControl =  

  7. USART_HardwareFlowControl_RTS_CTS;  

  8. USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;  

  9. USART_InitStructure.USART_Clock = USART_Clock_Disable;  

  10. USART_InitStructure.USART_CPOL = USART_CPOL_High;  

  11. USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;  

  12. USART_InitStructure.USART_LastBit = USART_LastBit_Enable;  

  13. USART_Init(USART1, &USART_InitStructure);  

Only one structure variable is declared above: USART_InitTypeDef USART_InitStructure;


Then we compile and find that the compilation error is reported, and the following clock variables are not defined



  1. USART_InitStructure.USART_Clock = USART_Clock_Disable;  

  2. USART_InitStructure.USART_CPOL = USART_CPOL_High;  

  3. USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;  

  4. USART_InitStructure.USART_LastBit = USART_LastBit_Enable;  

Why is this? I clearly wrote it according to what the manual says 55555. . . .

As a last resort, I had to use our great Baidu to find out. Through some information and posts from netizens, I finally found the reason. It turned out that in version 3.5 of the function library, when initializing the USART default value, there is not only one structure variable USART_InitStructure, but also a structure variable for USART clock management:

USART_ClockInitTypeDef USART_ClockInitStructure;

So the final USART configuration function is:


  1.        USART_InitTypeDef USART_InitStructure;     

  2. USART_ClockInitTypeDef USART_ClockInitStructure;  

  3. /*Baud rate 9600 8-bit data length 1 stop bit no parity bit 

  4.   Disable hardware flow control Disable USART clock Clock polarity low Capture data on second edge 

  5.   The clock pulse of the last bit of clock data is not output from SCLK*/  

  6. USART_InitStructure.USART_BaudRate =9600;  

  7. USART_InitStructure.USART_WordLength = USART_WordLength_8b;  

  8. USART_InitStructure.USART_StopBits = USART_StopBits_1;  

  9. USART_InitStructure.USART_Parity = USART_Parity_No;  

  10. USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;  

  11. USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;  

  12. USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;  

  13. USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;  

  14. USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;  

  15. USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;  

  16. USART_Init(USART1, &USART_InitStructure);  

  17. USART_ClockInit(USART1 , &USART_ClockInitStructure);  


  1. Of course, don't forget to enable USART: USART_Cmd(USART1, ENABLE); and turn on the clock of the corresponding USART.  


  1. Compile again and you will find that the compilation is correct, but this does not mean that you can use USART to send a "Hello World" to your computer. Continue down. Since almost all IO ports of STM32 are multiplexed, that is to say, an IO port is not only a regular input and output, it can also be used as an interface for on-chip peripherals such as USART/IIC/ADC. So when you use USART, you are actually sending and receiving data through the IO port. At this time, we also need to configure the corresponding IO port. Take USART1 as an example. Generally, it is multiplexed with ports 9 and 10 in GPIOA. If you are not sure, you can check the official manual of STM32, where GPIOA.9 is the data transmission port, i.e. TXD, and GPIOA.10 is the data receiving port RXD. Then we can configure it. The specific source code is as follows:  


  1.                GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;             

  2.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Note!! There is a very important point here, because USART is multiplexing port GPIOA.9, it is not a normal OUT but multiplexing AF  

  3.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

  4.     GPIO_Init(GPIOA, &GPIO_InitStructure);  

  5.   

  6.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;             

  7.     GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IN_FLOATING;    

  8.     GPIO_Init(GPIOA, &GPIO_InitStructure);  



Well, now we have completed all the configurations of USART. Can't wait to send a message to PC through the development board? By using two functions
USART_ SendData and USART_ ReceiveData in the main function, we can realize the two-way communication between the development board and PC~~


Keywords:STM32  USART Reference address:STM32 USART usage notes

Previous article:How to use and program the STM32 serial port USART1
Next article:Using DMA+USART3 to transfer data on STM32

Recommended ReadingLatest update time:2024-11-16 14:32

STM32 USB design microcontroller program
First, let's take a look at the working process of USB. When a USB device is connected to the host, the host begins to enumerate the USB device and sends a command to the USB device to obtain the relevant description information of the USB device, including device description (device descriptor), configuration
[Industrial Control]
Typical USART transceiver interface program generated by CVAVR
The UART transceiver program provided in general textbooks is often a simple code that uses polling to complete the transceiver. However, for high-speed AVR, this method greatly reduces the efficiency of MUC. When using AVR, you should write an efficient and reliable UART transceiver interface (low-level) program base
[Microcontroller]
STM32 external interrupt (EXTI) analysis and application
If you have learned the external interrupt of 51 microcontroller, you will get started quickly;  this blog post is based on STM32F103ZET6 chip, which is compatible with most STM32F10x chips;  the code is based on the 3.5.0 standard library provided by ST official website.  If there are any deficiencies, I hope seniors
[Microcontroller]
Design of intrinsically safe power supply for mines based on STM32
Intrinsically safe power supply is an important part of intrinsically safe electrical equipment. Due to the mechanization of coal mines, the power supply, communication and alarm of underground sensors, bend alarms and other equipment all require power supply equipment. However, based on the
[Power Management]
Design of intrinsically safe power supply for mines based on STM32
Distinguish between long press and short press of keys triggered by stm32 software
/* ------------------------------------------------------------------------------------------------  * @fn KeyCoolPressTimeHandle  *  * @brief key detection  *  * @param       none  *  * @return      none  **************************************************************************************************  */ uint8_t Ke
[Microcontroller]
Study notes on stm32 serial port and 485 communication
stm32 serial port interrupt: USART_IT_PE (Parity Interrupt) USART_IT_TXE (Transmit Interrupt) USART_IT_TC (Transfer Complete Interrupt) USART_IT_RXNE (Receive Interrupt) USART_IT_IDLE (Idle Bus Interrupt) USART_IT_LBD (LIN Break Detect Interrupt) USART_IT_CTS (CTS interrupt) USART_IT_ERR (Error Interrupt) Interrupts
[Microcontroller]
Description of Cortex-M3 registers used in STM32
Three groups of registers defined by Cortex-M3 are used in STM32. The description of these three groups of registers is not in the technical manual of STM32. You need to refer to the Cortex-M3 Technical Reference Manual (r2p0) released by ARM.   Three structures are defined in the STM32 firmware library to correspon
[Microcontroller]
STM32 serial port send interrupt
SECTION 2   Let's talk about TC first. It stands for Transmission Complete. The interrupt is triggered after a byte is sent, which is called "interrupt after sending". It is the same as the original 8051 TI method, which is to interrupt after sending. You need to send a byte in the sending function to trigger t
[Microcontroller]
STM32 serial port send interrupt
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号