CH32V203串口中断只进入一次

注意到了需要增加void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

但还是只进入一次,请问这和编译优化相关有关系吗?或是哪里的问题?

串口源码如下:


void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

void USARTx_Init(u32 baudRate)

{

    GPIO_InitTypeDef  GPIO_InitStructure = {0};

    USART_InitTypeDef USART_InitStructure = {0};

    NVIC_InitTypeDef  NVIC_InitStructure = {0};


    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);


    // USART2 RX-->A.3

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOA, &GPIO_InitStructure);


    USART_InitStructure.USART_BaudRate = baudRate;

    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_Init(USART2, &USART_InitStructure);

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);


    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);


    USART_Cmd(USART2, ENABLE);

}


void USART2_IRQHandler(void)

{

    if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)

    {

        //......


        USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);

    }

}


您好,根据代码,你在中断函数中将中断关闭了。注意中断函数中要注意清除对应的中断标志位,此外,若中断函数中有关中断的操作,再次使用时要开启。

image.png


感谢!确认了是开中断有问题,改用请标志位验证,就一直有数据了


只有登录才能回复,可以选择微信账号登录