#include<reg52.h>
#include<absacc.h>
#include<intrins.h>
#include<ctype.h>
/* These macro define simple data type 宏定义简单的数据类型*/
#define uchar unsigned char
#define ulint unsigned long
#define uint unsigned int
/* These macro for Mudbus function code :mudbus 功能码 */
#define ReadCoilSta 0x01 // 0X read coil function 读线圈
#define ForceSingleCoil 0x05 // 0X force single coil function写线圈
#define ReadHoldReg 0x03 // 4X read input register 读寄存器
#define PresetMulReg 0x06 // 4X write multi register 写寄存器
/* These macro describe index of communiction message from start to end */
#define CommIndexSlaveID 0 // macro for slave ID 站号
#define CommIndexFunction 1 // macro for function code 宏的功能码
#define CommIndexStartAdrHi 2 // macro for data hight adr 地址高位
#define CommIndexStartAdrLo 3 // macro for data low adr 地址地位
#define CommIndexNoPointHi 4 // macro for hight quantity datas 数据高位
#define CommIndexNoPointLo 5 // macro for low quantity datas 数据低位
/* These macro describe recieve message lenght of different function code */
/* But not include data lenght ,the data is master write to slave */
/* because we don't know the data lenght prior, it depend on ComBuf[ComIndexNoPoint] value */
#define ReadCoilStaIndexEnd 7 // 0~7,0x01,0X
#define ForceSingleCoilIndexEnd 7 // 0~7,0x05,0X
#define ReadHoldRegIndexEnd 7 // 0~7,0x03,4X
#define PresetMulRegIndexEnd 7 // 0~8,0x10,4X
/* these macro describe send or recieve allowed */
#define SEND 1 // send allowed 允许发送
#define RECIEVE 0 // recieve allowed 允许接收
#define SlaveID 0x01 // the slave's ID
#define SendRecieveDelay 50 // Send turn to Recieve or Recieve turn to Send delay time vlaue 发送与接收转换
时间
#define MaxDataLen 30 // preset CommBuf array Max length 通讯缓冲区 max 长度
#define MaxRegLen 20 // preset SlaveOutputBuf Max lenght
/* variable for modbus is following 变量定义*/
bit data RecFinishF;
uchar data CommIndex;
uchar data writeadr;
uchar data CommIndexEnd;
uchar idata CommBuf[MaxDataLen];
uchar idata SlaveOutputBuf[MaxRegLen]; // Hight 8 bit of word is front,Low 8 bit is back高 8 位在前低 8 位在后
uchar idata SlaveInputBuf[MaxRegLen];
uchar data *port _at_ 0x7F ;
/* exp data define */
//#define SlaveAdr XBYTE [0xD800]
sbit LED0=P1^0;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
sbit LED4=P1^4;
sbit LED5=P1^5;
sbit LED6=P1^6;
sbit LED7=P1^7;
/*后加的 P0*/
sbit LED00=P0^0;
sbit LED01=P0^1;
sbit LED02=P0^2;
sbit LED03=P0^3;
sbit LED04=P0^4;
sbit LED05=P0^5;
sbit LED06=P0^6;
sbit LED07=P0^7;
uchar idata ForceCoilBuf[10];
uchar count=0;
uchar forceval=0x00;
//bit forcesendF=0;
/* Table Of CRC Values for high-order byte CRC 校验高位*/
uchar code auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,