【STM32】5—UART串口(中断模式)

目录

0 实验预期效果

1 相关原理图

2 软件配置 

3 代码编写        

        3.1 函数认识

                3.1.1 串口发送

                3.1.2 串口接收 

                3.1.3 中断回调函数

        3.2 代码编写

                3.2.1 定义发送和接收缓冲区

                3.2.2 重新实现中断回调函数

                3.2.3 修改main函数

4 实验结果 


0 实验预期效果

        完成串口数据的接收和发送

1 相关原理图

2 软件配置 

        STM32CubeMX配置USART1:      

        在NVIC中配置USART中断优先级:

3 代码编写        

        3.1 函数认识

        见博客【STM32】HAL库学习 2—hal_uart_kokoのadventure的博客-CSDN博客

        3.1.1 串口发送

/**
  * @brief  Sends an amount of data in non blocking mode.
  * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  *         the sent data is handled as a set of u16. In this case, Size must indicate the number
  *         of u16 provided through pData.
  * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  *               the configuration information for the specified UART module.
  * @param  pData Pointer to data buffer (u8 or u16 data elements).
  * @param  Size  Amount of data elements (u8 or u16) to be sent
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)

        3.1.2 串口接收 

/**
  * @brief  Receives an amount of data in non blocking mode.
  * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  *         the received data is handled as a set of u16. In this case, Size must indicate the number
  *         of u16 available through pData.
  * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  *               the configuration information for the specified UART module.
  * @param  pData Pointer to data buffer (u8 or u16 data elements).
  * @param  Size  Amount of data elements (u8 or u16) to be received.
  * @retval HAL status
  */
HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)

         3.1.3 中断回调函数

/**
  * @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
  */
__weak 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_RxCpltCallback could be implemented in the user file
   */
}

        3.2 代码编写

        3.2.1 定义发送和接收缓冲区

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t hello[] = "USART1 is ready...\n";
uint8_t recv_buf[13] = {0};
/* USER CODE END 0 */

        3.2.2 重新实现中断回调函数

        HAL中弱定义了一个中断回调函数 HAL_UART_RxCpltCallback, 我们需要在用户文件中重新定义该函数,放在哪都可以,这里我放在 main.c 中:

/* USER CODE BEGIN 4 */
/* 中断回调函数 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	/* 判断是哪个串口触发的中断 */
	if(huart ->Instance == USART1)
	{
		//将接收到的数据发送
		HAL_UART_Transmit_IT(huart, (uint8_t*)recv_buf, 13);
		//重新使能串口接收中断
		HAL_UART_Receive_IT(huart, (uint8_t*)recv_buf, 13);
	}
}
/* USER CODE END 4 */

        3.2.3 修改main函数

int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
  //使能串口中断接收
  HAL_UART_Receive_IT(&huart1, (uint8_t*)recv_buf, 13);
  //发送提示信息
  HAL_UART_Transmit_IT(&huart1, (uint8_t*)hello, sizeof(hello));
  /* USER CODE END 2 */

  while (1)
  {
  }
}

4 实验结果 

【本文摘抄内容来自博客:【STM32Cube_07】使用USART发送和接收数据(中断模式)_Mculover666的博客-CSDN博客

【本文仅作为个人学习记录,不出于任何商业目的。】 
 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中的代码展示了一个基于STM32UART串口通信的示例程序。在这个示例中,首先通过引入相关头文件,包括"stm32f10x.h"和"bsp_usart.h"来实现对USART的初始化配置。然后,在主函数main中,通过调用USART_Config函数来进行USART的初始化配置,包括设置波特率、数据位数、停止位数等参数。接着使用Usart_SendString函数向串口发送一串字符串,并通过printf函数向终端输出一段欢迎信息。最后,通过一个无限循环来保持程序的运行。 引用和引用提供了更具体的关于初始化和配置串口的过程和代码说明。在这些引用中,首先要进行相关的时钟使能和GPIO配置,然后设置引脚的复用模式,接着进行串口参数的初始化配置,包括波特率、数据位数、停止位数等。如果需要开启中断,则需要进行中断配置和中断处理函数的编写。最后,使能串口以开始通信。 综上所述,如果您需要实现STM32UART串口通信,可以参考以上提供的代码和过程。请根据自己的具体需求进行相应的配置和修改。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [STM32——USART接发通信实验代码解析(适合野火stm32f103ZE)](https://blog.csdn.net/m0_57396609/article/details/120387284)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [(十三)STM32——串口通信(UART)](https://blog.csdn.net/weixin_66578482/article/details/126270118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值