[新品上市] 【APM32F107VCT6 MINI开发板测评】串口中断

[复制链接]
1473|12
 楼主| 比神乐 发表于 2023-2-17 18:52 | 显示全部楼层 |阅读模式
板子上有个232,我正好有个USB转232,就搞个232通讯程序吧。我用的串口1

原理图:
2.jpg

主程序代码:
  1. #include "main.h"
  2. #include "stdio.h"
  3. /** @addtogroup Examples
  4.   @{
  5. */

  6. /** @addtogroup USART_Interrupt
  7.   @{
  8. */

  9. /** @addtogroup USART_Interrupt_MACROS MACROS
  10.   @{
  11. */

  12. #define DEBUG_USART USART2
  13. /**@} end of group USART_Interrupt_MACROS */

  14. /** @addtogroup USART_Interrupt_Variables Variables
  15.   @{
  16. */

  17. uint8_t txBuf[] = "Hello USART2 \r\n";
  18. uint8_t count = 0;
  19. /**@} end of group USART_Interrupt_Variables */

  20. /** @addtogroup USART_Interrupt_Functions Functions
  21.   @{
  22. */

  23. /*!
  24. * [url=home.php?mod=space&uid=247401]@brief[/url]       Main program
  25. *
  26. * @param       None
  27. *
  28. * @retval      None
  29. *
  30. */
  31. int main(void)
  32. {

  33.     USART_Config_T USART_ConfigStruct;

  34.     APM_MINI_LEDInit(LED2);

  35.     RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1));
  36.     RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_USART2);

  37.     USART_ConfigStruct.baudRate = 115200;
  38.     USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
  39.     USART_ConfigStruct.mode = USART_MODE_TX_RX;
  40.     USART_ConfigStruct.parity = USART_PARITY_NONE;
  41.     USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  42.     USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
  43.     APM_MINI_COMInit(COM1, &USART_ConfigStruct);
  44.     //APM_MINI_COMInit(COM2, &USART_ConfigStruct);

  45.     USART_EnableInterrupt(USART1, USART_INT_RXBNE);
  46.     //USART_EnableInterrupt(USART2, USART_INT_RXBNE);

  47.     NVIC_EnableIRQRequest(USART1_IRQn, 2, 0);
  48.     //NVIC_EnableIRQRequest(USART2_IRQn, 1, 0);

  49.     while (1)
  50.     {
  51.     }
  52. }

  53. /*!
  54. * @brief       Delay
  55. *
  56. * @param       None
  57. *
  58. * @retval      None
  59. *
  60. */
  61. void Delay(void)
  62. {
  63.     uint32_t tick = 0xfffff;

  64.     while (tick--);
  65. }

  66. /*!
  67. * @brief       USART1_Interrupt
  68. *
  69. * @param       None
  70. *
  71. * @retval      None
  72. *
  73. */
  74. void USART1_Isr(void)
  75. {
  76. //    if(USART_ReadIntFlag(USART1, USART_INT_TXBE))
  77. //    {
  78. //        USART_TxData(USART1, txBuf[count]);
  79. //        count++;
  80. //        if (count == sizeof(txBuf))
  81. //        {
  82. //            count = 0;
  83. //            Delay();
  84. //            APM_MINI_LEDToggle(LED2);
  85. //        }
  86. //    }
  87.                 uint8_t dat;
  88.     if(USART_ReadIntFlag(USART1, USART_INT_RXBNE))
  89.     {
  90.         dat = (uint8_t)USART_RxData(USART1);
  91.         USART_TxData(USART1, dat);
  92.     }
  93. }

  94. /*!
  95. * @brief       USART2_Interrupt
  96. *
  97. * @param       None
  98. *
  99. * @retval      None
  100. *
  101. */
  102. void USART2_Isr(void)
  103. {
  104.     uint8_t dat;
  105.     if(USART_ReadIntFlag(USART2, USART_INT_RXBNE))
  106.     {
  107.         dat = (uint8_t)USART_RxData(USART2);
  108.         printf("%c", dat);
  109.     }
  110. }

  111. #if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

  112. /*!
  113. * @brief       Redirect C Library function printf to serial port.
  114. *              After Redirection, you can use printf function.
  115. *
  116. * @param       ch:  The characters that need to be send.
  117. *
  118. * @param       *f:  pointer to a FILE that can recording all information
  119. *              needed to control a stream
  120. *
  121. * @retval      The characters that need to be send.
  122. *
  123. * @note
  124. */
  125. int fputc(int ch, FILE *f)
  126. {
  127.     /* send a byte of data to the serial port */
  128.     USART_TxData(DEBUG_USART, (uint8_t)ch);

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

  131.     return (ch);
  132. }

  133. #elif defined (__GNUC__)

  134. /*!
  135. * @brief       Redirect C Library function printf to serial port.
  136. *              After Redirection, you can use printf function.
  137. *
  138. * @param       ch:  The characters that need to be send.
  139. *
  140. * @retval      The characters that need to be send.
  141. *
  142. * @note
  143. */
  144. int __io_putchar(int ch)
  145. {
  146.     /* send a byte of data to the serial port */
  147.     USART_TxData(DEBUG_USART, ch);

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

  150.     return ch;
  151. }

  152. /*!
  153. * @brief       Redirect C Library function printf to serial port.
  154. *              After Redirection, you can use printf function.
  155. *
  156. * @param       file:  Meaningless in this function.
  157. *
  158. * @param       *ptr:  Buffer pointer for data to be sent.
  159. *
  160. * @param       len:  Length of data to be sent.
  161. *
  162. * @retval      The characters that need to be send.
  163. *
  164. * @note
  165. */
  166. int _write(int file, char *ptr, int len)
  167. {
  168.     int i;
  169.     for (i = 0; i < len; i++)
  170.     {
  171.         __io_putchar(*ptr++);
  172.     }

  173.     return len;
  174. }

  175. #else
  176. #warning Not supported compiler type
  177. #endif

  178. /**@} end of group USART_Interrupt_Functions */
  179. /**@} end of group USART_Interrupt */
  180. /**@} end of group Examples */
