【求助】stm32L151c8t6串口不能进接收中断

2019-08-19 16:23发布

求助各位大神!
代码查了几天了,一直没找到问题在哪,各位帮忙看一看,谢谢啦!
问题描述:
    用stm32L151c8t6作的串口通信,板子与pc连接,PC端是串口调试助手,
    stm发送出的数据,在PC端能够正常接收;
    但是pc端发送的数据,stm接收不到
    仿真器调试,发现是没有进到接收中断程序,但是TXE中断是可以进入的
请各位帮忙看一下。我的代码如下:
void usart1_init(void)
{
    usart1_rcc_config();
    usart1_gpio_config();
    usart1_usart_config();
    NVIC_Configuration();
}

//rcc config

void usart1_rcc_config(void)
{   
    //enable gpio clock
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    //enable usart1 clock
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}

//gpio config
void usart1_gpio_config(void)
{
    GPIO_InitTypeDef usart1_gpio_initstructure;
   
    //USART_DeInit(USART1);
    //connect PA.9 to usart1's tx
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
    //connect PA.10 to usart1's rx
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
   
    /* Configure USART Tx as alternate function push-pull */
    usart1_gpio_initstructure.GPIO_Pin = GPIO_Pin_9;
    usart1_gpio_initstructure.GPIO_Mode = GPIO_Mode_AF;
    usart1_gpio_initstructure.GPIO_Speed = GPIO_Speed_10MHz;
    usart1_gpio_initstructure.GPIO_OType = GPIO_OType_PP;
    usart1_gpio_initstructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &usart1_gpio_initstructure);
   
    /* Configure USART Rx as alternate function push-pull */
    usart1_gpio_initstructure.GPIO_Pin =GPIO_Pin_10;
      usart1_gpio_initstructure.GPIO_Mode = GPIO_Mode_IN;
      usart1_gpio_initstructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA,&usart1_gpio_initstructure);
}


//usart config
void usart1_usart_config(void)
{
      USART_InitTypeDef    usart1_usart_initstructure;
      //USART_ClockInitTypeDef usart1_clock_initstructure;
   
      //usart1_clock_initstructure.USART_Clock = USART_Clock_Disable;
      //usart1_clock_initstructure.USART_CPHA = USART_CPOL_Low;
      //usart1_clock_initstructure.USART_CPOL = USART_CPHA_2Edge;
      //usart1_clock_initstructure.USART_LastBit = USART_LastBit_Disable;
      //USART_ClockInit(USART1,&usart1_clock_initstructure);
   
      //USART_DeInit(USART1);
      usart1_usart_initstructure.USART_BaudRate = 115200;
    usart1_usart_initstructure.USART_WordLength = USART_WordLength_8b;
    usart1_usart_initstructure.USART_StopBits = USART_StopBits_1;
    usart1_usart_initstructure.USART_Parity = USART_Parity_No;
    usart1_usart_initstructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    usart1_usart_initstructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
   
      //configuration
      USART_Init(USART1, &usart1_usart_initstructure);
            
      // uart_it enable
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
      //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
      
      //enable
      USART_Cmd(USART1, ENABLE);
}
void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
   
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  

    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);
}


void USART1_IRQHandler(void)
{         
    if(USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
    {
            USART_ReceiveData(USART1);
    }
   
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {   
           USART_ReceiveData(USART1);
           USART_ClearITPendingBit(USART1,USART_IT_RXNE);
    }

    if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
    {   
           USART_SendData(USART1,'A');
           USART_ClearITPendingBit(USART1,USART_IT_TXE);
    }
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。