[活动专区] 【AT-START-F407测评】+ 串口收发

[复制链接]
497|0
 楼主| 比神乐 发表于 2021-1-29 12:56 | 显示全部楼层 |阅读模式
今天搞了一下串口
串口2
代码:
  1. #include "at32f4xx.h"
  2. #include "at32_board.h"

  3. /** @addtogroup AT32F407_StdPeriph_Examples
  4.   * @{
  5.   */

  6. /** @addtogroup USART_Interrupt
  7.   * @{
  8.   */
  9.   
  10. /* Private typedef -----------------------------------------------------------*/
  11. typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;

  12. /* Private define ------------------------------------------------------------*/
  13. #define TxBufferSize1   (countof(TxBuffer1) - 1)
  14. #define TxBufferSize2   (countof(TxBuffer2) - 1)
  15. #define RxBufferSize1   TxBufferSize2
  16. #define RxBufferSize2   TxBufferSize1

  17. /* Private macro -------------------------------------------------------------*/
  18. #define countof(a)   (sizeof(a) / sizeof(*(a)))

  19. /* Private variables ---------------------------------------------------------*/
  20. USART_InitType USART_InitStructure;
  21. uint8_t TxBuffer1[] = "USART Interrupt Example: USART2 -> USART3 using Interrupt";
  22. uint8_t TxBuffer2[] = "USART Interrupt Example: USART3 -> USART2 using Interrupt";
  23. uint8_t RxBuffer1[RxBufferSize1];
  24. uint8_t RxBuffer2[RxBufferSize2];
  25. __IO uint8_t TxCounter1 = 0x00;
  26. __IO uint8_t TxCounter2 = 0x00;
  27. __IO uint8_t RxCounter1 = 0x00;
  28. __IO uint8_t RxCounter2 = 0x00;
  29. uint8_t NbrOfDataToTransfer1 = TxBufferSize1;
  30. uint8_t NbrOfDataToTransfer2 = TxBufferSize2;
  31. uint8_t NbrOfDataToRead1 = RxBufferSize1;
  32. uint8_t NbrOfDataToRead2 = RxBufferSize2;
  33. __IO TestStatus TransferStatus1 = FAILED;
  34. __IO TestStatus TransferStatus2 = FAILED;

  35. /* Private function prototypes -----------------------------------------------*/
  36. void RCC_Configuration(void);
  37. void GPIO_Configuration(void);
  38. void NVIC_Configuration(void);
  39. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

  40. /* Private functions ---------------------------------------------------------*/

  41. /**
  42.   * [url=home.php?mod=space&uid=247401]@brief[/url]   Main program
  43.   * @param  None
  44.   * @retval None
  45.   */
  46. int main(void)
  47. {  
  48.   /* System Clocks Configuration */
  49.   RCC_Configuration();
  50.       
  51.   /* NVIC configuration */
  52.   NVIC_Configuration();

  53.   /* Configure the GPIO ports */
  54.   GPIO_Configuration();

  55.   /* Initialize Leds mounted on board */
  56.   AT32_Board_Init();
  57.   
  58.   /* USART2 and USART3 configuration ------------------------------------------------------*/
  59.   /* USART2 and USART3 configured as follow:
  60.         - BaudRate = 9600 baud  
  61.         - Word Length = 8 Bits
  62.         - One Stop Bit
  63.         - No parity
  64.         - Hardware flow control disabled (RTS and CTS signals)
  65.         - Receive and transmit enabled
  66.   */
  67.   USART_StructInit(&USART_InitStructure);
  68.   USART_InitStructure.USART_BaudRate = 9600;
  69.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  70.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  71.   USART_InitStructure.USART_Parity = USART_Parity_No;
  72.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  73.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  74.   /* Configure USART2 */
  75.   USART_Init(USART2, &USART_InitStructure);
  76.   
  77.   
  78.   /* Enable USART2 Receive and Transmit interrupts */
  79.   USART_INTConfig(USART2, USART_INT_RDNE, ENABLE);
  80.   //USART_INTConfig(USART2, USART_INT_TDE, ENABLE);

  81.   
  82.   /* Enable the USART2 */
  83.   USART_Cmd(USART2, ENABLE);
  84.   

  85.   while(1)
  86.         {
  87.         }
  88. }

  89. /**
  90.   * @brief  Configures the different system clocks.
  91.   * @param  None
  92.   * @retval None
  93.   */
  94. void RCC_Configuration(void)
  95. {   
  96.   /* Enable GPIO clock */
  97.   RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOD | RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_AFIO, ENABLE);

  98.   /* Enable USART2 Clock */
  99.   RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART2, ENABLE);

  100.   /* Enable USART3 Clock */
  101.   //RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART3, ENABLE);  
  102. }

  103. /**
  104.   * @brief  Configures the different GPIO ports.
  105.   * @param  None
  106.   * @retval None
  107.   */
  108. void GPIO_Configuration(void)
  109. {
  110.   GPIO_InitType GPIO_InitStructure;

  111.   /* Enable the USART3 Pins Software Remapping */
  112.   //GPIO_PinsRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
  113.   
  114.   /* Enable the USART2 Pins Software Remapping */
  115.   //GPIO_PinsRemapConfig(GPIO_Remap_USART2, ENABLE);  


  116.   /* Configure USART2 Rx as input floating */
  117.   GPIO_InitStructure.GPIO_Pins = GPIO_Pins_3;
  118.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  119.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  120.   
  121.   /* Configure USART3 Rx as input floating */
  122. //  GPIO_InitStructure.GPIO_Pins = GPIO_Pins_11;
  123. //  GPIO_Init(GPIOC, &GPIO_InitStructure);  
  124.   
  125.   /* Configure USART2 Tx as alternate function push-pull */
  126.   GPIO_InitStructure.GPIO_Pins = GPIO_Pins_2;
  127.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  128.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  129.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  130. //  /* Configure USART3 Tx as alternate function push-pull */
  131. //  GPIO_InitStructure.GPIO_Pins = GPIO_Pins_10;
  132. //  GPIO_Init(GPIOC, &GPIO_InitStructure);  
  133. }
  134. /**
  135.   * @brief  Configures the nested vectored interrupt controller.
  136.   * @param  None
  137.   * @retval None
  138.   */
  139. void NVIC_Configuration(void)
  140. {
  141.   NVIC_InitType NVIC_InitStructure;

  142.   /* Configure the NVIC Preemption Priority Bits */  
  143.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  144.   
  145.   /* Enable the USART2 Interrupt */
  146.   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  147.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  148.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  149.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  150.   NVIC_Init(&NVIC_InitStructure);

  151.   /* Enable the USART3 Interrupt */
  152. //  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  153. //  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  154. //  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  155. //  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  156. //  NVIC_Init(&NVIC_InitStructure);
  157. }

  158. /**
  159.   * @brief  Compares two buffers.
  160.   * @param  pBuffer1, pBuffer2: buffers to be compared.
  161.   * @param  BufferLength: buffer's length
  162.   * @retval PASSED: pBuffer1 identical to pBuffer2
  163.   *         FAILED: pBuffer1 differs from pBuffer2
  164.   */
  165. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  166. {
  167.   while(BufferLength--)
  168.   {
  169.     if(*pBuffer1 != *pBuffer2)
  170.     {
  171.       return FAILED;
  172.     }

  173.     pBuffer1++;
  174.     pBuffer2++;
  175.   }

  176.   return PASSED;
  177. }

  178. #ifdef  USE_FULL_ASSERT

  179. /**
  180.   * @brief  Reports the name of the source file and the source line number
  181.   *         where the assert_param error has occurred.
  182.   * @param  file: pointer to the source file name
  183.   * @param  line: assert_param error line source number
  184.   * @retval None
  185.   */
  186. void assert_failed(uint8_t* file, uint32_t line)
  187. {
  188.   /* User can add his own implementation to report the file name and line number,
  189.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  190.   /* Infinite loop */
  191.   while (1)
  192.   {}
  193. }

  194. #endif

  195. /**
  196.   * @}
  197.   */

  198. /**
  199.   * @}
  200.   */

  201. void USART2_IRQHandler(void)
  202. {
  203.   if(USART_GetITStatus(USART2, USART_INT_RDNE) != RESET)
  204.   {
  205.     /* Read one byte from the receive data register */
  206.     RxBuffer1[0] = USART_ReceiveData(USART2);
  207.                 /* Write one byte to the transmit data register */
  208.     USART_SendData(USART2, RxBuffer1[0]);
  209.    
  210.   }
  211. //  
  212. //  if(USART_GetITStatus(USART2, USART_INT_TDE) != RESET)
  213. //  {   
  214. //    /* Write one byte to the transmit data register */
  215. //    USART_SendData(USART2, TxBuffer1[TxCounter1++]);

  216. //    if(TxCounter1 == NbrOfDataToTransfer1)
  217. //    {
  218. //      /* Disable the USART2 Transmit interrupt */
  219. //      USART_INTConfig(USART2, USART_INT_TDE, DISABLE);
  220. //    }   
  221. //  }
  222. }
效果图:
1.jpg
4.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

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