STM32 library function USART_SendData problem and solution

Publisher:delta14Latest update time:2016-04-07 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Problems and Phenomena

There is no problem in using USART_SendData() function to send single characters non-continuously; when sending characters continuously (no delay between two characters), the sending buffer will overflow. If the amount of data sent is very small, the serial port will only send the last character. When the amount of data sent is large, the sent data will be lost inexplicably.

like:

1
2
for(TxCounter = 0;TxCounter < RxCounter; TxCounter++)
USART_SendData(USART1, RxBuffer[TxCounter]);

 

2. Reasons

This API function is imperfect. There is no statement in the function body to determine whether a character has been sent. Instead, the data is directly placed in the send buffer. When sending data continuously, due to the speed limit of the send shift register (related to the communication baud rate), the data in the send buffer overflows. The old data has not been sent out in time, and the new data overwrites the old data in the send buffer.

 

 

3. Solution

I won't talk about the method of waiting for a period of time after sending. The waiting time is uncertain, which is the worst option. The following two solutions are provided:

Solution 1. Check the status bit after each character is sent

USART_SendData(USART1, RxBuffer[TxCounter]);

while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET){} //Wait for the send buffer to be empty before sending the next character

 

Solution 2. Modify the library function

Modify the USART_SendData() function and add the USART_FLAG_TXE status detection statement of the send buffer to ensure that one character is completely sent before sending the next character.

Implementation method: Check the status register every time a character is sent to ensure that the data has been sent. The specific steps are as follows.

Function definition body before modification

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void USART_SendData(USART_TypeDef* USARTx, u16 Data)
 
{
 
 
 
assert_param(IS_USART_ALL_PERIPH(USARTx));
 
assert_param(IS_USART_DATA(Data));
 
 
 
USARTx->DR = (Data & (u16)0x01FF);
 
}
 

Modified function definition body

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void USART_SendData(USART_TypeDef* USARTx, u16 Data)
 
{
 
 
 
assert_param(IS_USART_ALL_PERIPH(USARTx));
 
assert_param(IS_USART_DATA(Data));
 
 
 
USARTx->DR = (Data & (u16)0x01FF);
 
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET){} //Wait for the send buffer to be empty before sending the next character
}

Some people may think, why not handle this problem in advance in the library function, and throw the solution to the user. Personally, I think the reason why ST does this is: use the send interrupt function.

 

STM32 library function USART_SendData problem and solution | Xiao Xie's blog http://blog.xieyc.com/stm32-lib-function-usart-send-data-problem-and-solution/

Keywords:STM32 Reference address:STM32 library function USART_SendData problem and solution

Previous article:STM32 hardware multiplier - another difference from MSP430 hardware multiplier
Next article:8 working modes of GPIO in STM32!

Recommended ReadingLatest update time:2024-11-15 10:49

Eclipse builds stm32 development environment + jlink debugging
1. New construction projects:     Click File- New- C++ project   Then we will find the interface below, where there are ready-made options for projects such as STM32, and on the right there are compiler chains supported by the project, such as Cross Arm gcc. Based on this, we can determine that the previous software
[Microcontroller]
Eclipse builds stm32 development environment + jlink debugging
STM32 serial communication USART (II) --- DMA mode
When I first came into contact with DMA, I was completely confused. I only knew that it was convenient and fast, but I didn’t know how to use it. Later, after I figured out the principles, I felt a lot more relaxed. However, I still don’t understand it thoroughly, so I hope to write down my understanding and share it
[Microcontroller]
STM32 Learning Notes ⑤-SysTick Precise Delay
/****************************************************** ********** Routine name: SysTick experiment Hardware connection: indicator light connected to PE0 Function description: PEO flips once per second */ #include "stm32f10x_lib.h" extern vu32 TimingDelay; //This file references the precise timing variable defi
[Microcontroller]
Two STM32 chips use HAL to complete SPI full-duplex master-slave communication
SPI is a very simple and easy-to-use full-duplex master-slave communication protocol. This article uses two STM32F429 chips, one as the host and the other as the slave to complete the SPI full-duplex communication test. The HAL library of STM32 simplifies the sending and receiving of the SPI host side, but the HAL
[Microcontroller]
Two STM32 chips use HAL to complete SPI full-duplex master-slave communication
STM32 drives LCD12864 display
When we make an electronic product, we often need to realize the function of human-computer interaction. In addition to outputting to the host computer for display through the computer, the display is also a very good way for human-computer interaction, which can be used in some occasions where computers cannot be use
[Microcontroller]
STM32 Learning 009_ Definition and declaration of global variables
Today, when I was writing the interrupt function SysTcik_Handler(), I always got an error. The global variable extern u16 ntime was defined at the beginning (ntime is written in the systick interrupt function of stm32f10x_it.c), but it always got an error when compiling. I found a solution after searching on Baidu.
[Microcontroller]
STM32 Learning 009_ Definition and declaration of global variables
Optical fingerprint recognition system module circuit design based on STM32
  0 Preface   With the increasing application of electronic information technology, many occasions require the identification or identity record of specific user groups, such as access control systems, attendance systems, security authentication systems, etc. Various technologies are used in various systems, such as
[Microcontroller]
Optical fingerprint recognition system module circuit design based on STM32
Engineer's STM32 MCU Learning Basic Notes (4): Using PWM to implement firefly lights (I)
  Using PWM to realize firefly lamp   Last time I mentioned that I would use the PWM function of the Timer to implement a firefly light. Of course, I still need to find an existing example to modify, and the example I will use this time is here.      Copy a copy to your own practice folder and create a proj
[Analog Electronics]
Engineer's STM32 MCU Learning Basic Notes (4): Using PWM to implement firefly lights (I)
Latest Microcontroller Articles
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号