I am using MSP430F5325 in my hardware for MODBUS communication. My hardware is MOSBUS RTU slave. For MODBUS communication , I need to use CRC16 algorithm for checking errors in transmission/reception.
Following is the algorithm in C:
This is proven code for crc16 and it works absolutely fine in C on PC.Even it worked well with another micon hardware as a slave.
In order to implement this , I am using CRC module of MSP430F5325. However, my crc output from CRC module does not match with the above code writen in C. The Master RTU issues CRC error due this mismatch.
Below is my code in CCS Ver 5.3
//////////////////code////////////////////////
unsigned char rx_frame[8]; //array for storing the query received from Master RTU
after detecting end of frame, i store the received CRC from rx_frame buffer to temp variables and I calculate crc from first 6 recieved bytes and compare them with the received one.
The received Crc and calculated Crc should match for valid transmission and reception.
////////////code is as below://///////
received_crc_low=rx_frame[6];
received_crc_high=rx_frame[7];
unsigned cal_received_crcL,calc_received_crcH;
//////////////calculate crc of received frame
A) without reversing bits:
CRCINIRES=0xFFFF; // initialize
int zx;
for (zx=0;zx<6;zx++)
{
CRCDI_L=rx_frame[zx]; //8 byte
}
cal_received_crcL=CRCINIRES_L; //result lowbyte
calc_received_crcH=CRCINIRES_H; //result Highbyte
// compare "calculated Crclowbyte" with " received crc_lowbyte" and "calculated CrcHighbyte" with " received crc_Highbyte"
CRcs are not matching
///////////////////////////////////////////
B) with reversing bits:
CRCINIRES=0xFFFF; // initialize
int zx;
for (zx=0;zx<6;zx++)
{
CRCDIRB_L=rx_frame[zx];
}
cal_received_crcL=CRCRESR_L;
cal_received_crcH=CRCRESR_H;
// compare "calculated Crclowbyte" with " received crc_lowbyte" and "calculated CrcHighbyte" with " received crc_Highbyte"
Crcs are not matching
Please evaluarte the code and help understand what is going wrong.