
Using USART to send strings with STM32:




The meaning of the code is:
When there is data on the receive pin, the status register USART_FLAG_RXNE will be set to 1. At this point, the return value of USART_GetFlagStatus(USART1, USART_FLAG_RXNE) will be 1 (SET), and if there is no data, it will be RESET.
Common code writing methods and their data reception effects:
Method 1:


This method works well under normal conditions (not in special situations such as power-off or standby), and the USART data will be successfully sent. However, in the aforementioned special situations, the issue arises where the code only places the data in the send buffer without actually sending it before power-off or standby, resulting in the last two characters not being sent.
Method 2:


The difference with this method is that the second-to-last data is sent, meaning only the last character is not sent.
Method 3:


This method results in sending 10 characters.
Method 4:


This method should theoretically achieve the function, but in practice, multiple tests have shown that the first byte of data is lost.
Method 5:


This method is relatively complete, and to be conservative, it is recommended to use this method in special situations.


Screenshots from some electronic books

【Complete Set of Hardware Learning Materials Collection】
