【GD32评测】+ 4、驱动串口输出

[复制链接]
955|2
 楼主| tlled 发表于 2020-5-9 16:54 | 显示全部楼层 |阅读模式
    使用arduino扩展板来驱动串口输出。

   一、硬件电路

    1.1、GD32E231硬件电路部分
    使用GD32E231的PA9和PA10的UART0来输出数据。
    002.png
    1.2、扩展板部分电路
    使用USB0-TTL电路将数据送到PC
    003.png
    这里使用了串口扩展电路
    004.png

    二、程序部分

    2.1、uart.c

  1. #include "config.h"


  2. void uart_gpio_init(void)
  3. {
  4.     /* enable COM GPIO clock */
  5.     rcu_periph_clock_enable(RCU_GPIOA);

  6.     /* connect port to USARTx_Tx */
  7.     gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);

  8.     /* connect port to USARTx_Rx */
  9.     gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);

  10.     /* configure USART Tx as alternate function push-pull */
  11.     gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
  12.     gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);

  13.     /* configure USART Rx as alternate function push-pull */
  14.     gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
  15.     gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
  16. }

  17. void UART_Init(void)
  18. {
  19.                 uart_gpio_init();
  20.     /* enable USART clock */
  21.     rcu_periph_clock_enable(RCU_USART0);

  22.     /* USART configure */
  23.     usart_deinit(USART0);
  24.     usart_word_length_set(USART0, USART_WL_8BIT);
  25.     usart_stop_bit_set(USART0, USART_STB_1BIT);
  26.     usart_parity_config(USART0, USART_PM_NONE);
  27.     usart_baudrate_set(USART0, 115200U);
  28.     usart_receive_config(USART0, USART_RECEIVE_ENABLE);
  29.     usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);

  30.     usart_enable(USART0);
  31. }

  32. /* retarget the C library printf function to the USART */
  33. int fputc(int ch, FILE *f)
  34. {
  35.     usart_data_transmit(USART0, (uint8_t) ch);
  36.     while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
  37.     return ch;
  38. }

  39. void senddat(uint8_t dat)
  40. {
  41.         usart_data_transmit(USART0,  dat);
  42.     while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
  43. }



  44. void uart_chl(uint8_t chl)
  45. {
  46.         switch (chl)
  47.   {
  48.           case        0:  //RS485
  49.                         uart_chb_l();
  50.                         uart_cha_l();
  51.                   break;
  52.           case        1:        //UART_TTL_CH
  53.                         uart_chb_l();
  54.                         uart_cha_h();
  55.                   break;
  56.                 case        2:        //UART_EXT_CH
  57.                         uart_chb_h();
  58.                         uart_cha_l();
  59.                   break;
  60.           default:
  61.                   break;
  62.   }
  63. }
       2.2、uart.h
  1. #ifndef __UART_H
  2. #define __UART_H

  3. #define   RS485_CH                        0
  4. #define   USB_TTL_CH                1
  5. #define   UART_EXT_CH                2


  6. void UART_Init(void);
  7. int fputc(int ch, FILE *f);
  8. void senddat(uint8_t dat);

  9. void uart_chl(uint8_t chl);


  10. #endif

    2.3、main.c


  1. #include "config.h"


  2. int main(void)
  3. {
  4.         systick_config();
  5.         LED_Init();
  6.         BUS_Init();
  7.         UART_Init();
  8. //KEY_Init();
  9. //Init_lcd();

  10.         uart_chl(USB_TTL_CH);
  11.        
  12.         while(1)
  13.         {
  14.                 led_test();

  15.                 delay_1ms(100);
  16.                 printf("www.21ic.com-GD32E231C-START\r\n");
  17.         }
  18. }

    四、测试结果



    串口间隔100ms输出一串数据。
    01.png
coshi 发表于 2020-6-1 18:08 | 显示全部楼层
非常感谢楼主分享
zljiu 发表于 2020-6-1 18:08 | 显示全部楼层
不错的分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部