1. 程式人生 > >stm32 USART_IT_IDLE中斷 一幀資料

stm32 USART_IT_IDLE中斷 一幀資料

USART_IT_IDLE和USART_IT_RXNE區別
當接收到1個位元組,會產生USART_IT_RXNE中斷
當接收到一幀資料,就會產生USART_IT_IDLE中斷

清中斷方法

//USART_IT_RXNE
USART_ClearITPendingBit(USART1, USART_IT_RXNE);

//USART_IT_IDLE
USART1->SR; //先讀SR暫存器
USART1->DR; //再讀DR暫存器

使用舉例

u8 count;
u8 flag;

void uart_init(u32 bound)
{
    ...

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);

    ...
}

void USART1_IRQHandler(void)
{
    u8 r;

    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
    {
        USART_ClearITPendingBit(USART1, USART_IT_RXNE); //只USART_ReceiveData也可以

        //r = USART_ReceiveData(USART1);

        count++;
    }
    else if(USART_GetFlagStatus(USART1, USART_FLAG_IDLE) != RESET)
    {
        USART1->SR;
        USART1->DR;

        flag = 1;
    }
}

int main()
{
    while(1)
    {
        if(flag == 1)
        {
            printf("count %d\r\n", count);

            flag = 0;
            count = 0;
        }

        delay_ms(10);
    }
}

實驗現象
1