STM32串口接收一帧数据方法(处理一帧数据中所需内容)

stm32支持接受单个数据或者一帧数据,若配置单个数据接收中断的话,会出现接收包丢包,数据不完整的情况!因此在stm32的串口中断中,还有一个IDLE中断,用来产生串口接受一帧数据而产生的中断,比如说串口发来n个数据,会产生n次接收中断和一次IDLE中断,因此方便使用者来接收主机发送或者从机返回的数据!

原文链接:https://blog.csdn.net/qq_35341807/article/details/79157437

1、配置串口中断

void USART1_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	/* Configure USART1 Rx (PA.10) as input floating */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;		  //¸¡¿ÕÊäÈëģʽ	   
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;			  //¸
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = 9600;						//
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;		//
	USART_InitStructure.USART_StopBits = USART_StopBits_1;			//
	USART_InitStructure.USART_Parity = USART_Parity_No;				//
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   //
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;					
	/* Configure USART1 */
	USART_Init(USART1, &USART_InitStructure);							
	/* Enable USART1 Receive and Transmit interrupts */
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                    //
	USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//
	/* Enable the USART1 */
	USART_Cmd(USART1, ENABLE);	                  //
}

关键

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//没收到一个字节进入一次中断
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //开启串口空闲中断,每收到一帧数据进入一次中断

2、串口中断函数

处理一帧数据中所需要的内容

一帧数据原始内容:
在这里插入图片描述
我要提取出这帧数据中的31.93

char rece_buffer[BUFSIZ];
int RxCounter;

void USART1_IRQHandler(void)	
{
	 u8 clear=clear;
	 USART_ClearFlag(USART1,USART_FLAG_TC);
 //接收一帧数据
	 if(USART_GetITStatus(USART1,USART_IT_RXNE)!=Bit_RESET)        
	   {
 
           rece_buffer[RxCounter++]=USART1->DR;
	   }
		 
	 else if(USART_GetFlagStatus(USART1,USART_FLAG_IDLE)!=Bit_RESET)
	        {					
                clear=USART1->SR;
                clear=USART1->DR;						
	              RxCounter=0;
						
						//温度传感器的一帧数据,对需要的内容进行提取
						if((rece_buffer[0]=='o')&&(rece_buffer[1]=='b')&&(rece_buffer[2]=='j'))
						{
								if(rece_buffer[9]=='-')  //¸ºÊý
								{
								;
								}
						else
            {
								if(rece_buffer[10]=='.')   //x.xx
								{
								obj=(rece_buffer[9]-0x30)*100+(rece_buffer[11]-0x30)*10+(rece_buffer[2]-0x30);
								}
								if(rece_buffer[11]=='.')   //xx.xx
								{
								obj=(rece_buffer[9]-0x30)*1000+(rece_buffer[10]-0x30)*100+(rece_buffer[12]-0x30)*10+(rece_buffer[13]-0x30);
								}
								if(rece_buffer[12]=='.')   //xxx.xx
								{
								obj=(rece_buffer[9]-0x30)*10000+(rece_buffer[10]-0x30)*1000+(rece_buffer[11]-0x30)*100+(rece_buffer[13]-0x30)*10+(rece_buffer[14]-0x30)*10;
								}
								
						    obj_T=obj/100;
						}
						printf("目标温度:%f",obj_T);
						}
						//printf("%s",rece_buffer);
                }	
					
}

最终获得需要是数据
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43765237/article/details/107599234
今日推荐