Connectivity
下选择合适的串口,这里选择USART1
Asynchronous
NVIC Settings
下,打开串口中断。Parameter Settings
中,根据情况配置串口的参数。在GPIO Setting
中可以显示默认的IO引脚
右侧芯片引脚部分会显示配置的结果。
main
/* USER CODE BEGIN WHILE */
uint8_t cnt = 0;
while (1)
{
HAL_UART_Transmit(&huart1, &cnt, 1, HAL_MAX_DELAY);
cnt++;
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
通过
HAL_UART_Transmit
函数发送数据。
/* USER CODE BEGIN WHILE */
HAL_UART_Receive_IT(&huart1, usart1_buffer, USART1_BUFFER_LEN);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
/* USER CODE BEGIN 0 */
#define USART1_BUFFER_LEN 1
uint8_t usart1_buffer[USART1_BUFFER_LEN];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
HAL_UART_Transmit(&huart1, (const uint8_t *)usart1_buffer, USART1_BUFFER_LEN, HAL_MAX_DELAY);
// 开启中断接收
HAL_UART_Receive_IT(&huart1, usart1_buffer, USART1_BUFFER_LEN);
}
/* USER CODE END 0 */
?printf配置
#include <stdio.h>
int fputc(int ch, FILE* f) {
HAL_UART_Transmit(&huart1, (uint8_t *)&ch,1, 0xFFFF);
return ch;
}
/* USER CODE BEGIN WHILE */
uint8_t cnt = 0;
while (1)
{
HAL_UART_Transmit_DMA(&huart1, &cnt, 1);
cnt++;
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
#define USART1_BUFFER_LEN 8
uint8_t usart1_buffer[USART1_BUFFER_LEN];
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
HAL_UART_Transmit_DMA(&huart1, (const uint8_t *)usart1_buffer, USART1_BUFFER_LEN);
// 开启中断接收
HAL_UART_Receive_DMA(&huart1, usart1_buffer, USART1_BUFFER_LEN);
}