欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

STM32+串口空闲中断实现不定长数据接收

程序员文章站 2022-07-04 19:38:41
...

常见的数据帧断帧方式有两种,一种是通过固定的协议格式断帧,另外一个是通过两帧数据的时间断帧,下面介绍最后一种,利用STM32F1的串口空闲中断实现不定长的数据断帧。笔者使用的标准官方库。

空闲中断是接受数据后出现一个字节的空闲状态,就会触发空闲中断。利用该特性区分两个不同数据帧。

1.初始化USART

​ 按照官方的初始化流程配置,需要注意一点就是需要开启接收中断和空闲中断。


/**
  * @brief  USART初始化.
  * @param
	* @arg BaudRate 波特率
  * @retval None
  */
void Usart_Init(uint32_t baudrate)
{
	USART_DeInit(USART1);
<span class="token function">RCC_APB2PeriphClockCmd</span><span class="token punctuation">(</span>RCC_APB2Periph_USART1<span class="token punctuation">,</span> ENABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>
USART_InitStructure<span class="token punctuation">.</span>USART_BaudRate <span class="token operator">=</span> baudrate<span class="token punctuation">;</span>
USART_InitStructure<span class="token punctuation">.</span>USART_WordLength <span class="token operator">=</span> USART_WordLength_8b<span class="token punctuation">;</span>
USART_InitStructure<span class="token punctuation">.</span>USART_StopBits <span class="token operator">=</span> USART_StopBits_2<span class="token punctuation">;</span>
USART_InitStructure<span class="token punctuation">.</span>USART_Parity <span class="token operator">=</span> USART_Parity_No<span class="token punctuation">;</span>
USART_InitStructure<span class="token punctuation">.</span>USART_HardwareFlowControl <span class="token operator">=</span> USART_HardwareFlowControl_None<span class="token punctuation">;</span>
USART_InitStructure<span class="token punctuation">.</span>USART_Mode <span class="token operator">=</span> USART_Mode_Rx <span class="token operator">|</span> USART_Mode_Tx<span class="token punctuation">;</span>
<span class="token function">USART_Init</span><span class="token punctuation">(</span>USART1<span class="token punctuation">,</span> <span class="token operator">&amp;</span>USART_InitStructure<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">USART_ITConfig</span><span class="token punctuation">(</span>USART1<span class="token punctuation">,</span> USART_IT_RXNE<span class="token punctuation">,</span> ENABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//开启接收中断</span>
<span class="token function">USART_ITConfig</span><span class="token punctuation">(</span>USART1<span class="token punctuation">,</span> USART_IT_IDLE<span class="token punctuation">,</span> ENABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>		<span class="token comment">//开启空闲中断</span>

NVIC_InitStructure<span class="token punctuation">.</span>NVIC_IRQChannel <span class="token operator">=</span> USART1_IRQn<span class="token punctuation">;</span>
NVIC_InitStructure<span class="token punctuation">.</span>NVIC_IRQChannelPreemptionPriority <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span>
NVIC_InitStructure<span class="token punctuation">.</span>NVIC_IRQChannelSubPriority <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
NVIC_InitStructure<span class="token punctuation">.</span>NVIC_IRQChannelCmd <span class="token operator">=</span> ENABLE<span class="token punctuation">;</span>
<span class="token function">NVIC_Init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>NVIC_InitStructure<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">USART_Cmd</span><span class="token punctuation">(</span>USART1<span class="token punctuation">,</span> ENABLE<span class="token punctuation">)</span><span class="token punctuation">;</span>

}

2.实现中断接收

​ 代码如下:


#define RXBUFF_SIZE	100
uint8_t rxbuff[RXBUFF_SIZE];
uint8_t rx_index;
uint8_t rec_over;
/**
  * @brief  USART接收中断.
  * @param  None
  * @retval None
  */
void RxIrqDeal()
{
	uint8_t temp;
	if(USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET){	   //防止溢出中断
		USART_ReceiveData(USART1); 
	}   
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){		//USART接收中断
        temp = USART_ReceiveData(USART1);
		if(rx_index > RXBUFF_SIZE){		   //溢出不再接收数据
			return;
		}
		rxbuff[rx_index++] = temp;
	}
	else if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) != RESET){		//USART空闲中断
		USART_ReceiveData(USART1);
		rec_over = 1;		//接收完成
	}
}void USART1_IRQHandler(void)
{
	RxIrqDeal();
}

​ 这里有几个需要注意的地方,

​ 1.开启接收中断的同时会开启ORE中断

​ 2.IDLE中断和ORE中断需要USART_GetFlagStatus获取中断标识后通过读取USART_SR寄存器消除。这里可以从标准的函数说明可以看出,图下红框部分。

STM32+串口空闲中断实现不定长数据接收

3.提取缓存数据

​ 在应用层中判断接收完成标志位,按照用户定义的通讯格式解析数据。

/**
  * @brief  解析缓存数据.
  * @param  None
  * @retval None
  */
void  app_task()
{
	if(rec_over){
		rec_over = 0;
		//TODO
	}
}
相关标签: STM32F系列单片机