[APM32F4] 【APM32F402R Micro-EVB】-2-串口通信

[复制链接]
 楼主| 南来之风 发表于 2025-7-27 10:14 | 显示全部楼层 |阅读模式
Geehy Link具有USART TX RX,有两组可选:
4122468858a2f1c06f.png

USART1与USART2可以通过跳线帽选择:
8756468858a6b420dd.png


这里使用USART1,配置初始化参数:
  1.    USART_Config_T USART_ConfigStruct;

  2.     /* USART config */
  3.     USART_ConfigStructInit(&USART_ConfigStruct);
  4.     USART_ConfigStruct.baudRate = 115200;
  5.     USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
  6.     USART_ConfigStruct.mode = USART_MODE_TX;
  7.     USART_ConfigStruct.parity = USART_PARITY_NONE;
  8.     USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  9.     USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
初始化USART1:
  1. void BOARD_COM_Config(BOARD_COM_T com, USART_Config_T* configStruct)
  2. {
  3.     GPIO_Config_T GPIO_ConfigStruct = {0U};

  4.     /* Enable GPIO clock */
  5.     RCM_EnableAPB2PeriphClock(COM_TX_PORT_CLK[com] | COM_RX_PORT_CLK[com]);

  6.     if (com == COM1)
  7.     {
  8.         RCM_EnableAPB2PeriphClock(COM_PORT_CLK[com]);
  9.     }
  10.     else
  11.     {
  12.         RCM_EnableAPB1PeriphClock(COM_PORT_CLK[com]);
  13.     }

  14.     /* Configure USART Tx as alternate function push-pull */
  15.     GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  16.     GPIO_ConfigStruct.pin = COM_TX_PIN[com];
  17.     GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  18.     GPIO_Config(COM_TX_PORT[com], &GPIO_ConfigStruct);

  19.     /* Configure USART Rx as input floating */
  20.     GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
  21.     GPIO_ConfigStruct.pin = COM_RX_PIN[com];
  22.     GPIO_Config(COM_RX_PORT[com], &GPIO_ConfigStruct);

  23.     /* USART configuration */
  24.     USART_Config(COM_PORT[com], configStruct);

  25.     /* Enable USART */
  26.     USART_Enable(COM_PORT[com]);
  27. }


重定向printf:
  1. * [url=home.php?mod=space&uid=247401]@brief[/url]       Redirect C Library function printf to serial port.
  2. *              After Redirection, you can use printf function.
  3. *
  4. * @param       ch:  The characters that need to be send.
  5. *
  6. * @param       *f:  pointer to a FILE that can recording all information
  7. *              needed to control a stream
  8. *
  9. * @retval      The characters that need to be send.
  10. *
  11. * @note
  12. */
  13. int fputc(int ch, FILE* f)
  14. {
  15.     /* send a byte of data to the serial port */
  16.     USART_TxData(DEBUG_USART, (uint8_t)ch);

  17.     /* wait for the data to be send */
  18.     while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

  19.     return (ch);
  20. }

  21. /*!
  22. * [url=home.php?mod=space&uid=247401]@brief[/url]       Redirect C Library function printf to serial port.
  23. *              After Redirection, you can use printf function.
  24. *
  25. * @param       ch:  The characters that need to be send.
  26. *
  27. * @retval      The characters that need to be send.
  28. *
  29. * @note
  30. */
  31. int __io_putchar(int ch)
  32. {
  33.     /* send a byte of data to the serial port */
  34.     USART_TxData(DEBUG_USART, ch);

  35.     /* wait for the data to be send */
  36.     while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

  37.     return ch;
  38. }

  39. /*!
  40. * @brief       Redirect C Library function printf to serial port.
  41. *              After Redirection, you can use printf function.
  42. *
  43. * @param       file:  Meaningless in this function.
  44. *
  45. * @param       *ptr:  Buffer pointer for data to be sent.
  46. *
  47. * @param       len:  Length of data to be sent.
  48. *
  49. * @retval      The characters that need to be send.
  50. *
  51. * @note
  52. */
  53. int _write(int file, char* ptr, int len)
  54. {
  55.     UNUSED(file);
  56.     int i;
  57.     for (i = 0; i < len; i++)
  58.     {
  59.         __io_putchar(*ptr++);
  60.     }

  61.     return len;
  62. }


输出相应调试结果:
  1.   printf("Serial Init Done\r\n");
  2.                 printf("SystemCoreClock = %d Hz", SystemCoreClock);


串口助手:

  1. Serial Init Done
  2. SystemCoreClock = 120000000 Hz
  3. timeElaspsed_ms = 1000
  4. timeElaspsed_ms = 2000
  5. timeElaspsed_ms = 3000
  6. timeElaspsed_ms = 4000
  7. timeElaspsed_ms = 5000
  8. timeElaspsed_ms = 6000
  9. timeElaspsed_ms = 7000
  10. timeElaspsed_ms = 8000
  11. timeElaspsed_ms = 9000
  12. timeElaspsed_ms = 10000
  13. timeElaspsed_ms = 11000
  14. timeElaspsed_ms = 12000
  15. timeElaspsed_ms = 13000
  16. timeElaspsed_ms = 14000


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

本版积分规则

69

主题

290

帖子

2

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