I'm trying to calculate a crc modbus in an application for PIC, but the crc returned is always incorrect compared to simulators online .. follow the code
void CRC (unsigned char * msg, unsigned char * out)
{
// char CRC16 [2] = {0xFF, 0xFF};
unsigned int CRC16 = 0xffff;
unsigned int poly = 0xA001;
unsigned char data [14];
unsigned char crc [2];
for (int i = 0; i <14; i ++)
{
CRC16 = (unsigned int) msg [i] ^ CRC16; // change date to msg
for (int j = 0; j <8; j ++)
{
CRC16 >> = 1;
if (CRC16 & 0x0001 == 1)
{
CRC16 = poly;
}
}
}
crc [0] = CRC16 >> 8;
crc [1] = CRC16 & lt; / RTI & gt;
strcpy (data, msg);
strcat (data, crc);
strcpy (out, date);
}
I enter with a buffer of 12 bytes for the calculation and in the end should get the buffer next to the crc .. but the calculation of crc itself is returning the wrong value .. what could be wrong in the code?
in case my message should return 8C0C
but returns 68FE