参考:http://t.csdn.cn/NXP9Q
    引脚
    image.png
    串口发送函数:

    1. HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
    2. HAL_UART_Transmit(&huart1, "TMD", 3, 0xffff); //串口发送三个字节数据,最大传输时间0xffff

    中断接收

    1. HAL_UART_Transmit(&huart1, "TMD", 3, 0xffff); //串口发送三个字节数据,最大传输时间0xffff
    2. HAL_Delay(1000);
    3. HAL_UART_Receive_IT(&huart1,(uint8_t *)&value,1); //中断接收一个字符,存储到value中
    4. HAL_UART_Transmit(&huart1, (uint8_t *)&value, 1, 0xffff);
    5. HAL_Delay(1000);

    中断发送

    1. HAL_UART_Transmit_IT(&huart1, "TMD", 2);
    2. HAL_Delay(1000);

    发送数据被正常返回,需要添加哪些函数:

    1. /* USER CODE BEGIN Header */
    2. /**
    3. ******************************************************************************
    4. * @file : main.c
    5. * @brief : Main program body
    6. ******************************************************************************
    7. * @attention
    8. *
    9. * Copyright (c) 2022 STMicroelectronics.
    10. * All rights reserved.
    11. *
    12. * This software is licensed under terms that can be found in the LICENSE file
    13. * in the root directory of this software component.
    14. * If no LICENSE file comes with this software, it is provided AS-IS.
    15. *
    16. ******************************************************************************
    17. */
    18. /* USER CODE END Header */
    19. /* Includes ------------------------------------------------------------------*/
    20. #include "main.h"
    21. #include "usart.h"
    22. #include "gpio.h"
    23. #include <string.h>
    24. #define RXBUFFERSIZE 256 //最大接收字节数
    25. char RxBuffer[RXBUFFERSIZE]; //接收数据
    26. uint8_t aRxBuffer; //接收中断缓冲
    27. uint8_t Uart1_Rx_Cnt = 0; //接收缓冲计数
    28. /* Private includes ----------------------------------------------------------*/
    29. /* USER CODE BEGIN Includes */
    30. /* USER CODE END Includes */
    31. /* Private typedef -----------------------------------------------------------*/
    32. /* USER CODE BEGIN PTD */
    33. /* USER CODE END PTD */
    34. /* Private define ------------------------------------------------------------*/
    35. /* USER CODE BEGIN PD */
    36. #define RXBUFFERSIZE 256
    37. char RxBuffer[RXBUFFERSIZE];
    38. /* USER CODE END PD */
    39. /* Private macro -------------------------------------------------------------*/
    40. /* USER CODE BEGIN PM */
    41. /* USER CODE END PM */
    42. /* Private variables ---------------------------------------------------------*/
    43. /* USER CODE BEGIN PV */
    44. /* USER CODE END PV */
    45. /* Private function prototypes -----------------------------------------------*/
    46. void SystemClock_Config(void);
    47. /* USER CODE BEGIN PFP */
    48. /* USER CODE END PFP */
    49. /* Private user code ---------------------------------------------------------*/
    50. /* USER CODE BEGIN 0 */
    51. /* USER CODE END 0 */
    52. /**
    53. * @brief The application entry point.
    54. * @retval int
    55. */
    56. int main(void)
    57. {
    58. /* USER CODE BEGIN 1 */
    59. /* USER CODE END 1 */
    60. /* MCU Configuration--------------------------------------------------------*/
    61. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    62. HAL_Init();
    63. /* USER CODE BEGIN Init */
    64. /* USER CODE END Init */
    65. /* Configure the system clock */
    66. SystemClock_Config();
    67. /* USER CODE BEGIN SysInit */
    68. /* USER CODE END SysInit */
    69. /* Initialize all configured peripherals */
    70. MX_GPIO_Init();
    71. MX_USART1_UART_Init();
    72. /* USER CODE BEGIN 2 */
    73. HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1);
    74. /* USER CODE END 2 */
    75. /* Infinite loop */
    76. /* USER CODE BEGIN WHILE */
    77. while (1)
    78. {
    79. /* USER CODE END WHILE */
    80. /* USER CODE BEGIN 3 */
    81. }
    82. /* USER CODE END 3 */
    83. }
    84. /**
    85. * @brief System Clock Configuration
    86. * @retval None
    87. */
    88. void SystemClock_Config(void)
    89. {
    90. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    91. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    92. /** Configure the main internal regulator output voltage
    93. */
    94. __HAL_RCC_PWR_CLK_ENABLE();
    95. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    96. /** Initializes the RCC Oscillators according to the specified parameters
    97. * in the RCC_OscInitTypeDef structure.
    98. */
    99. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    100. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    101. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    102. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    103. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    104. {
    105. Error_Handler();
    106. }
    107. /** Initializes the CPU, AHB and APB buses clocks
    108. */
    109. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
    110. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    111. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
    112. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    113. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    114. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    115. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    116. {
    117. Error_Handler();
    118. }
    119. }
    120. /* USER CODE BEGIN 4 */
    121. /* USER CODE END 4 */
    122. /**
    123. * @brief This function is executed in case of error occurrence.
    124. * @retval None
    125. */
    126. void Error_Handler(void)
    127. {
    128. /* USER CODE BEGIN Error_Handler_Debug */
    129. /* User can add his own implementation to report the HAL error return state */
    130. __disable_irq();
    131. while (1)
    132. {
    133. }
    134. /* USER CODE END Error_Handler_Debug */
    135. }
    136. #ifdef USE_FULL_ASSERT
    137. /**
    138. * @brief Reports the name of the source file and the source line number
    139. * where the assert_param error has occurred.
    140. * @param file: pointer to the source file name
    141. * @param line: assert_param error line source number
    142. * @retval None
    143. */
    144. void assert_failed(uint8_t *file, uint32_t line)
    145. {
    146. /* USER CODE BEGIN 6 */
    147. /* User can add his own implementation to report the file name and line number,
    148. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    149. /* USER CODE END 6 */
    150. }
    151. #endif /* USE_FULL_ASSERT */
    152. /* USER CODE BEGIN 4 */
    153. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
    154. {
    155. /* Prevent unused argument(s) compilation warning */
    156. UNUSED(huart);
    157. /* NOTE: This function Should not be modified, when the callback is needed,
    158. the HAL_UART_TxCpltCallback could be implemented in the user file
    159. */
    160. if(Uart1_Rx_Cnt >= 255) //溢出判断
    161. {
    162. Uart1_Rx_Cnt = 0;
    163. memset(RxBuffer,0x00,sizeof(RxBuffer));
    164. HAL_UART_Transmit(&huart1, (uint8_t *)"数据溢出", 10,0xFFFF);
    165. }
    166. else
    167. {
    168. RxBuffer[Uart1_Rx_Cnt++] = aRxBuffer; //接收数据转存
    169. if((RxBuffer[Uart1_Rx_Cnt-1] == 0x0A)&&(RxBuffer[Uart1_Rx_Cnt-2] == 0x0D)) //判断结束位
    170. {
    171. HAL_UART_Transmit(&huart1, (uint8_t *)&RxBuffer, Uart1_Rx_Cnt,0xFFFF); //将收到的信息发送出去
    172. while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX);//检测UART发送结束
    173. Uart1_Rx_Cnt = 0;
    174. memset(RxBuffer,0x00,sizeof(RxBuffer)); //清空数组
    175. }
    176. }
    177. HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRxBuffer, 1); //再开启接收中断
    178. }
    179. /* USER CODE END 4 */