17.1实验内容
通过本实验主要学习以下内容:
使用中断进行串口收发
17.2实验原理
前面章节中我们已经学习了串口的状态标志位,本实验就是使用TBE中断和RBNE中断来实现中断收发数据,实验原理是RBNE中断用来接受数据,IDLE中断用于判断发送方数据结束,TBE中断用于发送数据。
17.3硬件设计
本实验仍然使用USB转UART接口,硬件设计见前面章节。
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.3main函数实现
以下为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.rn');
//配置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个字节以下长度的数据。在while(1)循环中循环查询uart_receive_complete_flag标志位,当该标志位为“SET”时,表示IDLE中断被触发,一帧数据接受完,最后将接收到的帧数据通过中断发送方式原封不动发送到串口上。
17.4.4中断函数
本实验中中断函数和DMA串口收发实验用到的中断函数相同。
17.5实验结果
使用USB-TypeC线,连接电脑和板上USB to UART口后,使用串口调试助手发送一帧数据到MCU,MCU会将这帧数据回发到串口调试助手中。


上一篇:【GD32F303红枫派开发板使用手册】第十五讲 USART-printf打印实验
下一篇:【GD32F470紫藤派开发板使用手册】第七讲 FWDG-看门狗实验
推荐阅读最新更新时间:2026-03-25 10:48
- 用于 7VIN 至 16VIN、1.5V 和 1.2V 输出的 LTM4628EV DC/DC 模块稳压器的典型应用电路
- 使用 Analog Devices 的 LTC3728LIGN 的参考设计
- DER-406 - 适用于 A19 灯的 5.76 W 高 PF 非隔离降压-升压型 TRIAC 调光 LED 驱动器
- ADR5045B 5V 输出精密微功率并联模式电压基准的典型应用
- LT3970EDDB-3.42 2.5V 降压转换器的典型应用
- MC78M08BDTG 8V 电流调节器的典型应用
- LT1021DCN8-5 精密电压基准的典型应用
- DER-282 - 100W, 扁平(11 mm), LLC DC-DC转换器
- REF193 低压差开尔文连接电压基准的典型应用电路
- LT3088EM 线性稳压器用于添加软启动的典型应用

嵌入式开发中常用总线知识点总结
GD32F4xx+RTC+Alarm 实现秒中断,通过串口打印时间
XCOM串口小助手
现代雷达系统的信号设计
BFR340T






京公网安备 11010802033920号