I've been playing with a CRC routine I grabbed from an Arduino library, with a view to transferring data from Arduino to PIC, or PC to PIC etc. and adding a CRC to provide a little error checking, as it could be via wire, radio, or anything else - I also want to try and add it to PHP on a server, so I can check data coming from that.
However, while the routine compiles perfect under XC8, the output is completely different to the Arduino Uno, and often doesn't change as the numerical input does.
The outputs, using the base number 65123 are like this:
If you add a 1 to the front of the base number (165123), then each XC8 output is different, but none are the same as the Arduino output fed with 165123.
Any ideas why the PIC version doesn't work correctly?, are the bytes in the variable perhaps in a different order?, but that wouldn't explain the repeating unchanged output from 65123?.
However, while the routine compiles perfect under XC8, the output is completely different to the Arduino Uno, and often doesn't change as the numerical input does.
C:
// common subroutine
uint32_t crcCalc(uint8_t *buf, uint16_t bufSize)
{
const uint32_t crcTable[16] =
{
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
uint32_t crc = ~0L;
for (uint16_t index = 0 ; index < bufSize ; ++index)
{
crc = crcTable[(crc ^ buf[index]) & 0x0f] ^ (crc >> 4);
crc = crcTable[(crc ^ (buf[index] >> 4)) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return crc;
}
// Arduino test code
uint32_t tempnum = 65123;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
Serial.println("CRC32 Test");
}
// the loop routine runs over and over again forever:
void loop() {
Serial.println(tempnum);
Serial.println(crcCalc(tempnum, sizeof(tempnum)));
Serial.println(crcCalc(tempnum, sizeof(tempnum)), HEX);
Serial.println();
tempnum++;
delay(5000); // delay in between reads for stability
}
// XC8 test code
Serial_Println(" ");
Serial_Println("CRC32 test");
uint32_t testnum = 65123;
while(1)
{
lcdPrintNumber(testnum, DEC);
Serial_Println(" ");
lcdPrintNumber(crcCalc(testnum, sizeof(testnum)), DEC);
Serial_Println(" ");
lcdPrintNumber(crcCalc(testnum, sizeof(testnum)), HEX);
Serial_Println(" ");
Serial_Println(" ");
testnum++;
fiveSecDelay();
}
The outputs, using the base number 65123 are like this:
Code:
// Arduino output
CRC32 Test
65123
3546579263
D364813F
65124
1191761199
4708D52F
65125
4124761098
F5DADC0A
65126
2142370248
7FB1F9C8
65127
1772435312
69A53770
// XC8 output
CRC32 test
65123
1822041184
6C9A2460
65124
1822041184
6C9A2460
65125
1822041184
6C9A2460
65126
1822041184
6C9A2460
65127
1822041184
6C9A2460
If you add a 1 to the front of the base number (165123), then each XC8 output is different, but none are the same as the Arduino output fed with 165123.
Any ideas why the PIC version doesn't work correctly?, are the bytes in the variable perhaps in a different order?, but that wouldn't explain the repeating unchanged output from 65123?.