STM loses the first byte when sending continuously

Publisher:SunshineHopeLatest update time:2018-10-17 Source: eefocusKeywords:STM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When I was compiling the program, I encountered a problem: the output of this program should be 57H 53H 07H D0H , but the actual debugging output was 53H 07H D0H, and the first byte 57H was lost. In fact, before this, when I was debugging the formaldehyde sensor, I found that the first command sent to the formaldehyde sensor failed. I naively thought it was a problem with the sensor and did not respond effectively to the command I sent. However, today's phenomenon shows that the serial port of the microcontroller failed to send out the first byte. So, I used Baidu and found the following excerpt: ———————————————————————————————————————————————— ———————————————————————————————————————————————— STM32 serial port transmission must first detect the status, otherwise the first byte cannot be sent. After sending, it is necessary to detect whether the sending status is completed, otherwise, the transmission is unsuccessful. When using stm32f10x to debug serial port communication, an error phenomenon is found. After hardware reset and restart, the test data 0x01 0x02 0x03 0x04 is sent.. The data received by the receiving end is: 0x02 0x03 0x04, and the first data is lost. Change to send data with other values, such as 0x06 0x0ff, then 0x0ff is received, and 0x06 is lost. The error remains.     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 data 0x01 0x02 0x03 0x04. 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 running is normal, and it seems that there is no error in the programming. So 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. Guess that maybe it is because of 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 overwriting may occur. Therefore, before instruction A, add the following instruction:    USART_ClearFlag(USART2,USART_FLAG_TC);     5. After adding the previous instruction, run and the error disappears. This shows that the previous assumption should be true.     6. Check the stm32f10x reference manual and find the following 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, and 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). In other words, you must first read USART_SR, then write USART_DR to complete the clearing of the TC status bit. After the hardware reset, the first data sent by the serial port is written to DR directly without the read SR operation, that is, TC is not cleared. This means 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. —————————————————————————————————————————————————— —————————————————————————————————————————————————— Inspired by this article, I did the following operations. According to the tips in Articles 7 and 8, I read USART_SR before sending the first byte. I put this operation in the serial port initialization, so that it is not necessary to perform this operation in each byte sending function, and it is enough to solve the first byte after initialization. As shown in the figure below. This solved the problem.
STM loses the first byte when sending continuously








































STM loses the first byte when sending continuously

Keywords:STM Reference address:STM loses the first byte when sending continuously

Previous article:stm32f429 three-channel ADC configuration
Next article:STM32CubeMX graphical configuration tool

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

stm32 learning four
systick (tick timer):  An example of testing the system tick timer is to light up the LEDs on the development board in turn (I chose to light up three LEDs in turn every 1s).  First of all, the characteristic of the system tick timer is that if the timer is set to start, it will automatically count. At this time, whe
[Microcontroller]
STM32F1_On-chip FLASH programming
Preface Today, we will summarize "STM32F103 on-chip FLASH programming". For those who learn programming, the word "FLASH" must be very familiar, because FLASH is mainly used to store data. For STM32, the capacity of internal FLASH varies from 16K to 2M, mainly depending on the chip model. For those who have just switc
[Microcontroller]
STM32F1_On-chip FLASH programming
Register address mapping in stm32
Peripheral base address ——》3 major peripheral bus base addresses (APB1, APB2, AHB) ——》Corresponding peripheral base addresses ——》Structure and register corresponding mapping
[Microcontroller]
STM32F429 serial communication_initialization
1. Related principles: 1. Initialize the HAL library; 2. Initialize the stm32 system clock, set the clock, including the PLL clock, and the peripheral clock APB1, APB2....... 3. Initialize the delay system; 4. Initialize the serial port, including the serial port, the GPIO port used by the serial port, enable the inte
[Microcontroller]
STM32 boot BOOT0 BOOT1 setting method
Different download methods correspond to different STM32 startup methods. The following figure shows three STM32 startup methods:     ● The first boot method is the most commonly used user FLASH boot. It works normally in this mode. The STM32 FLASH can be erased 100,000 times, so there is no need to worry that the c
[Microcontroller]
STM32 boot BOOT0 BOOT1 setting method
STM8 study notes ---- key interrupt
According to the Qingfeng STM8 development board, write the key_exti.h header file as follows: #ifndef  __KEY_EXTI_H #define  __KEY_EXTI_H #include "stm8s.h" #define KEY1_PIN        GPIO_PIN_0  #define KEY2_PIN        GPIO_PIN_4 #define KEY3_PIN        GPIO_PIN_3  #define KEY1_PORT       GPIOI #define KEY2_PORT   
[Microcontroller]
【stm32f407】NVIC
NVIC is called "Nested Vectored Interrupt Controller (NVIC)". The CM4 core supports 256 interrupts, including 16 core interrupts and 240 external interrupts, and has 256 levels of programmable interrupt settings. However, the STM32F4 does not use all of the CM4 core, but only uses part of it. STM32F40xx/STM32F41xx
[Microcontroller]
【stm32f407】NVIC
STM8S103 generates two symmetrical PWM waveforms
Use the two PWMs of Timer1 to generate waveforms and map the symmetrical waveforms at the same time. The purpose is, of course, wireless charging! timer.c void timer1_pwm_init() {     TIM1_CR1 |= 0x60; //     TIM1_CCMR1 |= 0x70; //CH1 output PWM, Mode 2     TIM1_CCMR2 |= 0x70; //CH2 output PWM, Mode 2     TIM1_CCE
[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号