485通訊與232通訊沒有什么本質區別,都是利用串口設備進行收發控制。485發送與接收需要設置相應的模式,使用485收發器而不是232芯片。以下是通過按鍵設置板載485芯片的發送或者接收模式。看注釋: 工程:
本文引用地址:http://www.104case.com/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
評論