STM32-Serial Port Experiment Study Notes

Publisher:第二眼帅哥Latest update time:2016-01-11 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
USART1_IRQHandler(void) function:

When a corresponding interrupt occurs on serial port 1, it will jump to the function execution. A small receiving protocol is designed here (not defined by the system): through this function, an array USART_RX_BUF[ ] and a receiving status register USART_RX_STA are used to manage the receiving of serial port data. The maximum value of USART_RX_BUF[ ] is 64, which means that the maximum data received at one time cannot exceed 64 bytes. USART_RX_STA is a receiving status register, and the definition of each bit is shown in the table:        

               

(Note: This is a protocol designed by the author. How to judge whether the serial port has received a group of data? Since the length of the data received each time is different, the least is 3 8-bit data, and the most is more than ten. The number of data is uncertain and irregular. Is there any good way to make it receive completely?

The design ideas of the protocol are as follows:

When receiving data sent from the computer, the received data is saved in USART_RX_BUF, and the number of valid data received is counted in the receive status register (USART_RX_STA). When the first byte 0X0D of the carriage return (0X0D, 0X0A) is received, the flag bit, that is, the sixth position, is 1, and the counter will no longer increase, waiting for the arrival of 0X0A. If 0X0A does not arrive, it is considered that this reception has failed and the next reception will be restarted. If 0X0A is successfully received, the seventh bit of USART_RX_STA is marked, so that one reception is completed, and the bit is cleared by other programs, so as to start the next reception. If 0X0D is not received for a long time, then when the received data exceeds 64, the previous data will be discarded and received again. Since this register is defined by the author, we may be easily confused when we understand it. In fact, this is similar to a normal register, except that the author defines an 8-bit number and stipulates the definition of each bit. The first two bits are two flag bits, and the last six bits are used for counting, which is easy to understand. The function code is as follows:

 

[cpp]  view plain copyView code snippets on CODEDerive to my code snippet
 
  1. void USART1_IRQHandler(void)  
  2. {  
  3.     u8 res;       
  4.     if(USART1->SR&(1<<5))//Receive data USART1 should be a structure defined by the author, containing various registers  
  5.     {      
  6.         res=USART1->DR;   
  7.         if((USART_RX_STA&0x80)==0)//receiving is not completed  
  8.         {  
  9.             if(USART_RX_STA&0x40) //Received 0x0d  
  10.             {  
  11.                 if(res!=0x0a)USART_RX_STA=0; //Receive error, restart  
  12.                 else USART_RX_STA|=0x80; //Receiving completed   
  13.             }else //Has not received 0X0D  
  14.             {     
  15.                 if(res==0x0d)USART_RX_STA|=0x40;  
  16.                 else  
  17.                 {  
  18.                     USART_RX_BUF[USART_RX_STA&0X3F]=res;  
  19.                     USART_RX_STA++;  
  20.                     if(USART_RX_STA>63)USART_RX_STA=0; //Receive data error, restart receiving        
  21.                 }          
  22.             }  
  23.         }                                                  
  24.     }                                              
  25. }   

In fact, we can use the agreement method

Keywords:STM32 Reference address:STM32-Serial Port Experiment Study Notes

Previous article:STM32-Serial port timeout judgment method receives unknown length data
Next article:STM32-Key Input Experiment Study Notes

Latest Microcontroller Articles
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号