STM32CubeMX的串口配置
配置串口为异步通信
打开串口中断
串口的初始化函数
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;//串口号
huart1.Init.BaudRate = 115200;//波特率
huart1.Init.WordLength = UART_WORDLENGTH_8B;//数据位
huart1.Init.StopBits = UART_STOPBITS_1;//停止位
huart1.Init.Parity = UART_PARITY_NONE;//无奇偶校验
huart1.Init.Mode = UART_MODE_TX_RX;//收发模式
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;//无硬件流控
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
启动串口中断
/* 使能串口接收断 */
__HAL_UART_ENABLE_IT(&UartHandle,UART_IT_RXNE);
设置接受中断函数
uint8_t receive_buf[100]={0};//定义一个串口接收数组(全局变量)
HAL_StatusTypeDef HAL_UART_Receive_IT (UART_HandleTypeDef *huart,
uint8_t *pData,
uint16_t Size);
/* *huart-->需要产生接受中断的串口号,
*pData-->接受到的数据存放位置,
Size-->产生中断的数据长度。 */
HAL_UART_Receive_IT(&huart1,receive_buf,10);
/* 使用HAL_UART_Receive_IT来产生接受中断,必须满足串口输入的数据达到Size的长度. */
中断回调函数
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1);
}
串口发送中断的回调函数
__weak void HAL_UART_TxCpltCallback(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 */ }
串口接收中断回调函数
__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 */ }
改写接受中断函数实现串口的自发自收功能
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &huart1)
{
//将接收到的数据再发送
HAL_UART_Transmit(&huart1,receive_buf,10,10);
}
memset(receive_buf,0,sizeof(receive_buf));
HAL_UART_Receive_IT(&huart1,receive_buf,10);//重新启动串口接收中断
}
空闲中断实现不定长的数据接受
在原来main.c文件中的接受中断改为空闲中断
//__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);//启动串口1的接受中断
__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);
注释掉之前的接受回调函数
//void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
//{
// if(huart == &huart1)
// {
// //将接收到的数据再发送
// HAL_UART_Transmit(&huart1,receive_buf,10,10);
// }
// memset(receive_buf,0,sizeof(receive_buf));
// HAL_UART_Receive_IT(&huart1,receive_buf,10);//重新启动串口接收中断
//}
进入stm32f4x_it.c文件下寻找
void voidUSART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart1);
}
对其进行编写
在文件前需要添加
#include "string.h"
才能使用strlen函数
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
extern char receive_buf[10];
//4、获取空闲中断是否已经产生
int flag=__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE);//产生空闲中断返回1,否则返回0
if(flag)
{
//清除空闲中断标志位
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
//处理数据
/* 添加数据处理函数 */
HAL_UART_Transmit(&huart1,(uint8_t*)receive_buf,strlen(receive_buf),1000);
// 将接收缓冲区指针设置为接收缓冲数组 reveive_buf 的起始地址
huart1.pRxBuffPtr = (uint8_t *)receive_buf;
// 设置需要接收的数据量大小,这里是接收缓冲数组 reveive_buf 的字节数大小
huart1.RxXferSize = sizeof(receive_buf);
// 设置接收计数器为接收缓冲数组 reveive_buf 的字节数大小
// 这个计数器用于跟踪已经接收到多少数据,初始值设为总接收字节数
huart1.RxXferCount = sizeof(receive_buf);
//清空接收数据缓冲区
memset(receive_buf,0,sizeof(receive_buf));
flag=0;
//重新指定接收数据存放的位置
HAL_UART_Receive_IT(&huart1,(uint8_t *)receive_buf,sizeof(receive_buf));
}
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
串口重定向
int fputc(int ch , FILE *fp) //加入这一行代码就会在串口打印出消息
{
HAL_UART_Transmit(&huart1 , (uint8_t*)&ch , 1 , 1);
return ch;
}