485通讯与232通讯没有什么本质区别,都是利用串口设备进行收发控制。485发送与接收需要设置相应的模式,使用485收发器而不是232芯片。以下是通过按键设置板载485芯片的发送或者接收模式。看注释: 工程:
本文引用地址:https://www.eepw.com.cn/article/201611/321304.htm
1.main.c
//RS485程序测试,RS485接受数据,通过串口1向上位机发送接收到的数据
#include"stm32f10x.h"
#include"stm32_eval.h"
#include"user_usart1.h"
#include"user_led.h"
#include"user_key.h"
#include"user_rs485.h"
#include"user_beep.h"
#include
#define CountOf(a) (sizeof(a)/sizeof(*(a)))//求出a的数据个数,sizeof(a)求出a占用多少地址,sizeof(*(a))求出a中第一个数据的长度
#define TxBufferSize (CountOf(TxBuffer)-1) //计算发送数据的个数,为什么减一:因为RxBuffer[]下标0开始
#define RxBufferSize TxBufferSize//计算接受数据的个数
u8 TxBuffer[]="------------wgchnln的RS485发送数据--------------";//发送机的发送数据数组
u8 RxBuffer[RxBufferSize];//用于存放接受到的数据
vu8 TxCounter=0x00;//记录当前发送次数
vu8 RxCounter=0x00;//记录当前接受次数
u8 NUMOfDataToTx=TxBufferSize;//存放发送数据的个数
u8 NUMOfDataToRx=RxBufferSize;//存放接受的数据个数
//=================================================================================
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
//=================================================================================
int main(void)
{
u8 KeyNumber=0; //存放按键号码
u8 RS485ModeStatus=0; //存放RS485传输模式
u8 i;
User_USART1Config(); //USART1初始化
User_LedConfig(); //LED初始化
User_LedSpark(Led0,3); //4LED闪烁3三次
printf("LED初始化完成啦");
User_KeyConfig(); //按键初始化
printf("按键初始化完成啦");
User_BeepConfig(); //蜂鸣器初始化
User_BeepStatus(BeepStatus_TurnOn); //蜂鸣器检验发声
printf("蜂鸣器初始化完成啦");
User_RS485CTRPortConfig(); //RS485模式选择端口初始化
User_RS485Config(); //RS485初始化
User_RS485NVICConfig(); //RS485中断嵌套中断向量配置
printf("RS485初始化完成啦");
while(RS485ModeStatus==RS485Mode_IDL)
{
KeyNumber=User_KeyRead(); //读取按键号码
RS485ModeStatus=User_RS485ModeSet(KeyNumber); //根据按键号码配置RS485传输模式
}
while(1)
{
if(RS485ModeStatus==RS485Mode_Rx) //如果出于接受模式
{
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //使能接受中断
while(RxCounter < RxBufferSize) //等待接受完成
{
;
}
printf("接收到的数据:%s",RxBuffer);
RxCounter=0;
}
else if(RS485ModeStatus==RS485Mode_Tx) //如果出于发送模式
{
USART_ITConfig(USART2,USART_IT_TXE,ENABLE); //使能发送中断
while(TxCounter
{
;
}
printf("数据正在发送:");
for(i=0;i
{
printf("%c",TxBuffer[i]);
if(i==NUMOfDataToTx)
{
printf("");
}
}
TxCounter=0;
}
else
{
KeyNumber=User_KeyRead(); //读取按键号码
RS485ModeStatus=User_RS485ModeSet(KeyNumber); //根据按键号码配置RS485传输模式
}
}
}
//=================================================================================
PUTCHAR_PROTOTYPE
{
USART_SendData(USART1, (uint8_t) ch);
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
}
return ch;
}
//=================================================================================
2.user_usart1.c
a//程序功能:USART1发送驱动
#include"stm32f10x.h"
#include"user_usart1.h"
#include
void User_USART1Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //使能时钟
USART_InitStructure.USART_BaudRate=115200; //波特率
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //8位字长
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_Parity=USART_Parity_No; //无奇偶效验位
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //发送接收模式
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //无硬件流控
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); //使能GPIO与复用功能时钟
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //USART1 TX使用引脚
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推免式输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //50MHZ输出速度
GPIO_Init(GPIOA,&GPIO_InitStructure); //发送端口初始化
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //USART1 RX使用引脚
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输入一般不需要配置速度
GPIO_Init(GPIOA,&GPIO_InitStructure); //接收端初始化
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
printf("看到此信息就说明串口1初始化完成啦");
}
2.user_rs485.c
//程序功能:RS485驱动 RS485使用的是串口2
#include"stm32f10x.h"
#include"user_rs485.h"
#include"user_led.h"
#include
评论