打印
[研电赛技术支持]

【GD32F303红枫派使用手册】第十七讲 USART-中断串口收发实验

[复制链接]
1007|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

17.1 实验内容
通过本实验主要学习以下内容:
使用中断进行串口收发
17.2 实验原理
前面章节中我们已经学习了串口的状态标志位,本实验就是使用TBE中断和RBNE中断来实现中断收发数据,实验原理是RBNE中断用来接受数据,IDLE中断用于判断发送方数据结束,TBE中断用于发送数据。
17.3 硬件设计
本实验仍然使用USBUART接口,硬件设计见前面章节。
17.4 代码解析
17.4.1 串口中断发送函数
driver_uart.c中定义了串口中断发送函数:
C
Drv_Err driver_uart_int_transmit(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{
    uint32_t timeout = driver_tick;   
    while(uartx->uart_control.Com_Flag.Bits.SendState==1){
        if((timeout+UART_TIMEOUT_MS) <= driver_tick) {              
            uartx->uart_control.Com_Flag.Bits.SendState=0;
            return DRV_ERROR;        
        }
    }
   
    uartx->uart_control.Com_Flag.Bits.SendSucess=0;
    uartx->uart_control.Com_Flag.Bits.SendState=1;  
    uartx->uart_control.p_Send=pbuff;
    uartx->uart_control.SendSize=length;
    uartx->uart_control.SendCount=0;
   
    usart_flag_clear(uartx->uart_x,USART_FLAG_TC);
    usart_interrupt_enable(uartx->uart_x,USART_INT_TBE);
   
    return DRV_SUCCESS;   
}
17.4.2 串口中断接受函数
driver_uart.c中定义了串口中断接受函数:
C
Drv_Err driver_uart_int_receive(typdef_uart_struct *uartx,uint8_t *pbuff,uint16_t length)
{   
    uint32_t timeout = driver_tick;     
    while(uartx->uart_control.Com_Flag.Bits.RecState==1){
        if((timeout+UART_TIMEOUT_MS) <= driver_tick) {              
            uartx->uart_control.Com_Flag.Bits.RecState=0;
            return DRV_ERROR;        
        }
    }
   
    uartx->uart_control.Com_Flag.Bits.RecSuccess=0;
    uartx->uart_control.Com_Flag.Bits.RecState=1;
    uartx->uart_control.p_Rec=pbuff;
    uartx->uart_control.RecSize=length;
    uartx->uart_control.RecCount=0;   

    usart_flag_clear(uartx->uart_x,USART_FLAG_IDLE);
    USART_STAT0(uartx->uart_x);
    USART_DATA(uartx->uart_x);  
   
    usart_interrupt_enable(uartx->uart_x,USART_INT_RBNE);
    usart_interrupt_enable(uartx->uart_x,USART_INT_IDLE);
  
   
    return DRV_SUCCESS;   
}
17.4.3 main函数实现
以下为main函数代码:
C
int main(void)
{
    delay_init();
    //初始化UART为中断模式,注册接受完成(IDLE)回调函数
    BOARD_UART.uart_mode_tx=MODE_INT;
    BOARD_UART.uart_mode_rx=MODE_INT;
    BOARD_UART.uart_idle_callback=user_receive_complete_callback;      
    bsp_uart_init(&BOARD_UART);
    nvic_irq_enable(USART0_IRQn,2,0);
    delay_ms(1000);
    printf("uart interrupt mode sends and receives loopback packets of indefinite length.\r\n");
    //配置UART接受,最长100byte
    driver_uart_int_receive(&BOARD_UART,uart_rec_buff,100);   
    while (1)
    {
    //查询到接受完成回调函数标志
    if(uart_receive_complete_flag==SET)
    {
        uart_receive_complete_flag=RESET;        
        //发送刚接受到的数据
        driver_uart_int_transmit(&BOARD_UART,uart_rec_buff,uart_receive_count);            
    }
    }
}
本例程main函数首先进行了延时函数初始化,再初始化UART为中断模式,接着配置串口BOARD_UART,开启串口中断NVIC,这里使用到了IDLE中断,TBE中断和RBNE中断,然后配置串口D中断接受,最长100个字节,所以我们可以给串口发送100个字节以下长度的数据。在while1)循环中循环查询uart_receive_complete_flag标志位,当该标志位为“SET”时,表示IDLE中断被触发,一帧数据接受完,最后将接收到的帧数据通过中断发送方式原封不动发送到串口上。
17.4.4 中断函数
本实验中中断函数和DMA串口收发实验用到的中断函数相同。
17.5 实验结果
使用USB-TypeC线,连接电脑和板上USB to UART口后,使用串口调试助手发送一帧数据到MCUMCU会将这帧数据回发到串口调试助手中。
本教程由GD32 MCU方案商聚沃科技原创发布,了解更多GD32 MCU教程,关注聚沃科技官网,GD32MCU技术交流群:859440462

使用特权

评论回复
沙发
huquanz711| | 2024-6-17 21:07 | 只看该作者
之前论坛里是不是还有个紫藤派?

使用特权

评论回复
板凳
FranklinUNK| | 2024-6-20 15:52 | 只看该作者
这个开发板去哪买啊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

81

主题

101

帖子

2

粉丝