📄 main.c
字号:
#include "main.h"
/******************************
微控电子 www.mcuc.cn
modbus RTU 的C51程序
单片机89S52
通信波特率 9600 8位数据 1位停止位 偶校验 485通位接口
单片机控制板地址 localAddr(变量)
通信可设置数据的地址:
字地址 0 - 255 (只取16位的低8位)
位地址 0 - 255 (只取16位的低8位)
*******************************/
uint8 sendBuf[16],receBuf[16]; //发送接收缓冲区
uint16 a;
uint8 checkoutError; // ==2 偶校验错
uint8 receTimeOut; //接收超时
uint8 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个字节
}
}
//定时器0 1ms 中断
void timer0IntProc() interrupt 1
{
TL0 = TIMER_LOW;
TH0 = TIMER_HIGHT;
bt1ms = 1;
c10ms++;
if(c10ms >= 10)
{
c10ms = 0; //10ms计时器清零
bt10ms = 1;
}
}
//外部中断0
void intEx0Proc(void) interrupt 0
{
}
//定时处理
void timeProc(void)
{
static uint8 c200ms;
b1ms = 0;
b10ms = 0;
b100ms = 0;
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 >= 100) //判断是否计时到200ms
{
c200ms = 0; //清200ms计时器
bRunLED = ~bRunLED; //取反运行指示灯
a++;
}
}
}
//初始化中断
void initInt(void)
{
//WDT_CONTR=0x34;//启动看门狗
SCON = 0x50; //串口方式1,允许接收
TMOD=0x21;//定时/计数器为16位、1为8位自动重装模式
TH0 = TIMER_HIGHT;
TL0 = TIMER_LOW;
PCON=0x00;
TH1=0xFD; //11.0592M.19200
TL1=0xFD;
TCON=0x55;//外中断0、1均为下降沿触发
IE=0xF3;//允许定时器0,外中断0中断,并打开总中断
}
//初始化
void initProg(void)
{
initInt();
//b485Send = 0;
}
void main(void)
{
initProg();
while(1)
{
timeProc();
checkComm0Modbus();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -