鱼C论坛

 找回密码
 立即注册
查看: 1340|回复: 1

[学习笔记] keil工具编写GD32单片机代码

[复制链接]
发表于 2024-1-28 13:52:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
需求:我想在keil中编写GD32f103RG单片机代码。单片机是64PIN的芯片。我想要配置出芯片上USART0、USART1、USART2、UART3、UART4,这五个串口的收发数据的代码。要求上位机发送什么数据,对应串口就回传什么数据。@FishC ,能用C语言写出这个代码吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-1-28 13:57:56 | 显示全部楼层
当然可以使用C语言编写这个代码。以下是一个示例代码,实现了配置GD32f103RG单片机上的五个串口(USART0、USART1、USART2、UART3、UART4)并实现收发数据的功能。

  1. #include "gd32f10x.h"

  2. /* 配置串口参数 */
  3. void uart_config(uart_periph_enum uart_periph, uint32_t baudrate)
  4. {
  5.     /* 初始化GPIO */
  6.     rcu_periph_clock_enable(RCU_GPIOA);
  7.    
  8.     gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
  9.     gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);

  10.     /* 初始化串口 */
  11.     rcu_periph_clock_enable(RCU_USART0 + uart_periph);
  12.    
  13.     usart_deinit(uart_periph);

  14.     usart_baudrate_set(uart_periph, baudrate);
  15.     usart_word_length_set(uart_periph, USART_WL_8BIT);
  16.     usart_stop_bit_set(uart_periph, USART_STB_1BIT);
  17.     usart_parity_config(uart_periph, USART_PM_NONE);
  18.     usart_hardware_flow_rts_config(uart_periph, USART_RTS_DISABLE);
  19.     usart_hardware_flow_cts_config(uart_periph, USART_CTS_DISABLE);
  20.     usart_receive_config(uart_periph, USART_RECEIVE_ENABLE);
  21.     usart_transmit_config(uart_periph, USART_TRANSMIT_ENABLE);

  22.     usart_enable(uart_periph);
  23. }

  24. /* 发送数据 */
  25. void uart_send_data(uart_periph_enum uart_periph, uint16_t data)
  26. {
  27.     usart_data_transmit(uart_periph, data);
  28.     while(usart_flag_get(uart_periph, USART_FLAG_TBE) == RESET);
  29. }

  30. /* 接收数据 */
  31. uint16_t uart_receive_data(uart_periph_enum uart_periph)
  32. {
  33.     while(usart_flag_get(uart_periph, USART_FLAG_RBNE) == RESET);
  34.     return (uint16_t)usart_data_receive(uart_periph);
  35. }

  36. int main(void)
  37. {
  38.     /* 配置串口参数 */
  39.     uart_config(USART0, 9600);
  40.     uart_config(USART1, 9600);
  41.     uart_config(USART2, 9600);
  42.     uart_config(UART3, 9600);
  43.     uart_config(UART4, 9600);

  44.     while (1)
  45.     {
  46.         /* 接收上位机发送的数据,并回传对应串口 */
  47.         uint16_t data = uart_receive_data(USART0);
  48.         uart_send_data(USART0, data);
  49.         
  50.         data = uart_receive_data(USART1);
  51.         uart_send_data(USART1, data);
  52.         
  53.         data = uart_receive_data(USART2);
  54.         uart_send_data(USART2, data);
  55.         
  56.         data = uart_receive_data(UART3);
  57.         uart_send_data(UART3, data);
  58.         
  59.         data = uart_receive_data(UART4);
  60.         uart_send_data(UART4, data);
  61.     }
  62. }
复制代码


该示例代码通过使用GD32库函数,实现了串口的初始化配置和数据的收发。在`uart_config`函数中,我们先初始化相应的GPIO引脚,然后再根据需要配置相应的串口参数。`uart_send_data`函数用于发送数据,`uart_receive_data`函数用于接收数据。

你可以根据需要修改波特率等参数,并根据实际情况在主函数中进行数据的处理。

希望以上代码对你有帮助!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 01:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表