GD32F303X Uart配置
void Usart_init(void)
{
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_USART0);
rcu_periph_clock_enable(RCU_AF);
/* configure USART Tx as alternate function push-pull */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* USART configure */
usart_deinit(USART0);
nvic_irq_enable(USART0_IRQn, 0, 0);
usart_baudrate_set(USART0, 115200);
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_interrupt_enable(USART0, USART_INT_RBNE);
usart_interrupt_enable(USART0, USART_INT_ERR);
usart_enable(USART0);
}
void UART_SendSpBuffer(uint32_t usart_periph,uint8_t* pSda,uint8_t len)
{
int i=0;
for(i=0;i<len;i++)
{
while(RESET == usart_flag_get(usart_periph, USART_FLAG_TBE));//等待数据寄存器变空
usart_data_transmit(usart_periph, *pSda);
pSda++;
}
while(RESET == usart_flag_get(usart_periph, USART_FLAG_TC));
}
void USART0_IRQHandler(void )
{
uint8_t value;
if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
{
/* receive data */
value = (uint8_t)usart_data_receive(USART0);
usart_interrupt_flag_clear(USART0,USART_INT_FLAG_ERR_FERR);
}
}
fputc重定义
#if 1
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
/* 重定义fputc函数 */
int fputc(int ch, FILE *f)
{
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
;
usart_data_transmit(USART0, (uint8_t)ch);
return ch;
}
#endif
版权声明:本文为CSDN博主「Bourne76」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cftchaoxiaoshu/article/details/122702216
暂无评论