Reasons and solutions for the loss of the first character of a string sent by USART

Publisher:Aq123456258Latest update time:2016-10-10 Source: eefocusKeywords:USART Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
When writing the sending code with reference to the official USART example, the first character of the string is inexplicably lost
/* eg write a character to the USART */
  USART_SendData(EVAL_COM1, (uint8_t) ch);
 
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {}
After looking through the data sheet, I found that the TXE and TC flags are set to 1 by default after reset, which causes the first flag detection to fail and the first character is overwritten before it is sent.
[STM32] Causes and solutions for the loss of the first character of a string sent by USART - rainsmell - rainsmells blog
 
Once the cause is found, the solution is quite simple. Just clear the flag bit before sending the first character.
 
[STM32] Causes and solutions for the loss of the first character of a string sent by USART - rainsmell - rainsmells blog
Two methods:
1. Change the TC detection to the TXE inspection
while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET)
2. Before writing data to the USART_DR register, read the USART_SR register once
/* Read USART_SR register */ USARTx -> SR; /* eg write a character to the USART */ USART_SendData(EVAL_COM1, (uint8_t) ch); /* Loop until the end of transmission */ while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC ) == RESET) {}
Keywords:USART Reference address:Reasons and solutions for the loss of the first character of a string sent by USART

Previous article:stm32f051 key control (3)
Next article:STM32 clock tree analysis

Recommended ReadingLatest update time:2024-11-16 12:58

STM32F103xC USART/UART decomposition
Chip: STM32F103RCT6 (64pin, 256K, LQFP, -40~85) Object: USART         The STM32F103xC chip embeds three universal synchronous/asynchronous receiver transmitters (USART1, USART2 and USART3) and two universal asynchronous receiver transmitters (UART4 and UART5).         These five interfaces provide asynchronous communi
[Microcontroller]
STM32 USART2 sends data notes
Hardware device: STM32VCT6 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //使能USART2时钟    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  GPIO_InitStructure.GPIO_Speed = GPIO
[Microcontroller]
STM32F0 USART wakeup from STOP mode
   Recently, I am considering using the STM32F030/042 series to make some low-power products, such as the 433/315 copy-type remote control paired with the car navigation, which requires serial communication and STOP mode. But the annoying thing is that the built-in serial port wake-up function of the STM32F0 seri
[Microcontroller]
STM32F0 USART wakeup from STOP mode
STM32_USART serial communication detailed explanation
Anyone who knows about 51 MCU knows the working principle of serial communication of 51 MCU. Our MCU uses TTL level. In order to make our MCU communicate with PC, we need a level conversion chip to convert TTL level to USB level (USB interface is used. If DB9 interface is used, the level conversion chip is TTL to RS23
[Microcontroller]
STM32_USART serial communication detailed explanation
Initialization of ATmega32 USART
ATmega32 must first initialize USART before communicating. The initialization process usually includes setting the baud rate, setting the frame structure, and enabling the receiver or transmitter as needed. For interrupt-driven USART operation, the global interrupt flag must be cleared first during initialization (glo
[Microcontroller]
Initialization of ATmega32 USART
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号