STM32:HAL_UART_Receive_IT接收多字符

HAL_UART_Receive_IT放在串口全局中断里,可以接收指定长度的字符串,并在接收完成之后产生中断。
stm32f4xx_it.c里把void USART1_IRQHandler(void)函数修改如下所示即可:

/* USER CODE BEGIN 0 */
extern uint8_t RxBuffer[12];
/* USER CODE END 0 */

void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */

  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
  HAL_UART_Receive_IT(&huart1,(uint8_t *)&RxBuffer,12); //重新打开串口中断
  /* USER CODE END USART1_IRQn 1 */
}

printf用法:
main.c里面添加如下代码即可:

/* USER CODE BEGIN 0 */
int fputc(int ch, FILE *f) 
{
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
    return ch;
}
/* USER CODE END 0 */

其中huart1为指定输出的窗口.

你可能感兴趣的:(STM32:HAL_UART_Receive_IT接收多字符)