STM32串口通信

STM32的通信接口

STM32主要用的通信接口有USART、IIC、SPI、USB、CAN。这些通信接口的主要目的是将设备的数据传送到另一个设备,扩展硬件系统。
在串口通信中有着TTL、RS232、RS485等电平标准。其中TTL和RS485都为正逻辑,只有RS232为负电压表示1,正电压表示0.
TTL:+3.3V或+5V表示1,0V表示0.
RS232:-3V—— -15V表示1,+3V——+15V表示0.
RS485:+2——+6V表示1,-2V——— -6V表示0。

串口参数时序

波特率:串口通信的速率。
起始位:数据帧的开始,固定为低电平。
数据位:数据的有效载荷,1为高电平,0为低电平。(数据位低位先行)
校验位:用于数据验证。
停止位:数据帧的间隔,固定为高电平。
无校验数据帧
在这里插入图片描述

有校验数据帧
在这里插入图片描述

STM32中USART的基本结构图

请添加图片描述

STM32串口收发代码

初始化串口

void Serial_Init(void)
{
	//配置GPIO管脚
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIOInitStructure;
	GPIOInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIOInitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIOInitStructure);
	
	GPIOInitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIOInitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIOInitStructure);
	
	//开启USART中断
	USART_InitTypeDef USART_InitStruture;
	USART_InitStruture.USART_BaudRate = 9600;
	USART_InitStruture.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStruture.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStruture.USART_Parity = USART_Parity_No;
	USART_InitStruture.USART_StopBits = USART_StopBits_1;
	USART_InitStruture.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStruture);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2 );
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}

`

发送单字节函数

void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while((USART_GetFlagStatus(USART1,USART_FLAG_TXE))==RESET);
	
}

发送数组函数

//发送数组函数
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for(i=0; i<Length; i++)
	{
		Serial_SendByte(Array[i]);
	}
	
}

发送字符串函数

//发送字符串函数
void Serial_SendString(char *String)
{	
	uint8_t i;
	for(i=0; String[i]!='\0'; i++)
	{
		Serial_SendByte(String[i]);
	}
}

中断函数

自己定义了RxFlag标志位,目的是保证本次读取完成后在进行下一次读取

uint8_t Serial_GetRxFlag(void)
{
	if(Serial_RxFlag ==1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}
	
	
uint8_t Serial_GetRxData(void)
{
	
	return Serial_RxData;
}

void USART1_IRQHandler(void)
{
	if((USART_GetITStatus(USART1,USART_IT_RXNE)==1))
	{
		Serial_RxData = USART_ReceiveData(USART1);
		Serial_RxFlag = 1;
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
}

串口发送Hex数据包

在日常使用中通常会通过数据包来进行通信,在确定起始位和停止位后,就可以确定一个数据包的长度。为了防止数据与起始位或者停止位相同,一般会根据实际情况来设置起始位和停止位,在这里我把起始位设置位FF,停止位设置为FE数据位为8位,无校验位。
先通过单个函数将起始位发送,再通过数组发送函数发送固定长度的数据位,最后再发送停止位。

void Serial_SendPacket(void)
 {
	Serial_SendByte(0xFF);
	Serial_SendArray(Serial_TxPacket, 4);
	Serial_SendByte(0xFE);
 }

void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;
	static uint8_t pRxState = 0;
	if((USART_GetITStatus(USART1,USART_IT_RXNE)==1))
	{
		uint8_t RxData = USART_ReceiveData(USART1);
		if(RxState == 0)
		{
			if(RxData == 0xFF)
			{
				RxState = 1;
				pRxState = 0;
			}
		}
		else if(RxState == 1)
		{
			Serial_RxPacket[pRxState] = RxData;
			pRxState++;
			if(pRxState>=4)
			{
				RxState = 2;
				
			}
		}
		else if (RxState == 2)
		{
			if(RxData == 0xFE)
			{
				RxState = 0;
				Serial_RxFlag = 1;
			}
		}
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
}

最后附上整个代码
在使用时记得在头文件中加上
extern uint8_t Serial_TxPacket[];
extern uint8_t Serial_RxPacket[];

#include "stm32f10x.h"                  // Device header
#include "stdio.h"


 uint8_t Serial_TxPacket[4];
 uint8_t Serial_RxPacket[4];
 uint8_t Serial_RxFlag;

void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	
	GPIO_InitTypeDef GPIOInitStructure;
	GPIOInitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIOInitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIOInitStructure);
	
	GPIOInitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIOInitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIOInitStructure);
	
	USART_InitTypeDef USART_InitStruture;
	USART_InitStruture.USART_BaudRate = 9600;
	USART_InitStruture.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStruture.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
	USART_InitStruture.USART_Parity = USART_Parity_No;
	USART_InitStruture.USART_StopBits = USART_StopBits_1;
	USART_InitStruture.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1,&USART_InitStruture);
	
	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2 );
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_Cmd(USART1, ENABLE);
}

//发送单个字节函数
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1,Byte);
	while((USART_GetFlagStatus(USART1,USART_FLAG_TXE))==RESET);
	
}
//重定向printf函数至串口
int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);
	return ch;
}
//发送数组函数

void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for(i=0; i<Length; i++)
	{
		Serial_SendByte(Array[i]);
	}
	
}

//发送字符串函数
void Serial_SendString(char *String)
{	
	uint8_t i;
	for(i=0; String[i]!='\0'; i++)
	{
		Serial_SendByte(String[i]);
	}
}

//发送数字函数
//需要将发送的数字进行拆分
uint32_t Serial_Pow(uint32_t x, uint32_t y)
{
	uint32_t Result = 1;
	while(y--)
	{
		Result *= x;
	}
	return Result;
}
void Serial_SendNum(uint32_t Number, uint8_t Length)
{
	uint8_t i ;
	for(i=0; i<Length; i++)
	{
		Serial_SendByte(Number/Serial_Pow(10,Length-i-1)%10+ '0');
	}
}


//标志位
uint8_t Serial_GetRxFlag(void)
{
	if(Serial_RxFlag ==1)
	{
		Serial_RxFlag = 0;
		return 1;
	}
	return 0;
}
	
//发送数据包函数
void Serial_SendPacket(void)
 {
	Serial_SendByte(0xFF);
	Serial_SendArray(Serial_TxPacket, 4);
	Serial_SendByte(0xFE);
 }

 //中断函数
void USART1_IRQHandler(void)
{
	static uint8_t RxState = 0;
	static uint8_t pRxState = 0;
	if((USART_GetITStatus(USART1,USART_IT_RXNE)==1))
	{
		uint8_t RxData = USART_ReceiveData(USART1);
		if(RxState == 0)
		{
			if(RxData == 0xFF)
			{
				RxState = 1;
				pRxState = 0;
			}
		}
		else if(RxState == 1)
		{
			Serial_RxPacket[pRxState] = RxData;
			pRxState++;
			if(pRxState>=4)
			{
				RxState = 2;
				
			}
		}
		else if (RxState == 2)
		{
			if(RxData == 0xFE)
			{
				RxState = 0;
				Serial_RxFlag = 1;
			}
		}
		USART_ClearITPendingBit(USART1,USART_IT_RXNE);
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值