1.主函数中实现。
#include "stdio.h"
#include "gd32f30x_usart.h"
#include "systick.h"
#include <stdio.h>
#define DBG_USART_BAUD_RATE 115200
#define DBG_USART_DATA_BIN_LEN USART_WL_8BIT //data
#define DBG_USART_PARITY_MODE USART_PM_NONE //parity
#define DBG_USART_STOP_BIT_LEN USART_STB_1BIT //stop
#define DBG_GPIO_RCU_PERIPH RCU_GPIOA
#define DBG_GPIO_PERIPH GPIOA
#define DBG_USART_RCU_PERIPH RCU_USART0
#define DBG_USART USART0
#define DBG_USART_TX_PIN GPIO_PIN_9
#define DBG_USART_RX_PIN GPIO_PIN_10
#define ARRAYNUM(arr) (uint32_t)(sizeof(arr) / sizeof(*(arr)))
#define TRANSMIT_SIZE (ARRAYNUM(txbuffer) - 1)
uint8_t txbuffer[] = "zhongduan test\r\n"; /* TX发送的数据 */
uint8_t tx_size = TRANSMIT_SIZE; /* TX发送数据长度 */
__IO uint8_t txcount = 0; /*发送数据长度,__IO类似volatile,随时可变 */
// uint8_t rxbuffer[10]; /* RX接收数组定长 */
// uint8_t rx_size = 10; /* RX需要接收长度 */
// __IO uint16_t rxcount = 0; /* 实际接收长度 */
/* 串口重定向C语言printf */
int fputc(int ch, FILE *f)
{
while(RESET == usart_flag_get(DBG_USART, USART_FLAG_TBE));
usart_data_transmit(DBG_USART, (uint8_t)ch);
return ch;
}
/* 串口重定向C语言getchar、scanf */
int fgetc(FILE *f)
{
uint8_t ch = 0;
ch = usart_data_receive(USART0);
return ch;
}
/* uart printf init */
void uart_printf_init(void)
{
/* enable GPIO clock */
rcu_periph_clock_enable(DBG_GPIO_RCU_PERIPH);
/* enable USART clock */
rcu_periph_clock_enable(DBG_USART_RCU_PERIPH);
/* connect port to USARTx_Tx */
gpio_init(DBG_GPIO_PERIPH, GPIO_MODE_AF_PP,
GPIO_OSPEED_50MHZ, DBG_USART_TX_PIN);
/* connect port to USARTx_Rx */
gpio_init(DBG_GPIO_PERIPH, GPIO_MODE_IN_FLOATING,
GPIO_OSPEED_50MHZ, DBG_USART_RX_PIN);
/* USART configure */
usart_deinit(DBG_USART);
/* 设置波特率 */
usart_baudrate_set(DBG_USART, DBG_USART_BAUD_RATE);
/* 设置数据位 */
usart_word_length_set(DBG_USART, DBG_USART_DATA_BIN_LEN);
/* 设置奇偶校验位 */
usart_parity_config(DBG_USART, DBG_USART_PARITY_MODE);
/* 设置停止位 */
usart_stop_bit_set(DBG_USART, DBG_USART_STOP_BIT_LEN);
usart_hardware_flow_rts_config(DBG_USART, USART_RTS_DISABLE); /* 禁用rts */
usart_hardware_flow_cts_config(DBG_USART, USART_CTS_DISABLE); /* 无硬件数据流控制 */
/* 使能接收器 */
usart_receive_config(DBG_USART, USART_RECEIVE_ENABLE);
/* 使能发送器 */
usart_transmit_config(DBG_USART, USART_TRANSMIT_ENABLE);
/* 使能串口0 */
usart_enable(DBG_USART);
}
/* 1B,4B,数据输入,1B=8bit */
void uart_data_transmit(uint8_t arr[], uint32_t len)
{
uint32_t i;
for(i=0; i<len; i++)
{
usart_data_transmit(USART0, arr[i]); /* transmit */
while (usart_flag_get(USART0, USART_FLAG_TBE)== RESET); /* 获取缓冲区是否为空 */
}
}
/* 串口0的中断配置函数 */
void USART0_IRQHandler(void)
{
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) /* receive */
{
/* receive data */
// data = usart_data_receive(USART0); /* 接收 */
// usart_data_transmit(USART0, data);
// if(rxcount == rx_size){ /* 接收数据长度为10 */
// // usart_interrupt_disable(USART0, USART_INT_RBNE); /* 关闭接收中断 */
// }
}
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)) /* transmit */
{
usart_data_transmit(USART0, txbuffer[txcount++]); /* 发送一个八位数据 一个字节 */
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE_ORERR);
if(txcount == tx_size) /* 发送完毕 */
{
usart_interrupt_disable(USART0, USART_INT_TBE); /* 关闭发送中断 */
}
}
}
/* void usart_data_transmit(uint32_t usart_periph, uint16_t data) 数据发送函数 */
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
systick_config();
/* 连接串口 */
uart_printf_init();
/*开启USART0中断 */
nvic_irq_enable(USART0_IRQn,0U, 0U);
/* 使能串口发送中断 */
//usart_interrupt_enable(USART0, USART_INT_TBE);
delay_1ms(1000); /*开启后会进入中断进行发送,故需要进行一个延迟*/
/* USART_FLAG_TC发送完成中断 */
while(RESET == usart_flag_get(USART0, USART_FLAG_TC));
/* 使能串口接收中断 */
//usart_interrupt_enable(USART0, USART_INT_RBNE); /* 参数:读数据缓冲区非空中断和过载错误中断 */
/* 等待接收完毕 */
// while(rxcount < rx_size); /* 长度是10 */
// if(rxcount == rx_size)
// printf("\n\rThe receive data is \n");
// uart_data_transmit(rxbuffer,rx_size); /* 发送接收数据 */
// printf("\n\r");
// usart flag_clear(USART0, USART_INT_FLAG_RBNE_ORERR);
/* 打印字符串 */
uint8_t data = 0;
while(1)
{
while(!usart_flag_get(USART0, USART_FLAG_RBNE));
data = usart_data_receive(USART0);
usart_data_transmit(USART0, data);
// uint8_t str[] = "\rHELLO!WORLDmain\r\n ";
// uint32_t len = sizeof(str) / sizeof(*str); /* length */
// uart_data_transmit(str,len-1);
// delay_1ms(1000);
}
// while(1){
// printf("Hello World\r\n");
// //usart_data_transmit(USART0,'a');
// delay_1ms(1000);
// }
}
2.中断实现。
#include "stdio.h"
#include "gd32f30x_usart.h"
#include "systick.h"
#include <stdio.h>
#define DBG_USART_BAUD_RATE 115200
#define DBG_USART_DATA_BIN_LEN USART_WL_8BIT //data
#define DBG_USART_PARITY_MODE USART_PM_NONE //parity
#define DBG_USART_STOP_BIT_LEN USART_STB_1BIT //stop
#define DBG_GPIO_RCU_PERIPH RCU_GPIOA
#define DBG_GPIO_PERIPH GPIOA
#define DBG_USART_RCU_PERIPH RCU_USART0
#define DBG_USART USART0
#define DBG_USART_TX_PIN GPIO_PIN_9
#define DBG_USART_RX_PIN GPIO_PIN_10
#define ARRAYNUM(arr) (uint32_t)(sizeof(arr) / sizeof(*(arr)))
#define TRANSMIT_SIZE (ARRAYNUM(txbuffer) - 1)
uint8_t txbuffer[] = "zhongduan test\r\n"; /* TX发送的数据 */
uint8_t tx_size = TRANSMIT_SIZE; /* TX发送数据长度 */
__IO uint8_t txcount = 0; /*发送数据长度,__IO类似volatile,随时可变 */
uint8_t rxbuffer[10]; /* RX接收数组定长 */
uint8_t rx_size = 10; /* RX需要接收长度 */
__IO uint16_t rxcount = 0; /* 实际接收长度 */
/* 串口重定向C语言printf */
int fputc(int ch, FILE *f)
{
while(RESET == usart_flag_get(DBG_USART, USART_FLAG_TBE));
usart_data_transmit(DBG_USART, (uint8_t)ch);
return ch;
}
/* 串口重定向C语言getchar、scanf */
int fgetc(FILE *f)
{
uint8_t ch = 0;
ch = usart_data_receive(USART0);
return ch;
}
/* uart printf init */
void uart_printf_init(void)
{
/* enable GPIO clock */
rcu_periph_clock_enable(DBG_GPIO_RCU_PERIPH);
/* enable USART clock */
rcu_periph_clock_enable(DBG_USART_RCU_PERIPH);
/* connect port to USARTx_Tx */
gpio_init(DBG_GPIO_PERIPH, GPIO_MODE_AF_PP,
GPIO_OSPEED_50MHZ, DBG_USART_TX_PIN);
/* connect port to USARTx_Rx */
gpio_init(DBG_GPIO_PERIPH, GPIO_MODE_IN_FLOATING,
GPIO_OSPEED_50MHZ, DBG_USART_RX_PIN);
/* USART configure */
usart_deinit(DBG_USART);
/* 设置波特率 */
usart_baudrate_set(DBG_USART, DBG_USART_BAUD_RATE);
/* 设置数据位 */
usart_word_length_set(DBG_USART, DBG_USART_DATA_BIN_LEN);
/* 设置奇偶校验位 */
usart_parity_config(DBG_USART, DBG_USART_PARITY_MODE);
/* 设置停止位 */
usart_stop_bit_set(DBG_USART, DBG_USART_STOP_BIT_LEN);
usart_hardware_flow_rts_config(DBG_USART, USART_RTS_DISABLE); /* 禁用rts */
usart_hardware_flow_cts_config(DBG_USART, USART_CTS_DISABLE); /* 无硬件数据流控制 */
/* 使能接收器 */
usart_receive_config(DBG_USART, USART_RECEIVE_ENABLE);
/* 使能发送器 */
usart_transmit_config(DBG_USART, USART_TRANSMIT_ENABLE);
/* 使能串口0 */
usart_enable(DBG_USART);
}
/* 1B,4B,数据输入,1B=8bit */
void uart_data_transmit(uint8_t arr[], uint32_t len)
{
uint32_t i;
for(i=0; i<len; i++)
{
usart_data_transmit(USART0, arr[i]); /* transmit */
while (usart_flag_get(USART0, USART_FLAG_TBE)== RESET); /* 获取缓冲区是否为空 */
}
}
/* 串口0的中断配置函数 */
void USART0_IRQHandler(void)
{
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) /* receive */
{
/* receive data */
uint8_t data;
data = usart_data_receive(USART0); /* 接收 */
usart_data_transmit(USART0, data);
if(rxcount == rx_size){ /* 接收数据 */
// usart_interrupt_disable(USART0, USART_INT_RBNE); /* 关闭接收中断 */
}
}
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)) /* transmit */
{
usart_data_transmit(USART0, txbuffer[txcount++]); /* 发送一个八位数据 一个字节 */
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE_ORERR);
if(txcount == tx_size) /* 发送完毕 */
{
usart_interrupt_disable(USART0, USART_INT_TBE); /* 关闭发送中断 */
}
}
}
/* void usart_data_transmit(uint32_t usart_periph, uint16_t data) 数据发送函数 */
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
systick_config();
/* 连接串口 */
uart_printf_init();
/*开启USART0中断 */
nvic_irq_enable(USART0_IRQn,0U, 0U);
/* 使能串口发送中断 */
usart_interrupt_enable(USART0, USART_INT_TBE);
delay_1ms(1000); /*开启后会进入中断进行发送,故需要进行一个延迟*/
/* USART_FLAG_TC发送完成中断 */
while(RESET == usart_flag_get(USART0, USART_FLAG_TC));
/* 使能串口接收中断 */
usart_interrupt_enable(USART0, USART_INT_RBNE); /* 参数:读数据缓冲区非空中断和过载错误中断 */
/* 等待接收完毕 */
// while(rxcount < rx_size); /* 长度是10 */
// if(rxcount == rx_size)
// printf("\n\rThe receive data is \n");
// uart_data_transmit(rxbuffer,rx_size); /* 发送接收数据 */
// printf("\n\r");
// usart flag_clear(USART0, USART_INT_FLAG_RBNE_ORERR);
/* 打印字符串 */
// uint8_t data = 0;
// while(1)
// {
// while(!usart_flag_get(USART0, USART_FLAG_RBNE));
// data = usart_data_receive(USART0);
// usart_data_transmit(USART0, data);
// uint8_t str[] = "\rHELLO!WORLDmain\r\n ";
// uint32_t len = sizeof(str) / sizeof(*str); /* length */
// uart_data_transmit(str,len-1);
// delay_1ms(1000);
// }
// while(1){
// printf("Hello World\r\n");
// //usart_data_transmit(USART0,'a');
// delay_1ms(1000);
// }
}