打印

STM32 串口2 接收不到中断数据

[复制链接]
556|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
车水马龙|  楼主 | 2018-6-29 15:34 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一下部分,是我的串口2 初始化部分;
void USART2_Config(void)
{
//Uart2 GPIO口初始化
GPIO_InitTypeDef GPIO_InitStructure;
//Uart2 串口模式初始化
USART_InitTypeDef USART_InitStructure;
//Uart2 接受中断初始化
NVIC_InitTypeDef NVIC_InitStructure;

/* config USART2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* USART2 GPIO config */
/* Configure USART2 Tx (PA.2) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);   
/* Configure USART3 Rx (PA.3) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART2 mode config */
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;
USART_Init(USART2, &USART_InitStructure);

/* 使能串口2接收中断 */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   
USART_Cmd(USART2, ENABLE);
    /* Configure the NVIC Preemption Priority Bits */  
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;  
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
一下部分是我的中断接收函数
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
{
  USART_ClearFlag(USART2 , USART_FLAG_ORE);
  USART_ReceiveData(USART2);
  if(RecCard_Ok_Flag == 0)
  {
   if(Count < 4)
   {
     RecCardData_Buf[Count] = USART_ReceiveData(USART2);
    Count ++;
   }
   else if(Count == 4)
   {
    RecCardData_Buf[Count] = USART_ReceiveData(USART2);
    if(RecCardData_Buf[4]==(RecCardData_Buf[0]^RecCardData_Buf[1]^RecCardData_Buf[2]^RecCardData_Buf[3]))
    {
     RecCard_Ok_Flag = 1;
     USART_ClearITPendingBit(USART2,USART_IT_RXNE);
    }   
   }
   else
   {
    Count = 0;
    RecCard_Ok_Flag = 0;
     
   }
  }
}
}
就这样,中断函数一直进不去。求大神指点啊。

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

474

主题

476

帖子

0

粉丝