求大神指教,stm32接收串口的数据流,不能保证百分百解码数据,困扰了好久。

zxj19900621 2015-10-06 04:44:39
问题描述:
串口向stm32发送数据比如:00aa 0x01 0x06 oxbb 其中oxaa是数据开始的标志,0xbb是数据接收的标志。程序的功能就是接收oxaa 和0xbb之间的数据。我写的程序时而可以正确解码得到数据时而不能,好生困扰!求大神指教。
程序:
void uart_init(u32 bound){

GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
USART_DeInit(USART1);
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA9

//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//Usart1 NVIC ÅäÖÃ

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

//USART ³õʼ»¯ÉèÖÃ

USART_InitStructure.USART_BaudRate = bound;
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;

USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);/
USART_Cmd(USART1, ENABLE);

}

/*******************************************************************************
* Function Name : USART1_IRQHandler
* Description : This function handles USART1 global interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_IRQHandler(void)
{
u8 i=0;
if(USART_GetFlagStatus(USART1, USART_IT_RXNE) != RESET) //
{
// hight = USART_ReceiveData(USART1);
// flag = 1;
rxbuffer[rxcounter++] = USART_ReceiveData(USART1);
if(rxbuffer[rxcounter-1]==0xAA)
{
rxcounter=1;
rxbuffer[0]=0xAA ;
}
if(rxbuffer[rxcounter-1]==0xBB)
{

if(rxcounter == 4)
{
flag1 = 1;
flag = 1;
frequency = rxbuffer[1];
amp=rxbuffer[2];
}
for(i=0;i<20;i++)
rxbuffer[i]=0x00 ;
rxcounter=0;

}
}

if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //

}
...全文
213 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
子逑 2015-10-14
  • 打赏
  • 举报
回复
中断只用于接收,处理在main
91program 2015-10-08
  • 打赏
  • 举报
回复
把时而不能的数据找出来,分析一下是数据的原因引起的,还是其它原因引起的
听涛-于斯 2015-10-07
  • 打赏
  • 举报
回复
aa bb作为头尾的数据包,错误耦合率有点高
dceacho 2015-10-07
  • 打赏
  • 举报
回复
弄一个大RING BUFFER, main中一个字节一个自己的匹配
默寞 2015-10-07
  • 打赏
  • 举报
回复
aa与bb之间的数据有没有是aa或bb的
玉怀一捧雪 2015-10-06
  • 打赏
  • 举报
回复
中断用于接收,主函数用来解析不就OK了~ 长度固定的就先判断长度是否满足;然后查看帧头帧尾;

27,375

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