stm32 USART_IT_IDLE中断 一帧数据

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

posted @ 2018-01-22 10:49  thomas_blog  阅读(4399)  评论(0编辑  收藏  举报