新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > STM32 RS485 收發(fā)

STM32 RS485 收發(fā)

作者: 時間:2016-11-25 來源:網(wǎng)絡 收藏
485通訊與232通訊沒有什么本質(zhì)區(qū)別,都是利用串口設備進行收發(fā)控制。485發(fā)送與接收需要設置相應的模式,使用485收發(fā)器而不是232芯片。以下是通過按鍵設置板載485芯片的發(fā)送或者接收模式??醋⑨專?p> 工程:

本文引用地址:http://butianyuan.cn/article/201611/321304.htm

1.main.c

//RS485程序測試,RS485接受數(shù)據(jù),通過串口1向上位機發(fā)送接收到的數(shù)據(jù)


#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的數(shù)據(jù)個數(shù),sizeof(a)求出a占用多少地址,sizeof(*(a))求出a中第一個數(shù)據(jù)的長度
#define TxBufferSize (CountOf(TxBuffer)-1) //計算發(fā)送數(shù)據(jù)的個數(shù),為什么減一:因為RxBuffer[]下標0開始
#define RxBufferSize TxBufferSize//計算接受數(shù)據(jù)的個數(shù)

u8 TxBuffer[]="------------wgchnln的RS485發(fā)送數(shù)據(jù)--------------";//發(fā)送機的發(fā)送數(shù)據(jù)數(shù)組
u8 RxBuffer[RxBufferSize];//用于存放接受到的數(shù)據(jù)

vu8 TxCounter=0x00;//記錄當前發(fā)送次數(shù)
vu8 RxCounter=0x00;//記錄當前接受次數(shù)

u8 NUMOfDataToTx=TxBufferSize;//存放發(fā)送數(shù)據(jù)的個數(shù)
u8 NUMOfDataToRx=RxBufferSize;//存放接受的數(shù)據(jù)個數(shù)

//=================================================================================
#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); //蜂鳴器檢驗發(fā)聲
printf("蜂鳴器初始化完成啦");

User_RS485CTRPortConfig(); //RS485模式選擇端口初始化
User_RS485Config(); //RS485初始化
User_RS485NVICConfig(); //RS485中斷嵌套中斷向量配置
printf("RS485初始化完成啦");


while(RS485ModeStatus==RS485Mode_IDL)
{
KeyNumber=User_KeyRead(); //讀取按鍵號碼

RS485ModeStatus=User_RS485ModeSet(KeyNumber); //根據(jù)按鍵號碼配置RS485傳輸模式
}

while(1)
{
if(RS485ModeStatus==RS485Mode_Rx) //如果出于接受模式
{
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //使能接受中斷

while(RxCounter < RxBufferSize) //等待接受完成
{
;
}
printf("接收到的數(shù)據(jù):%s",RxBuffer);
RxCounter=0;
}


else if(RS485ModeStatus==RS485Mode_Tx) //如果出于發(fā)送模式
{
USART_ITConfig(USART2,USART_IT_TXE,ENABLE); //使能發(fā)送中斷

while(TxCounter
{
;
}
printf("數(shù)據(jù)正在發(fā)送:");

for(i=0;i
{
printf("%c",TxBuffer[i]);
if(i==NUMOfDataToTx)
{
printf("");
}
}

TxCounter=0;
}


else
{
KeyNumber=User_KeyRead(); //讀取按鍵號碼

RS485ModeStatus=User_RS485ModeSet(KeyNumber); //根據(jù)按鍵號碼配置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發(fā)送驅(qū)動

#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; //發(fā)送接收模式
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); //發(fā)送端口初始化


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驅(qū)動 RS485使用的是串口2

#include"stm32f10x.h"
#include"user_rs485.h"
#include"user_led.h"
#include


上一頁 1 2 下一頁

關鍵詞: STM32RS48

評論


技術專區(qū)

關閉