今晚搞了一下串口。
原理图:
代码:
#include "gd32e23x.h"
#include "gd32e231c_start.h"
#include "systick.h"
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
void led_flash(int times);
//-----------------------------------------------------------------------------------------
//????:Delay()
//? ?:?????
//-----------------------------------------------------------------------------------------
void Delay(unsigned char us) //5,7,9
{
while(--us);
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* initialize KEY and LED, configure SysTick */
gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
systick_config();
gd_eval_led_init(LED1);
/* flash the LED for test */
led_flash(1);
delay_1ms(500);
gd_eval_led_on(LED1);
rcu_periph_clock_enable(RCU_GPIOA);
/* configure led GPIO port */
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_9);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
rcu_periph_clock_enable(RCU_USART0);
usart_deinit(USART0);
usart_baudrate_set(USART0,115200);
usart_parity_config(USART0,USART_PM_NONE);
usart_word_length_set(USART0,USART_WL_8BIT);
usart_stop_bit_set(USART0,USART_STB_1BIT);
usart_transmit_config(USART0,USART_TRANSMIT_ENABLE);
usart_data_first_config(USART0,USART_MSBF_LSB);
usart_enable(USART0);
while(1)
{
usart_data_transmit(USART0,0xaa);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
delay_1ms(500);
}
}
/*!
\brief flash the LED for test
\param[in] times: times to flash the LEDs
\param[out] none
\retval none
*/
void led_flash(int times)
{
int i;
for (i = 0;i < times;i++){
/* delay 500 ms */
delay_1ms(500);
/* toggle the LEDs */
gd_eval_led_toggle(LED1);
/* delay 500 ms */
delay_1ms(500);
/* toggle the LEDs */
gd_eval_led_toggle(LED1);
}
}
效果图:
工程:
UART0.rar
(609.44 KB)
|