gd32f407 串口3
时间: 2025-02-04 20:13:19 浏览: 49
### 配置与使用GD32F407中的UART3
#### 初始化库和头文件引入
为了配置和使用GD32F407上的UART3接口,首先需要包含必要的头文件,并初始化相应的外设。通常情况下,在项目源码中会看到如下形式的代码:
```c
#include "gd32f4xx.h"
#include "systick.h"
void uart3_configuration(void);
```
此部分确保了后续能够调用特定于该系列MCU的功能函数。
#### GPIO端口设置
UART通信依赖于GPIO引脚作为发送(TX)和接收(RX),因此必须先设定这些引脚的工作模式。对于GD32F407而言,UART3默认连接至PB10 (TXD) 和 PB11 (RXD):
```c
rcu_periph_clock_enable(RCU_GPIOB); /* Enable clock for GPIOB */
rcu_periph_clock_enable(RCU_UART3); /* Enable clock for UART3 */
/* Configure TX pin as alternate function push-pull */
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_10);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* Configure RX pin as input floating */
gpio_af_select(GPIOB, GPIO_AF_7, GPIO_PIN_10 | GPIO_PIN_11);
gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_11);
```
上述代码片段展示了如何启用相关时钟资源以及针对UART3所需的具体GPIO配置[^2]。
#### UART参数配置
接下来是对UART本身进行详细的参数定义,比如波特率、数据位长度等:
```c
struct gd_uart_config {
uint32_t baud_rate;
uint8_t data_bits;
uint8_t stop_bits;
uint8_t parity;
};
static void uart3_configuration(struct gd_uart_config *config){
uart_deinit(UART3);
uart_word_length_set(UART3,UART_WL_8BIT);
uart_stop_bit_set(UART3,UART_STB_1BIT);
uart_parity_none(UART3);
uart_baudrate_set(UART3,SystemCoreClock / config->baud_rate);
}
```
这里通过结构体传递具体的配置选项给`uart3_configuration()`函数来完成进一步定制化需求[^4].
#### 中断使能及回调处理程序安装
当涉及到异步收发操作时,中断机制变得尤为重要。下面的例子说明了怎样开启UART3接收中断并向NVIC注册对应的ISR服务例程:
```c
nvic_irq_enable(UART3_IRQn, 0, 0);
uart_interrupt_enable(UART3, UART_INT_RBNE);
void UART3_IRQHandler(void){
if(RESET != uart_flag_get(UART3,UART_FLAG_RBNE)){
char ch = uart_data_receive(UART3);
// Process received character 'ch'
}
}
```
这段代码实现了基本的数据接收流程监控,并提供了简单的字符读取逻辑.
阅读全文
相关推荐


















