STM32Cube IDE配置串口发送与接收

此项目源码下载地址: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, 1);//使能串口接收中断
  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 != 0)
            return;

        if(Uart1_Rx_Cnt >= 255)
        {
            Uart1_Rx_Cnt = 0;
            memset(Uart1_RxBuff,0x00,sizeof(Uart1_RxBuff));
        }
        else
        {
            Uart1_RxBuff[Uart1_Rx_Cnt] = aRx1Buffer;
            Uart1_Rx_Cnt++;
            if((Uart1_RxBuff[Uart1_Rx_Cnt-1] == 0x0A)&&(Uart1_RxBuff[Uart1_Rx_Cnt-2] == 0x0D))
            {
                Uart1_Rx_Cnt= 0;
                bRx1_complete = 0;
            }
        }
    }
    HAL_UART_Receive_IT(&huart1, (uint8_t *)&aRx1Buffer, 1);
}

/* USER CODE END 4 */
复制代码

调试结果如下:

 

posted @   阿坦  阅读(10367)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示