前言:本代码使用宏定义的方式裁切.c .h文件,不用担心放在工程中编译后产生对应的bin(编译后不会产生usart的内容)
代码框架:
有三个宏定义控制整个.c 和 .h的裁剪(注释宏定义则关闭相关函数)
代码功能:
① 实现波特率115200,8位数据位,1位停止位,无校验位,tx,rx的初始化(接收数据使用的是串口的rx中断)
② 实现c库中的printf的重定向(在代码中需解开注释才可用,在中断服务函数下面int fputc(int ch, FILE* f) )
③ 实现不定长度的数据包收发(只有串口1书写了,可根据自己的需求完善其他串口)
代码讲解:
1. 数据包发送函数:在初始化串口后,只需调用一次即可发送data数据内len长度的数据包。
2.数据包接收:Rxdata1为每一个数据,RxPack1为接收的数据包数据不含包头包尾(后面的数字为对应的串口)变量已经在头文件extern出去了,在别的文件直接调用即可。其中数据包每完成一次接收会有一个标志位pack1_f,使用函数USART1_GetPackFlag即可获得。
完整代码: (直接可用)
头文件内容:
#ifndef __USART_H_
#define __USART_H_
/**** 头文件 ****/
#include "stm32f10x.h"
#include <stdio.h>
/**** 宏定义 ****/
#define EN_USART1 //使用串口1
#define EN_USART2 //使用串口2
#define EN_USART3 //使用串口3
/**** 函数 ****/
#ifdef EN_USART1
void USART1_Init(void);
void USART1_SendByte(u8 byte);
void USART1_SendString(char* str);
void USART1_IRQHandler(void);
void USART1_SendPack(uint8_t* dat, u8 len, u8 top, u8 tail);
s8 USART1_GetPackFlag(void);
extern u8 Rxdata1; // 数据
extern u8 RxPack1[4];// 数据包
#endif //EN_USART1
#ifdef EN_USART2
void USART2_Init(void);
void USART2_SendByte(u8 byte);
void USART2_SendString(char* str);
void USART2_IRQHandler(void);
extern u8 Rxdata2;
#endif //EN_USART2
#ifdef EN_USART3
void USART3_Init(void);
void USART3_SendByte(u8 byte);
void USART3_SendString(char* str);
void USART3_IRQHandler(void);
extern u8 Rxdata3;
#endif //EN_USART3
#endif // __USART_H_
.c内容
#include "usart.h"
#ifdef EN_USART1
/***********************************************************
*@fuction : USART1_Init
*@brief : USART1 初始化
*@param : None
*@return : None
*@author : HongScholar
*@date : 2025.01.12
***********************************************************/
void USART1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//USART1_TX PA9
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIOA->LCKR = 0x00010000 | GPIO_Pin_9;
GPIOA->LCKR = GPIO_Pin_9;
GPIOA->LCKR = 0x00010000 | GPIO_Pin_9;
GPIOA->LCKR;
GPIOA->LCKR;
//USART1_RX PA10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIOA->LCKR = 0x00010000 | GPIO_Pin_10;
GPIOA->LCKR = GPIO_Pin_10;
GPIOA->LCKR = 0x00010000 | GPIO_Pin_10;
GPIOA->LCKR;
GPIOA->LCKR;
USART_InitTypeDef USART_InitStructure;
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
// 开启接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// 设置中断优先级
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;// 抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // 响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/***********************************************************
*@fuction : USART1_SendByte
*@brief : USART1 发发送一个字节
*@param : 数据
*@return : None
*@author : HongScholar
*@date : 2025.01.12
***********************************************************/
void USART1_SendByte(u8 byte)
{
USART_SendData(USART1, byte);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void USART1_SendString(char* str)
{
while(*str != '\0')
USART1_SendByte(*str++);
}
/***********************************************************
*@fuction : USART1_SendPack
*@brief : USART1 发送数据包 帧头01 帧尾68
*@param : 数据, 长度, 包头, 包尾
*@return : None
*@author : HongScholar
*@date : 2025.01.12
***********************************************************/
void USART1_SendPack(uint8_t* dat, u8 len, u8 top, u8 tail)
{
USART1_SendByte(top);
for(uint8_t i=0; i<len; i++)
USART1_SendByte(dat[i]);
USART1_SendByte(tail);
}
s8 pack1_f; // 数据包接收完成标志位
u8 RxPack1[4];// 数据包
/***********************************************************
*@fuction : USART1_GetPackFlag
*@brief : USART1 获取接收数据包完成标志位
*@param : None
*@return : 1: finish
-1: err 接收错误
0: empty
*@author : HongScholar
*@date : 2025.01.12
***********************************************************/
s8 USART1_GetPackFlag(void)
{
if(pack1_f == 1)
{
pack1_f = 0;
return 1;
}
else if(pack1_f == -1)
{
pack1_f = 0;
return -1;
}
else
return 0;
}
/***********************************************************
*@fuction : USART1_GetPack
*@brief : USART1 接收数据包 帧头01 帧尾68
*@param : USRT1->DR, 无帧头帧尾的数据包, 包长, 帧头, 帧尾
*@return : None
*@author : HongScholar
*@date : 2025.01.12
***********************************************************/
void USART1_GetPack(uint8_t* DR, u8* dat, u8 len, u8 top, u8 tail)
{
static u8 rx1_s; // 接收进程
static u8 p_rx1; // 数据帧
/* 接收帧头*/
if(rx1_s == 0)
{
if(*DR == top)// 帧头
{
rx1_s = 1;
p_rx1 = 0;
}
}/* 接收数据*/
else if(rx1_s == 1)
{
dat[p_rx1] = *DR;
p_rx1++;
if(p_rx1 == len)
{
rx1_s = 2;
p_rx1 = 0;
}
}/* 接收帧尾*/
else if(rx1_s == 2)
{
if(*DR == tail)// 帧尾
{
rx1_s = 0;
pack1_f = 1;
}
else/* 接收错误, 清空之前接收*/
{
pack1_f = -1;
for(uint8_t i=0; i<len; i++)
dat[i] = 0x00;
}
}
}
u8 Rxdata1;
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
{
Rxdata1 = USART_ReceiveData(USART1); //读取接收到的数据
USART1_GetPack(&Rxdata1, RxPack1, 4, 0x01, 0x68);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
/* 重印相printf*/
//int fputc(int ch, FILE* f)
//{
// USART1_SendByte(ch);
// return ch;
//}
#endif //EN_USART1
#ifdef EN_USART2
void USART2_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//USART2_TX PA2
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinLockConfig(GPIOA, GPIO_Pin_2);
//USART2_RX PA3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinLockConfig(GPIOA, GPIO_Pin_3);
USART_InitTypeDef USART_InitStructure;
USART_DeInit(USART2);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
// 开启接收中断
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
// 设置中断优先级
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;// 抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // 响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_SendByte(uint8_t byte)
{
USART_SendData(USART2, byte);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
}
void USART2_SendString(char* str)
{
while(*str != '\0')
{
USART2_SendByte(*str++);
}
}
u8 Rxdata2;
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
{
Rxdata2 = USART_ReceiveData(USART2); //读取接收到的数据
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
/* 重印相printf*/
//int fputc(int ch, FILE* f)
//{
// USART2_SendByte(ch);
// return ch;
//}
#endif //EN_USART2
#ifdef EN_USART3
void USART3_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//USART3_TX PB10
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinLockConfig(GPIOB, GPIO_Pin_10);
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinLockConfig(GPIOB, GPIO_Pin_11);
USART_InitTypeDef USART_InitStructure;
USART_DeInit(USART3);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
// 开启接收中断
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
// 设置中断优先级
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;// 抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // 响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART3_SendByte(uint8_t byte)
{
USART_SendData(USART3, byte);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
void USART3_SendString(char* str)
{
while(*str != '\0')
{
USART3_SendByte(*str++);
}
}
u8 Rxdata3;
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) == SET)
{
Rxdata3 = USART_ReceiveData(USART3); //读取接收到的数据
USART_ClearITPendingBit(USART3, USART_IT_RXNE);
}
}
/* 重印相printf*/
//int fputc(int ch, FILE* f)
//{
// USART3_SendByte(ch);
// return ch;
//}
#endif //EN_USART3
作者希望所有读者一路顺风,发现乐趣!敬请期待...