STM32中串口中断接收异常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32中串口中断接收异常相关的知识,希望对你有一定的参考价值。
我在中断处理函数中用了两种方式处理接收的数据,思路代码基本一样,结果却不一样?
下面是简化的代码:
方法一
void USART1_IRQHandler(void)
if(USART_GetITStatus(USART1, USART_IT_RXNE)!= RESET)
switch(RecState)
case HEAD1:
if(0x68 == USART_ReceiveData(USART1))
RecState = DLTADDR0;
break;
case DLTADDR0:
if(0xaa == USART_ReceiveData(USART1))
RecState = DLTHEAD;
else
RecState = HEAD1;
break;
case DLTHEAD:
if(0x68 == USART_ReceiveData(USART1))
RecState = DLTCS;
else
RecData = HEAD1;
break;
case DLTCS:
if(0xdf == USART_ReceiveData(USART1))
RecState = DLTEND;
else
RecState = HEAD1;
break;
case DLTEND:
if(0x16 == USART_ReceiveData(USART1))
g_DltRec = 1;
RecState = HEAD1;
else
RecState = HEAD1;
break;
default:
RecState = HEAD1;
break;
/*Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
方法二
void USART1_IRQHandler(void)
if(USART_GetITStatus(USART1, USART_IT_RXNE) !=RESET)
RecData = USART_ReceiveData(USART1);
switch(RecState)
case HEAD1:
if(0x68 == RecData)
RecState = DLTADDR0;
break;
case DLTADDR0:
if(0xaa == RecData)
RecState = DLTHEAD;
else
RecState = HEAD1;
break;
caseDLTHEAD:
if(0x68 == RecData)
RecState = DLTCS;
else
RecData = HEAD1;
break;
case DLTCS:
if(0xdf == RecData)
RecState = DLTEND;
else
RecState = HEAD1;
break;
case DLTEND:
if(0x16 == RecData)
g_DltRec = 1;
RecState = HEAD1;
else
RecState = HEAD1;
break;
default:
RecState = HEAD1;
break;
/*Clear the USART1 Receive interrupt */
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
其实就是加了一个中间变量接收串口的数据,结果却不一样,加了就正确。求解?RecData= USART_ReceiveData(USART1);
发送数据为16进制 68 aa 68 df 16
结果是为了置位g_ DltRec。
RecState为全局变量,默认值是常量HEAD1
RecData = USART_ReceiveData(USART1);
也放到每个case中看看是不是对的。如果对的说明不能采用这种模式接受,如果出现同样情况那可能和你case有关了。追问
把RecData = USART_ReceiveData(USART1); 放在每个case中可以。但是不明白为什么if(0x16 == USART_ReceiveData(USART1))不能接收数据,USART_ReceiveData(USART1)不也是读取串口数据吗
STM32串口中断接收问题
void USART6_IRQHandler(void)
while(USART_GetITStatus(USART6, USART_IT_RXNE)!= RESET)
// USART_ClearITPendingBit(USART2, USART_IT_RXNE);
Buffer[dataLen1]=USART6->DR;
dataLen1++;
if(dataLen1>1200)
dataLen1=0;
GPIO_SetBits(GPIOD, GPIO_Pin_13);
Flag=1;
void USART6_Puts(char * str)
while(*str)
USART_SendData(USART6, *str++);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART6, USART_FLAG_TC) == RESET); /
main()
GPIO_Config();
USART6_Config();
USART1_Config();
DMA_Config();
NVIC_Config();
GPIO_SetBits(GPIOD, GPIO_Pin_12); //打开LED
while (1)
if(Flag==1)
USART6_Puts(Buffer);
Flag=0;
GPIO_ResetBits(GPIOD, GPIO_Pin_13);
若串口发送的数据在3个以内,接收正常,显示正常,若串口发送数据大于4个的时候,只能接收前四位!小弟望得到大神们的帮助
以上是关于STM32中串口中断接收异常的主要内容,如果未能解决你的问题,请参考以下文章