A deep understanding of the stm32 serial communication receiving code

Recently I was doing an electronic game and found that some students would not write data processing code for serial communication when solving STM32 and OpenMV communication or other module serial communication. Today I will share my commonly used serial communication data processing code (mainly ideas)

The first step of serial communication-configure both sides to initialize the serial port

I will not go into details about the initialization of stm32 serial port, because the configuration source code can be found in many places, and today I mainly talk about the idea of ​​serial communication processing data. After understanding, no matter what board (51 or 430) you can use this logic

Receive interrupt configuration

The following is a section of the stm32 I used and the reception interrupt for receiving Bluetooth data

The following code 串口接收代码is: .

u8 USART1_RX_BUF[USART_REC_LEN];     //接收缓冲,最大USART_REC_LEN个字节.(一般给200,接收数据量大就增加)
u16 USART1_RX_STA;       //接收状态标记	

void USART1_IRQHandler(void)                	//串口1中断服务程序
{
	u8 Res;
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
	{
		Res =USART_ReceiveData(USART1);	//读取接收到的数据
		if((USART1_RX_STA&0x8000)==0)//接收未完成
		{
			//上次数据未处理则退出
			if(Res==0x0a)
			{
				USART1_RX_STA|=0x8000;
			}
			else
			{
				USART1_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
				USART1_RX_STA++;
				if(USART1_RX_STA>(USART_REC_LEN-1))
				USART1_RX_STA=0;
			}		 
		}
	}		
} 

Explain the code separately:

u8 USART1_RX_BUF[USART_REC_LEN];
This sentence is the receive buffer, temporarily stores the information you receive, and its length must be greater than the length of the completed data received by the serial port each time

u16 USART1_RX_STA;
Receiving state: RX STATE is a very important point. Its highest bit or several bits are used to retain the status of the received data, and the rest are used to retain the length information of the received data. Generally, the highest 1 bit saves the end information, which is set to 0 by default. It is set to 1 when receiving the end flag code defined by us (indicating that a group of data is received and waiting to be processed), and then every time it enters the receive interrupt First judge whether this bit has been set. If it has been set, it means that there is a set of unprocessed data in the buffer and will not continue to receive the following data.

The specific judgment code is as follows

	if((USART1_RX_STA&0x8000)==0)//判断缓存是否为空
		{
			//首先判断本次接收的内容是不是结尾码0x0a
			if(Res==0x0a)
			{
				USART1_RX_STA|=0x8000;//如果是把最高位置1表示接收完成一组数据
			}
			else
			{
			//不是则把当前的数据按照顺序放入缓存里面
			//记录数据长度,判断是否超出长度,超出则清除最高位,缓存重新接收
				USART1_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
				USART1_RX_STA++;
				if(USART1_RX_STA>(USART_REC_LEN-1))
				USART1_RX_STA=0;
			}		 
		}

Data processing after receiving

The data processing is placed in the main function. A scan period can be set to determine whether the highest bit of STA is set. After it is set, the data processing function is executed.
code show as below

	if(USART1_RX_STA&0x8000)
		{
			//处理数据的函数(用户自定义)
      		X=(USART2_RX_BUF[1]-'0')*100+(USART2_RX_BUF[2]-'0')*10+(USART2_RX_BUF[3]-'0');
			J=(USART2_RX_BUF[5]-'0')*100+(USART2_RX_BUF[6]-'0')*10+(USART2_RX_BUF[7]-'0');
			//清除接收完成状态位,等待下一次接收完成
			USART1_RX_STA=0;
		}

The processing function of the serial port reception is basically just these contents, written in this way, basically all serial communication can be applied, stable and convenient! ! !

Guess you like

Origin blog.csdn.net/qq_45396672/article/details/109097842