您的位置 首页 新能源

根据51单片机的MODBUS通讯C言语程序(从机)

includereg52htypedefunsignedcharuint8;typedefunsignedintuint16;typedefunsignedlonguint32;defineTIMER

#include “reg52.h”

typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
#define TIMER_HIGHT 0xf8
#define TIMER_LOW 0xcd
sbit bRunLED =P3^4; //运转指示灯
sbit bWatchDog=P1^7; //看门狗复位
sbit b485Send= P1^3; //75LBC184 发送接纳操控
uint32 dwTickCount,dwIntTick; //时钟
uint8 idata sendBuf[16],receBuf[16]; //发送接纳缓冲区
uint8 idata checkoutError; // ==2 偶校验错
uint8 idata receTimeOut; //接纳超时
uint8 idata c10ms; //10ms 计时
bit b1ms,bt1ms,b10ms,bt10ms,b100ms,bt100ms; //守时标志位
// 串行中止程序
void commIntProc() interrupt 4
{
if(TI)
{
TI = 0;
if(sendPosi < sendCount)
{
sendPosi++;
ACC = sendBuf[sendPosi];
TB8 = P; //加上校验位
SBUF = sendBuf[sendPosi];
}
else
{
b485Send = 0; //发送完后将485置于接纳状况
receCount = 0; //清接纳地址偏移寄存器
checkoutError = 0;
}
}
else if(RI)
{
RI = 0;
receTimeOut = 10; //通讯超时值
receBuf[receCount] = SBUF;
ACC = receBuf[receCount];
if(P != RB8)
checkoutError = 2; //偶校验犯错
receCount++; //接纳地址偏移寄存器加1
receCount &= 0x0f; //最多一次只能接纳16个字节
}
} // void CommIntProc()
//守时器0 1ms 中止
void timer0IntProc() interrupt 1
{
TL0 = TIMER_LOW;
TH0 = TIMER_HIGHT;
dwIntTick++;
bt1ms = 1;
c10ms++;
if(c10ms >= 10)
{
c10ms = 0; //10ms计时器清零
bt10ms = 1;
}
} // void Timer0IntProc()
//外部中止0
void intEx0Proc(void) interrupt 0
{
}
//计数器1中止
void counter1IntProc(void) interrupt 3 using 1
{
}
//守时处理
void timeProc(void)
{
static uint8 c200ms;
bWatchDog = ~ bWatchDog; //看门狗取反
b1ms = 0;
b10ms = 0;
b100ms = 0;
ET0 = 0;
dwTickCount = dwIntTick;
ET0 = 1;
if(bt1ms)
{
bt1ms = 0;
b1ms = 1;
if(receTimeOut>0)
{
receTimeOut–;
if(receTimeOut==0 && receCount>0) //判别通讯接纳是否超时
{
b485Send = 0; //将485置为接纳状况
receCount = 0; //将接纳地址偏移寄存器清零
checkoutError = 0;
}
}
}
if(bt100ms)
{
bt100ms = 0;
b100ms = 1;
}
if(bt10ms) //判别中止10ms标志位是否1
{
bt10ms = 0; //清中止10ms标志位
b10ms = 1;
c200ms++; //200ms计时器加1
if(c200ms >= 20) //判别是否计时到200ms
{
c200ms = 0; //清200ms计时器
bRunLED = ~bRunLED; //取反运转指示灯
}
}
} // void TimerProc(void)
//初始化串口
void initUart(void)
{
//T2 用于波特率 9600
T2CON = 0x30;
RCAP2H = 0xff;
RCAP2L = 0xb8;
TR2 = 1;
//偶校验
SCON = 0xd0;
PCON = 0;
ES = 1;
}//void initUart(void)
//初始化中止
void initInt(void)
{
TMOD = 0x51;
TH0 = TIMER_HIGHT;
TL0 = TIMER_LOW;
TR0 = 1;
ET0 = 1;
TH1 = 0; //9600
TL1 = 0;
TR1 = 0; //守时器1用于计数守时器2用于波特
ET1 = 1;
IT0 = 1;
IT1 = 1;
EX0 = 0;
PX0 = 1;
EX1 = 0;
initUart();
EA = 1;
} // void initInt(void)
//初始化
void initProg(void)
{
initInt();
b485Send = 0;
}
void main(void)
{
initProg();
while(1)
{
timeProc();
checkComm0Modbus();
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/xinnengyuan/255875.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部