STM32的RS485通信

1.简介

与CAN类似,RS-485是一种工业控制环境中常用的通讯块议,它具有抗干扰能力强、传输距离远的特点。RS-485通讯协议由RS-232协议改进而来,协议层不变,只是改进了物理层,因而保留了串口通讯协议应用简单的特点。
在这里插入图片描述用的是SP3485芯片:
在这里插入图片描述

通信的时候,A端口连接另一个设备的A端口,B端口连接B端口,不是交叉相连。
最多能够连接128个设备,所以在某种情况下可以取代网络,RE引脚用来控制通讯数据的方向,要么进行接收,要么进行发送。
本质还是串口通信

RS485_RE为高电平的时候,DE为高电平有效,允许发送数据
RS485_RE为低电平的时候,RE为低电平有效,允许接收数据

所以当你要发送数据的时候 ,需要将与RE连接的引脚置为高电平、

2.编码

所以我们大致可以得到一个程序模板:

发送数据函数
void rs485_send(uint8_t *pbuf,uint32_t len)
{
    
    
	//设置RS458为发送模式,将所连引脚设置高电平输出
	PGout(2)=1;
	//调用串口2的库函数发送数据
	...
	//延时100us
	delay_us(100);
	//设置RS485为接收模式
	PGout(2)=0;

}
接收函数,使用中断接收
void USART2_IRQHandler(void)
{
    
    
	uint8_t data=0;
	if(USART2_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
		data = USART2_ReceiveData(USART2);

}

下面是演示代码,供参考

#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_usart.h"

//模式控制
#define RS485_TX_EN		PGout(8)	//485模式控制.0,接收;1,发送.
static GPIO_InitTypeDef  	GPIO_InitStructure;
static USART_InitTypeDef 	USART_InitStructure;
static NVIC_InitTypeDef 	NVIC_InitStructure;		   

void USART1_Init(uint32_t baud)
{
    
    
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); 							//使能GPIOA时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);							//使能USART1时钟
 
	//串口1对应引脚复用映射
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); 						//GPIOA9复用为USART1
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); 						//GPIOA10复用为USART1
	
	//USART1端口配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; 						//GPIOA9与GPIOA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;									//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;								//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 									//推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 									//上拉
	GPIO_Init(GPIOA,&GPIO_InitStructure); 											//初始化PA9,PA10

	//USART1 初始化设置
	USART_InitStructure.USART_BaudRate = baud;										//波特率设置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;						//字长为8位数据格式
	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); 										//初始化串口1
	
	USART_Cmd(USART1, ENABLE);  													//使能串口1 
	
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);									//开启相关中断

	//Usart1 NVIC 配置
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;								//串口1中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;							//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;								//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;									//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);													//根据指定的参数初始化VIC寄存器
}


void USART1_WriteBytes(uint8_t *pbuf,uint32_t len)
{
    
    
	uint32_t i =0;
	
	for(i=0; i<len; i++)
	{
    
    
		//发送数据
		USART_SendData(USART1,*pbuf++);
		
		while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);		
	}	
}

//初始化IO 串口2
//baud:波特率	  
void RS485_Init(uint32_t baud)
{
    
      	 

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); 				//使能GPIOA时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);				//使能USART2时钟
	
	//串口2引脚复用映射
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); 			//GPIOA2复用为USART2
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); 			//GPIOA3复用为USART2
	
	//USART2    
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; 				//GPIOA2与GPIOA3
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;						//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;					//速度100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 						//推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; 						//上拉
	GPIO_Init(GPIOA,&GPIO_InitStructure); 								//初始化PA2,PA3
	
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG,ENABLE); 				//使能GPIOA时钟
	
	//PG8推挽输出,485模式控制  
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; 							//GPIOG8
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;						//输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;					//速度100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 						//推挽输出
	GPIO_InitStructure.GPIO_PuPd  =  GPIO_PuPd_UP; 						//上拉
	GPIO_Init(GPIOG,&GPIO_InitStructure); 								//初始化PG8
	

	//USART2 初始化设置
	USART_InitStructure.USART_BaudRate = baud;							//波特率设置
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;			//字长为8位数据格式
	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); 							//初始化串口2
	
	USART_Cmd(USART2, ENABLE);  										//使能串口 2
	USART_ClearFlag(USART2, USART_FLAG_TC);
	

	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);						//开启接收中断

	//Usart2 NVIC 配置
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;				//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;					//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;						//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);										//根据指定的参数初始化NVIC寄存器
	
	RS485_TX_EN=0;														//默认为接收模式	

}

//RS485发送len个字节.
//pbuf:发送区首地址
//len:发送的字节数
void RS485_Send_Data(uint8_t *pbuf,uint32_t len)
{
    
    
	uint32_t i;

	RS485_TX_EN=1;												//设置为发送模式
	
	for(i=0; i<len; i++)										//循环发送数据
	{
    
    
		USART_SendData(USART2,pbuf[i]); 						//发送数据
		while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);	
	}	
	
	delay_us(100);
	RS485_TX_EN=0;												//设置为接收模式	

}

int main(void)
{
    
     
	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); 
		
	//设置中断优先级分组2
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	//串口1,波特率115200bps,开启接收中断
	USART1_Init(115200);
	
	//RS485初始化,115200bps
	RS485_Init(115200);
	
	
	while(1)
	{
    
    

	}
}


void USART1_IRQHandler(void)    	//串口1中断服务程序
{
    
    
	uint8_t d;

	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  	//接收中断
	{
    
    
		//接收数据
		d = USART_ReceiveData(USART1);	

		RS485_Send_Data(&d,1);
	 
	} 

} 

void USART2_IRQHandler(void)
{
    
    
	uint8_t d;
	
	if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//接收到数据
	{
    
    	 
		d =USART_ReceiveData(USART2);					 //读取接收到的数据USART2->DR

		USART1_WriteBytes(&d,1);
	}  											 
} 

猜你喜欢

转载自blog.csdn.net/ABCisCOOL/article/details/115256095