文章目录[隐藏]
一,配置串口初始化
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();
}
}
二,配置串口中断
1.先写一个串口接收函数
typedef struct
{
bool UART2_Flag_Bit;
unsigned int RxCounter; //IDLE receive flag
uint8_t RxBuff[256]; //DMA receive buffer
}USART_2;
uint8_t UART2_TIME=0;
void USER2_UART_IRQHandler()//串口中段处理函数
{
if (__HAL_UART_GET_FLAG(&uartoline ,UART_FLAG_RXNE )!=RESET ){
__HAL_UART_ENABLE_IT(&uartoline,UART_IT_IDLE) ;
Off_Line.RxBuff[Off_Line.RxCounter++] =(uint8_t)(uartoline.Instance->DR&(uint8_t)0x00ff);
UART2_TIME=1;
__HAL_UART_CLEAR_FLAG ( &uartoline,UART_FLAG_RXNE);}
if((__HAL_UART_GET_FLAG(&uartoline ,UART_FLAG_IDLE )!=RESET ))
{
__HAL_UART_DISABLE_IT(&uartoline ,UART_IT_IDLE);
}
}
2.替换掉串口中断
void USART2_IRQHandler(void)
{
/* USER CODE BEGIN USART2_IRQn 0 */
USER2_UART_IRQHandler();
/* USER CODE END USART2_IRQn 0 */
// HAL_UART_IRQHandler(&huart2);
/* USER CODE BEGIN USART2_IRQn 1 */
/* USER CODE END USART2_IRQn 1 */
}
3.在进入程序之前切记打开中断
void USART_IT_Start(void)
{
__HAL_UART_ENABLE_IT(&huart2,UART_IT_RXNE);
}
三,处理串口接收
1.运用定时器计时手法,来判断什么时间串口接收完毕;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef*htim)
{
if(htim->Instance==uart10ms_.Instance)//10ms定时器
{
if(UART2_TIME)
{
UART2_TIME++;
if(UART2_TIME>4)
{
Off_Line.UART2_Flag_Bit=1;
UART2_TIME=0;
}
}
}
}
2.主循环中判断标志位处理数据
void main (void)
{
HAL_Init();
SystemClock_Config();
MX_USART2_UART_Init();
USART_IT_Start();
HAL_TIM_Base_Start_IT(&htim5);
while(1)
{
if(Off_Line.UART2_Flag_Bit)
{
Off_Line.UART2_Flag_Bit=0;
//执行操作
}
}
}
四,串口发送
1.printf发送
#include "stdio.h"
#include "string.h"
int fputc(int ch,FILE *f)//printf打印
{
while(!(UART5->SR & (1<<7)));//TXE=1状态寄存器
UART5->DR=(uint8_t)ch;
return ch;
}
void main (void)
{
printf("123");
}
2.HAL库发送函数
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
/*
UART_HandleTypeDef *huart 发送的串口
uint8_t *pData 发送的数组
uint16_t Size 发送的字节
uint32_t Timeout 超时时间
*/
void main (void)
{
uint8_t buf[25];
HAL_UART_Transmit(&huart5,buf,25,0xfff);
}
3.根据HAL_UART_Transmit修改成多printf发送函数
void uart2_printf(const char *format, ...)//串口5发送
{
va_list args;
uint32_t length;
uint8_t txbuf[TXBUF_SIZE_MAX] = {0};
va_start(args, format);
length = vsnprintf((char *)txbuf, sizeof(txbuf), (char *)format, args);
va_end(args);
HAL_UART_Transmit(&huart5, (uint8_t *)txbuf, length, HAL_MAX_DELAY);
memset(txbuf, 0, TXBUF_SIZE_MAX);
}
五,例举:将接收的内容转发出去
void main (void)
{
HAL_Init();
SystemClock_Config();
MX_USART2_UART_Init();
USART_IT_Start();
HAL_TIM_Base_Start_IT(&htim5);
while(1)
{
if(Off_Line.UART2_Flag_Bit)
{
Off_Line.UART2_Flag_Bit=0;
HAL_UART_Transmit(&huart5,Off_Line.RxBuff,Off_Line.RxCounter,0xfff);
Off_Line.RxCounter=0;
memset(Off_Line.RxBuff,0x00,sizeof(Off_Line.RxBuff)); //清空数组;
}
}
}
版权声明:本文为CSDN博主「Z*Y*C」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45763485/article/details/120503415
暂无评论