STM32只进入一次接收中断

2019-08-18 19:48发布

STM32只有在程序复位时第一次发送数据能进入接收中断,之后再发送数据无法进入接收中断。程序一直正常运行代码
[mw_shl_code=c,true]void USART1_IRQHandler(void)
{
  u8 ucUartTemp;
  if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  {
    ucUartTemp = USART_ReceiveData(USART1);
    Usart1.TimeOutCounter = Reset;
    if(Usart1.Usart_RxStatus == RX_IDLE)
    {
      if(ucUartTemp == Usart_StartMark )
      {
        
        Usart1.Usart_RxStatus = RX_HEAD;
        Usart1.Usart_ReceiveCache[CPOS_Start] = ucUartTemp;
        Usart1.Usart_Cache_Where = CPOS_CMD;
      }
    }
    else if(Usart1.Usart_RxStatus == RX_HEAD )
    {
      if(Usart1.Usart_Cache_Where == CPOS_Length-1)
        Usart1.Usart_RxStatus = RX_Length;
      Usart1.Usart_ReceiveCache[Usart1.Usart_Cache_Where] = ucUartTemp;
      Usart1.Usart_Cache_Where ++;
    }
    else if(Usart1.Usart_RxStatus == RX_Length)
    {
      Usart1.Usart_ReceiveCache[Usart1.Usart_Cache_Where] = ucUartTemp;
      Usart1.Usart_Cache_Where++;
//      if((Usart1.Usart_Cache_Where > Usart1_ReceiveLength) ||
//         (Usart1.Usart_Cache_Where > (Usart1.Usart_ReceiveCache[CPOS_Length] -'0')*10 + (Usart1.Usart_ReceiveCache[CPOS_Length+1] -'0')*1))
//      {//接收超过限定长度
//        Usart1.Err_Code = ERR_OutLength;
//        Usart1.Usart_RxStatus = RX_END;
//      }
      if((Usart1.Usart_ReceiveCache[CPOS_Start] == Usart_StartMark ) &&
         Usart1.Usart_Cache_Where == Usart1.Usart_ReceiveCache[CPOS_Length] )
      {//正确接收完成
        Usart1.Usart_RxStatus = RX_END;
      }
    }
  }
    ucUartTemp=USART1->SR;
    //if(ucUartTemp==RESET)ucUartTemp=Reset;
    ucUartTemp=USART1->DR;
    //if(ucUartTemp==RESET)ucUartTemp=Reset;
}[/mw_shl_code]下面是初始化的代码
[mw_shl_code=applescript,true]void Five_usart_init(void)
{
  /* Timer Clock configuration ------------------------------------------------*/
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 ,ENABLE);
//  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2 ,ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
  

  
  /* Timer Interrupt configuration ---------------------------------------------*/
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//4组抢占 4组从优先
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_Init(&NVIC_InitStructure);
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_Init(&NVIC_InitStructure);
  NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  NVIC_Init(&NVIC_InitStructure);
//  NVIC_InitStructure.NVIC_IRQChannel = USART5_IRQChannel;
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
//  NVIC_Init(&NVIC_InitStructure);
  
  /* Usart configuration ---------------------------------------------*/
  Usart_init(USART1,Usart1_BandRate,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_HardwareFlowControl_None);
  USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  USART_Cmd(USART1, ENABLE);
  //使能u1-tx DMA发送
  Usart_DMAtx_init(USART1,DMA1_Channel4);
  DMA_Cmd(DMA1_Channel4, ENABLE);
  USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);

  Usart_init(USART2,Usart2_BandRate,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_HardwareFlowControl_None);
  USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
  //USART_Cmd(USART2, ENABLE);  //使能串口2
  
  Usart_init(USART3,Usart3_BandRate,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_HardwareFlowControl_None);
  USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
// USART_Cmd(USART3, ENABLE);  //使能串口3
  
  Usart_init(UART4,Usart4_BandRate,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_HardwareFlowControl_None);
  USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
  //USART_Cmd(UART4, ENABLE);  //使能串口4
  
  Usart_init(UART5,Usart5_BandRate,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_HardwareFlowControl_None);
  USART_ITConfig(UART5,USART_IT_RXNE,ENABLE);
  //USART_Cmd(UART5, ENABLE);  //使能串口5
  DelayNms(50);
  /* Timer GPIO configuration ------------------------------------------------*/
  Set_Port_Status(GPIO_A,GPIO_Pin_9,GPIO_Mode_AF_PP);          //TX1
  Set_Port_Status(GPIO_A,GPIO_Pin_10,GPIO_Mode_IN_FLOATING);   //RX1
  Set_Port_Status(GPIO_A,GPIO_Pin_2,GPIO_Mode_AF_PP);        //TX2
  Set_Port_Status(GPIO_A,GPIO_Pin_3,GPIO_Mode_IN_FLOATING);  //RX2
  Set_Port_Status(GPIO_B,GPIO_Pin_10,GPIO_Mode_AF_PP);       //TX3
  Set_Port_Status(GPIO_B,GPIO_Pin_11,GPIO_Mode_IN_FLOATING); //RX3
  Set_Port_Status(GPIO_C,GPIO_Pin_10,GPIO_Mode_AF_PP);       //TX4
  Set_Port_Status(GPIO_C,GPIO_Pin_11,GPIO_Mode_IN_FLOATING); //RX4
  //Set_Port_Status(GPIO_C,GPIO_Pin_12,GPIO_Mode_AF_PP);       //TX5
  //Set_Port_Status(GPIO_C,GPIO_Pin_13,GPIO_Mode_IN_FLOATING); //RX5
}
/*******************************************************************************
*函数名称:
*******************************************************************************/
void Usart_init(USART_TypeDef* USARTx,u32 Usartx_BandRate,u16 USARTx_WordLength,u16 USARTx_StopBits,u16 USARTx_Parity,u16 USARTx_HardwareFlowControl)
{
  USART_InitTypeDef USART_InitStructure;                          
  USART_InitStructure.USART_BaudRate = Usartx_BandRate;           
  USART_InitStructure.USART_WordLength = USARTx_WordLength;     
  USART_InitStructure.USART_StopBits = USARTx_StopBits;         
  USART_InitStructure.USART_Parity = USARTx_Parity;            
  USART_InitStructure.USART_HardwareFlowControl = USARTx_HardwareFlowControl;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USARTx, &USART_InitStructure);
}
/*******************************************************************************
*函数名称:
*******************************************************************************/
void Usart_DMAtx_init(USART_TypeDef* USARTx,DMA_Channel_TypeDef * DMAx_Channelx)
{
  DMA_InitTypeDef DMA_InitStructure;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USARTx->DR);
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)(&USARTx->DR);
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMAx_Channelx, &DMA_InitStructure);
}[/mw_shl_code]

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
正点原子
1楼-- · 2019-08-18 21:19
参考我们例程
769417953
2楼-- · 2019-08-19 02:25
再次调试之后发现波特率调到115200时偶尔第二次能进入接收中断,第三次之后还是不能进入接收中断

一周热门 更多>