目录
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博客】
【本文仅作为个人学习记录,不出于任何商业目的。】