gd32f3x0系列串口例程
时间: 2023-12-11 17:33:43 浏览: 34
以下是GD32F3x0系列MCU的串口例程,您可以参考以下代码:
```c
#include "gd32f3x0.h"
#include <stdio.h>
#define USART0 USART0_BASE
void usart0_init(void);
void usart0_send_char(char ch);
void usart0_send_string(char *str);
int main(void)
{
usart0_init();
usart0_send_string("Hello, world!\r\n");
while (1) {
/* Do something */
}
return 0;
}
void usart0_init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2 | GPIO_PIN_3);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2 | GPIO_PIN_3);
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200U);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE);
usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
}
void usart0_send_char(char ch)
{
usart_data_transmit(USART0, (uint8_t)ch);
while (usart_flag_get(USART0, USART_FLAG_TBE) == RESET);
}
void usart0_send_string(char *str)
{
while (*str) {
usart0_send_char(*str++);
}
}
```