初始化代码:
  1. #define MINI_COM1                        USART1
  2. #define MINI_COM1_CLK                    RCM_APB2_PERIPH_USART1
  3. #define MINI_COM1_TX_PIN                 GPIO_PIN_9
  4. #define MINI_COM1_TX_GPIO_PORT           GPIOA
  5. #define MINI_COM1_TX_GPIO_CLK            RCM_APB2_PERIPH_GPIOA
  6. #define MINI_COM1_RX_PIN                 GPIO_PIN_10
  7. #define MINI_COM1_RX_GPIO_PORT           GPIOA
  8. #define MINI_COM1_RX_GPIO_CLK            RCM_APB2_PERIPH_GPIOA
  9. #define MINI_COM1_IRQn                   USART1_IRQn


  10. /*!
  11. * @brief       Configures COM port.
  12. *
  13. * @param       COM: Specifies the COM port to be configured.
  14. *              This parameter can be one of following parameters:
  15. *              [url=home.php?mod=space&uid=2817080]@ARG[/url] COM1
  16. *              @arg COM2
  17. *
  18. * @retval      None
  19. */
  20. void APM_MINI_COMInit(COM_TypeDef COM, USART_Config_T* configStruct)
  21. {
  22.     GPIO_Config_T GPIO_configStruct;

  23.     /* Enable GPIO clock */
  24.     RCM_EnableAPB2PeriphClock(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM]);

  25.     if (COM == COM1)
  26.     {
  27.         RCM_EnableAPB2PeriphClock(COM_USART_CLK[COM]);
  28.     }
  29.     else
  30.     {
  31.         RCM_EnableAPB1PeriphClock(COM_USART_CLK[COM]);
  32.     }

  33.     /* Configure USART Tx as alternate function push-pull */
  34.     GPIO_configStruct.mode = GPIO_MODE_AF_PP;
  35.     GPIO_configStruct.pin = COM_TX_PIN[COM];
  36.     GPIO_configStruct.speed = GPIO_SPEED_50MHz;
  37.     GPIO_Config(COM_TX_PORT[COM], &GPIO_configStruct);

  38.     /* Configure USART Rx as input floating */
  39.     GPIO_configStruct.mode = GPIO_MODE_IN_FLOATING;
  40.     GPIO_configStruct.pin = COM_RX_PIN[COM];
  41.     GPIO_Config(COM_RX_PORT[COM], &GPIO_configStruct);

  42.     /* USART configuration */
  43.     USART_Config(COM_USART[COM], configStruct);

  44.     /* Enable USART */
  45.     USART_Enable(COM_USART[COM]);
  46. }
中断函数:
  1. /*!
  2. * @brief   This function handles USART1 Handler
  3. *
  4. * @param   None
  5. *
  6. * @retval  None
  7. *
  8. */
  9. void USART1_IRQHandler(void)
  10. {
  11.     USART1_Isr();
  12. }
效果图:
0.jpg
1.jpg
pl202 发表于 2023-3-2 12:05 | 显示全部楼层
APM32F107VCT6 MINI开发板看着不错。
jkl21 发表于 2023-3-7 19:35 | 显示全部楼层
APM32F107VCT6 MINI开发板还以申请吗?
macpherson 发表于 2023-3-7 21:44 | 显示全部楼层
APM32F107VC 可以通过串口下载代码吗?
 楼主| 比神乐 发表于 2023-3-8 09:31 | 显示全部楼层
macpherson 发表于 2023-3-7 21:44
APM32F107VC 可以通过串口下载代码吗?

没试过。
linfelix 发表于 2023-3-9 12:46 | 显示全部楼层
APM32F107VCT6 支持多少个串口的?
 楼主| 比神乐 发表于 2023-3-10 09:05 | 显示全部楼层
linfelix 发表于 2023-3-9 12:46
APM32F107VCT6 支持多少个串口的?

没仔细看,应该比较多。
jackcat 发表于 2023-3-10 10:07 | 显示全部楼层
串口接收中断怎么触发?               
kmzuaz 发表于 2023-3-10 10:26 | 显示全部楼层
最大的波特率可以设置为多少?              
saservice 发表于 2023-3-10 10:54 | 显示全部楼层
可以使用dma接收数据吗?
              
 楼主| 比神乐 发表于 2023-3-11 09:03 | 显示全部楼层
kmzuaz 发表于 2023-3-10 10:26
最大的波特率可以设置为多少?

不知道
 楼主| 比神乐 发表于 2023-3-11 09:04 | 显示全部楼层
saservice 发表于 2023-3-10 10:54
可以使用dma接收数据吗?

应该可以的
forgot 发表于 2023-6-28 17:07 | 显示全部楼层
感谢楼主的分享,很全面,学习一下,期待更多好的内容
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

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