此项目源码下载地址:https://github.com/lizhiqiang0204/STM32CubeIDE_Uart
串口与中断配置如下
在生成的main函数中,添加开启串口接收中断
HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */
SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
LED2(OFF);
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRx1Buffer, );//使能串口接收中断
printf("************FreeRTOS********************\r\n"); /* USER CODE END 2 */
然后在接收中断回调函数中写接收过程
/* USER CODE BEGIN 4 */
/**
* @brief Rx Transfer completed callbacks.
* @param huart pointer to a UART_HandleTypeDef structure that contains
* the configuration information for the specified UART module.
* @retval None
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
/* NOTE: This function Should not be modified, when the callback is needed,
the HAL_UART_TxCpltCallback could be implemented in the user file
*/
if(huart->Instance == huart1.Instance)
{
if(bRx1_complete != )
return; if(Uart1_Rx_Cnt >= )
{
Uart1_Rx_Cnt = ;
memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff));
}
else
{
Uart1_RxBuff[Uart1_Rx_Cnt] = aRx1Buffer;
Uart1_Rx_Cnt++;
if((Uart1_RxBuff[Uart1_Rx_Cnt-] == 0x0A)&&(Uart1_RxBuff[Uart1_Rx_Cnt-] == 0x0D))
{
Uart1_Rx_Cnt= ;
bRx1_complete = ;
}
}
}
HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRx1Buffer, );
} /* USER CODE END 4 */
调试结果如下: