stm32设置中断之后无法使用printf

2019-03-23 19:40发布

使用的的库函数编写的程序,想用中断接收数据然后用的printf发送的数据。使用的是USART2接收数据然后使用USART1发送数据。
    加了中断设置之后进入printf就进入死循环了怎么办,好像是无法检测到发送标志,该怎么处理呢。

/*************************************************************
* 中断初始化函数
*************************************************************/

void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_SetVectorTable(NVIC_VectTab_FLASH,0X0000);
  /* Configure the NVIC Preemption Priority Bits */  
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  
  /* Enable the USART1 Interrupt */    //之前把USART1的中断设置取消了也不行
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Enable the USART2 Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/*************************
* 主函数
**************************/
int main(void)
{   
    USART_InitTypeDef USART_InitStructure;    //串口结构体定义
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
      
      SystemInit();   //时钟初始化
      
      NVIC_Configuration();
     
      //RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
      GPIO_config();      //GPIO端口初始化
      
    USART_InitStructure.USART_BaudRate = 115200;
    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;
    STM_EVAL_COMInit(COM1, &USART_InitStructure);

      USART_InitStructure.USART_BaudRate = 9600;
    USART_Init(USART2, &USART_InitStructure); //串口2初始化函数
   
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
      USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
      USART_Cmd(USART2, ENABLE);
   
    printf("串口成功%d ",SystemCoreClock);
      printf("串口成功%d ",SystemCoreClock);
         
    /*主循环 */
    while (1)
    {
        if(cycle!=0)
      {
          printf("周期是%d.%ds ",cycle/100,cycle%100);
          cycle=0;
       }
    }   
}


/*******************************************************************************
* 串口打印函数
*******************************************************************************/

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(EVAL_COM1, (uint8_t) ch);

  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)
  {}

  return ch;
}

/*串口2中断处理函数*/
void USART2_IRQHandler(void)
{
    static u8 a=0;
  if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断
  {
    /* Read one byte from the receive data register */
    RXbuffer[a++] = USART_ReceiveData(USART2);  //接收串口2的数据存到RXbuffer
    if(a==12 || RXbuffer[a]=='')  //判断接收完成
    {
        a=0;
        cycle=(u32)(RXbuffer[8]<<8)+RXbuffer[9];
        //printf("周期是%d.%ds ",cycle/100,cycle%100);
         //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//当接收完成时打开串口1发送中断
    }
    }
  
  /*if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)  //判断发送中断
  {   
   
    USART_SendData(USART2,RXbuffer1[a++]);  //发送buffer数据
    if(a==9 || RXbuffer1[a]=='')
    {
        a=0;
        USART_ITConfig(USART2, USART_IT_TXE, DISABLE);//发送中断关闭
    }
  }*/
} 此帖出自小平头技术问答
0条回答

一周热门 更多>