| 
 
| 最近在调试GD32F303RET6时,遇到了个问题,就是USART0在发送的时候,TBE一直为1,但TC有变化,查看了memery地址40013804的4个字节都是0,也就是USART0的data寄存器没有数据。 
 后来使用GD32官方例程单独调试USART0,也没辙,查看了芯片数据手册:
 
 
   
 发现本来USART0应该默认映射在PA9和PA10:
 
 
   
 如果要使用PB6和PB7,需要重映射 ,可在串口初始化时,添加下面这句:
 
 gpio_pin_remap_config(GPIO_USART0_REMAP, ENABLE);
 
 
 以下附上完整串口配置全过程,使用轮询发送+中断接收的方式
 
 串口初始化:
 
 void usart0_init(void)
 {
 rcu_periph_clock_enable(RCU_AF);
 
 rcu_periph_clock_enable(RCU_GPIOB);
 rcu_periph_clock_enable(RCU_USART0);
 gpio_pin_remap_config(GPIO_USART0_REMAP, ENABLE);
 /* initialize USART */
 nvic_irq_enable(USART0_IRQn, 0, 0);
 /* connect port to USARTx_Tx */
 gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_6);
 /* connect port to USARTx_Rx */
 gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_10MHZ, GPIO_PIN_7);
 
 /* USART configure */
 usart_deinit(USART0);
 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(usart_periph, USART_INT_RBNE);
 }
 
 
 
 串口发送函数
 
 void u0send(uint8_t *pByte, uint32_t len)
 {
 //如果有485收发切换IO口,可添加在函数首尾
 for (int i = 0; i < len; i++)
 {
 usart_data_transmit(USART0, *(pByte + i));
 while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
 ;
 }
 }
 
 
 
 中断接收函数 :
 
 
 uint8_t rxlen = 0;
 uint8_t rxbuf[64] = {0};
 void USART0_IRQHandler(void)
 {
 uint8_t ch;
 if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
 {
 /* read one byte from the receive data register */
 ch = (uint8_t)usart_data_receive(USART0);
 if (rxlen >= UART0_RX_SIZE)
 {
 rxlen = 0;
 }
 rxbuf[rxlen] = ch;
 rxlen++;
 usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
 }
 
 if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_TC))
 {
 /* write one byte to the transmit data register */
 usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TC);
 }
 }
 
 
 主函数:
 
 int main(void)
 {
 uint8_t data[2] = {0x55,0xAA};
 usart0_init();
 
 while(1)
 {
 u0send(&data[0], 2);
 //添加延时函数 delay_ms(100);
 }
 //记得处理接收函数时,清除接收长度rxlen = 0;
 
 }
 
 
 
 ————————————————
 
 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
 
 原文链接:https://blog.csdn.net/qq_40990392/article/details/148189091
 
 
 | 
 |