// CONSTANTS
#define START 1
#define STEP1 2
#define STEP2 3
#define STEP3 4
#define STEP4 5
#define STEP5 6
#define STEP6 7
#define TRUE 1
#define FALSE 0
//MODBUS FUNCTION CODES
#define FC_READ_COIL_STATUS 0x01
#define FC_READ_INPUT_STATUS 0x02
#define FC_FORCE_SINGLE_COIL 0x05
#define K_MAX_COMBUF 20
/* Table of CRC values for highorder byte */
const char HItbl[] = {
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, 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,
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, 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
} ;
/* Table of CRC values for loworder byte */
const char LOtbl[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
0x43, 0x83, 0x41, 0x81, 0x80, 0x40
} ;
sfr DI_mport0 at portA;
sfr DI_mport1 at portE;
char DO_mport1 at portD;
//char DO_mport at portC;
sbit DO_mport at RB2_bit;
unsigned char gc1,gc2,gc3,gc4;
unsigned char out1, out2,out3;
unsigned int gi1,gi2,gi3,gi4;
unsigned char crcLO, crcHI;
unsigned char my_add;
unsigned char combuf[20];
unsigned char combuf_count, combuf_index;
unsigned char di_port0, di_port1, do_port, do_port1;
unsigned char rtu_timeout;
unsigned char rx_state, tx_state;
//bit f_one_sec;
//bit f_delay_over;
bit f_pkt_ready, f_transmission_over;
bit f_dummy_pkt;
void com_receive(unsigned char sbuf);
void com_send(void);
void com_service(void);
void com_init(void);
void get_crc(unsigned char length);
unsigned char check_crc(void);
void scan_di(void);
void update_do(void);
void interrupt(){
unsigned char sbuf;
if(PIR1.TXIF){
com_send();
}
if (PIR1.RCIF){
sbuf = RCREG;
com_receive(sbuf);
//PIR1.RCIF = 0;
}
if(PIR1.TMR1IF){
TMR1H = 0x27;
TMR1L = 0x10;
if(rtu_timeout){
rtu_timeout--;
if(rtu_timeout == 0){
if(!f_dummy_pkt)f_pkt_ready = 1;
rx_state = START;
}
}
PIR1.TMR1IF = 0;
}
}
void com_send(void){
switch(tx_state){
case START:
if(combuf_count){
TXREG = combuf[combuf_index++];
if(combuf_index>=combuf_count)tx_state = STEP1;
}
break;
case STEP1:
f_transmission_over = 1;
combuf_count = 0;
tx_state = START;
break;
}
}
void com_receive(unsigned char scombuf){
switch(rx_state){
case START:
if(f_pkt_ready)break;
if(scombuf != my_add){
f_dummy_pkt = 1;
}
else f_dummy_pkt = 0;
rx_state = STEP1;
if(!f_dummy_pkt){
combuf[0] = scombuf;
}
combuf_count = 1;
rtu_timeout = 5;
break;
case STEP1:
if(!f_dummy_pkt){
if(combuf_count < K_MAX_COMBUF){
Combuf[combuf_count++] = scombuf;
}
}
rtu_timeout = 5;
break;
}
}
/*void update_in(void)
{
do_port = outdata;
}*/
void update_do(void)
{
DO_mport = do_port;
DO_mport1 = do_port1;
}
void scan_di(void)
{
di_port0 = DI_mport0;
di_port1 = DI_mport1;
}
void nw_send(unsigned char length){
get_crc(length);
combuf_index = 1;
combuf_count = length;
f_transmission_over=0;
Delay_ms(4); //Pkt start 3.5 char
//po_485_en=1;
//delayms(2); //Pkt start 3.5 char
tx_state = START;
f_pkt_ready=0;
TXREG = combuf[0];
while(!f_transmission_over);
//po_485_en = 0;
}
void main() {
INTCON.GIE = 1;
INTCON.PEIE = 1; //enabling global, pheriperal and timer0 overflow interrupts
SPBRG = 0x11; // Baud Rate 9600 using 11.0592 MHz (((11.0592 MHz/4)/16)/desiredbaudrate)-1
TXSTA = 0X20; // Transmit Enabled
RCSTA = 0X90; // Serial port enables, Continous Receive
TMR1H = 0x27;
TMR1L = 0x10;
//T0CON = 0X88; // TIMER0 ON, 16 BIT MODE, NO PRESCALAR
T1CON = 0X09; // TIMER1 ON, 16 BIT MODE, NO PRESCALAR
PIE1.RCIE = 1;
PIE1.TXIE = 1;
PIE1.TMR1IE = 1;
//PIE1 = 0x31; // Enabled serial receive, transmit and timer1 overflow interrupts
TRISA = 0xFF;
TRISC6_bit = 0;
TRISC7_bit = 1;
TRISB2_bit = 0;
TRISE = 0xFF;
//TRISC = 0x00;
com_init();
while(1){
my_add = 8;
com_service();
scan_di();
update_do();
};
}
void com_service(void){
if(!f_pkt_ready) // if data is not ready ; return to main program
return;
if(!check_crc())
{
f_pkt_ready = 0; // if data is not ready ; return to main program
return;
}
switch(combuf[1]){
case FC_READ_COIL_STATUS: // read modbus protocol pdf (monitoring digital input status)
gi1 = combuf[2];
gi1<<=8;
gi1+=combuf[3];
gi2 = combuf[4];
gi2<<=8;
gi2+=combuf[5];
if(gi1 > 15)break;
gi3 = do_port1;gi3<<=8;
gi3 +=do_port;
if(gi2 > 8) combuf[2]= 2;
else combuf[2]=1;
//gi3 >>= gi1; // doubt
combuf[3]=gi3;
if(gi2 > 8){
combuf[4]=(gi3>>8);
nw_send(5);
}
else nw_send(4);
break;
case FC_READ_INPUT_STATUS: // read modbus protocol pdf (monitoring digital input status)
gi1 = combuf[2];
gi1<<=8;
gi1+=combuf[3];
gi2 = combuf[4];
gi2<<=8;
gi2+=combuf[5];
if(gi1 > 15)break;
gi3 = di_port1;gi3<<=8;
gi3 +=di_port0;
if(gi2 > 8) combuf[2]= 2;
else combuf[2]=1;
//gi3 >>= gi1; //doubt
combuf[3]=gi3;
if(gi2 > 8){
combuf[4]=(gi3>>8); //
nw_send(5);
}
else nw_send(4);
break;
case FC_FORCE_SINGLE_COIL: //changing digital output state
gi1 = combuf[2];
out1 = gi1;
gi1<<=8;
gi1+=combuf[3]; // 0008
gi2 = combuf[4];
out2 = gi2;
gi2<<=8;
gi2+=combuf[5];
//if(gi1 == 0x05){
if(gi2 == 0xFF00){
do_port = 0xFF;
}
else { do_port = 0x00;
}
nw_send(6);
break;
f_pkt_ready = 0;
}
}
void com_init(void){
rx_state = tx_state = START; // used for internal program purpose
combuf_count = 0; // initially buffer count to 0
f_dummy_pkt = 0;
f_pkt_ready=0; // packet is not ready initially
f_transmission_over=1; // initialization over
}
unsigned char check_crc(void){
get_crc(combuf_count-2);
if(crcLO != combuf[combuf_count-1])return 0;
if(crcHI != combuf[combuf_count-2])return 0;
return 1;
}
void get_crc(unsigned char length){
unsigned char index,a;
crcHI = crcLO = 0xFF;
a=0;
while(length--){
index = (crcHI ^ combuf[a]);
a++;
crcHI = (crcLO ^ HItbl[index]);
crcLO = LOtbl[index];
};
}