STM32 serial port first byte and tail byte are less

Publisher:一直333Latest update time:2015-05-11 Source: 51heiKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The STM32 serial port must first detect the status when sending, otherwise the first byte cannot be sent. After sending, it is necessary to detect whether the sending status is completed, otherwise, the sending is unsuccessful.

When using stm32f10x to debug serial communication, an error was found. After hardware reset and restart, the test data 0x01 0x02 0x03 0x04 was sent. The data received by the receiving end was: 0x02 0x03 0x04, and the first data was lost. When other values ​​were sent, such as 0x06 0x0ff, 0x0ff was received, and 0x06 was lost. The error still exists.
    Troubleshooting process:
    1. At first, I suspected that it was an error on the receiving end. I used the computer serial port and ran the serial port auxiliary debugging tool to receive. After changing to other software, I found that the fault still existed, and the computer software was always in the open state, which did not seem to be related to the computer software.
    2. Using single-step debugging, single-step running of each sending instruction, all normal. Can receive 0x01 0x02 0x03 0x04 data. Indirectly ruled out that it was not a problem with the computer software, but other errors.
    3. Although the single-step debugging runs normally, the error still exists when running continuously. Now I am a little confused. The single-step operation is normal. It seems that there is no error in programming. Where is the fault? The test program is as follows
   USART_SendData(USART2, 0x01); //A
   while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); //B
   USART_SendData(USART2, 0x02); //C
   while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
   USART_SendData(USART2, 0x03);
   while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
   USART_SendData(USART2, 0x04);
   while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
    4. It is guessed that perhaps it is due to some special reason that the second data overwrites the first data, causing the first data to be lost. Assumption: When executing instruction B, the TC status bit of USART == SET, then instruction C will be executed immediately, and data may be overwritten. Therefore, add the following instruction before instruction A:
   USART_ClearFlag(USART2,USART_FLAG_TC);
    5. After adding the previous instruction, run it and the error disappears. It shows that the previous assumption should be established.
    6. Check the stm32f10x reference manual and find this sentence:
TC: Transmission completed
When a frame containing data is transmitted, the hardware sets this bit. If TCIE in USART_CR1 is 1, an interrupt is generated. The bit is cleared by the software sequence (read USART_SR first, then write USART_DR). The TC bit can also be cleared by writing 0. This clearing procedure is only recommended in multi-buffer communication.
0: Transmission is not yet completed;
1: Transmission is completed.
    7. Note this sentence: The bit is cleared by the software sequence (read USART_SR first, then write USART_DR). That is to say, you need to read USART_SR first, then write USART_DR, to clear the TC status bit. After the hardware reset, the first data sent by the serial port is directly written to DR without the read SR operation, that is, TC is not cleared. This shows that the guess in step 4 is correct.
    8. Then, the USART_ClearFlag(USART2,USART_FLAG_TC); added in front of instruction A should be changed to USART_GetFlagStatus(USART2, USART_FLAG_TC);, which should also eliminate the error. After testing, it is confirmed that this is indeed the case. Before sending the first data, read USART_SR first, then the first data will not be lost.
    9. Summary: After the hardware reset, read USART_SR before sending the first data of the serial port, which can ensure that there is no overwriting when the first data is sent. Of course, there are other methods, such as clearing the TC status bit first, or adding a small delay after writing USART_DR to allow the data to be sent, which should also indirectly eliminate this error.
 

How to use TXE and TC flags when STM32 USART sends data

There are two registers on the transmitting end of USART, one is the USART_DR register that can be seen by the program (the shaded TDR in the figure below), and the other is the shift register that cannot be seen by the program (the shaded Transmit Shift Register in the figure below).

There are two flags corresponding to USART data transmission, one is TXE = transmit data register empty, and the other is TC = transmission end; referring to the figure below, when the data in TDR is transferred to the shift register, TXE is set, and the shift register starts to transmit data bit by bit to the TX signal line, but because TDR has become empty, the program can write the next byte to be sent (operation USART_DR) into TDR without waiting until all bits in the shift register are sent. When all bits are sent (after sending the stop bit), the hardware will set the TC flag.

On the other hand, when the USART has just been initialized and no data has been sent, there will also be a TXE flag because the transmit data register is empty at this time.

The meaning of TXEIE and TCIE is very simple. TXEIE allows an interrupt to be generated when the TXE flag is '1', while TCIE allows an interrupt to be generated when the TC flag is '1'.

As for when to use which flag, you need to decide according to your needs. But I think TXE allows the program to have more time to fill the TDR register to ensure that the data flow is uninterrupted. TC allows the program to know the exact time when the transmission ends, which is helpful for the program to control the timing of the external data flow.

Keywords:STM32 Reference address:STM32 serial port first byte and tail byte are less

Previous article:stm32 interrupt serial port control LED light
Next article:Share STM32 FLASH erase (and prevent accidental erasure of program code), write

Recommended ReadingLatest update time:2024-11-16 03:25

What about STM32 development reset
Question: The program cannot run after power is supplied. It can only run after pressing the reset button. I don’t know why. 1Answer: After pulling down B0 and B1, the computer should start up.  Check if there is any problem with your reset circuit, for example, is the 104 capacitor missing? Don't use an external crys
[Microcontroller]
What about STM32 development reset
Learning STM32 serial port (2)
This study is mainly to organize the serial port receive interrupt The code for serial port interrupt initialization is as follows:  NVIC_InitTypeDef NVIC_InitStructure;  /* Configure the NVIC Preemption Priority Bits */    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //Set the priority group: preempt priority
[Microcontroller]
STM32--Low Power Mode
The STM32F10xxx has three low-power modes: ●Sleep mode (Cortex?-M3 core stops, peripherals are still running) ●Stop mode (all clocks are stopped) ●Standby mode (1.8V power supply is turned off)   When the clock frequency is 72MHz and the code is executed from the flash memory, the STM32 consumes 36mA, which is t
[Microcontroller]
STM32--Low Power Mode
A little understanding of STM32 system clock and frequency division
System clock and frequency division First, a passage from the manual. Three different clock sources can be used to drive the system clock (SYSCLK) HSI Oscillator Clock HSE Oscillator Clock PLL Clock The PLL clock is generally used, and there is evidence below. We can get the clock values ​​through the library func
[Microcontroller]
A little understanding of STM32 system clock and frequency division
STM32 uses systick to achieve precise delay
SYSTICK register initialization void SysTick_Configuration(void) {     if (SysTick_Config(SystemCoreClock / 100))          {         while (1);   }     NVIC_SetPriority(SysTick_IRQn, 0x0);                } SysTick_Config default clock is SysTick_CLKSource_HCLK, so using SysTick_CLKSourceConfig() to select the system c
[Microcontroller]
STM32 serial communication--data packaging and sending
Serial port and interrupt initialization void USART1Init(void) {   GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructu
[Microcontroller]
Understanding the Difficulties of STM32 Interface FSMC/FMC
1. FSMC Brief           FSMC, the Flexible Static Storage Controller, is capable of interfacing with synchronous or asynchronous memories and 16-bit PC memory cards. The FSMC interface of STM32 supports memories including SRAM, NAND FLASH, NOR FLASH and PSRAM. 2. FSMC storage block The STM32F767's FMC divides
[Microcontroller]
Input filtering mechanism of STM32 timer
The timer input channel of STM32 has a filter unit, which is located on each input path (yellow box in the figure below) and external trigger input path (blue box in the figure below). Their function is to filter out high-frequency interference on the input signal. The specific operating principle is as follows: C
[Analog Electronics]
Input filtering mechanism of STM32 timer
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号