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.
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Why does a computer become slow when not used for a long time?
- EEWORLD University ---- Machine Learning
- Has anyone tried the baud rate adaptation of LIN communication?
- Introduction to TI_DSP link command file (*.cmd)
- [N32L43x Review] 2. Turn on the lights, blink, blink, blink...
- EEWORLD University Hall - Talking about the development creativity of electronic products and using network resources to help development
- The output of the op amp in the active integrator circuit oscillates. What should I do?
- CircuitPython controlled snowy Christmas tree
- When replying, just press CTRL+Enter to post directly. Details determine success or failure. You must be like Master Qiao.
- This circuit can adjust the voltage under no-load condition, but the voltage is only about 2V after the load is installed.