Hello Eneko,
I think you will need to change all references to unsigned int type to read unsigned char (including the function parameters).
Rather than calculating the CRC from a data table, for some applications it may be useful to update the CRC value "on the fly", perhaps within a SCI interrupt routine. The following code is intended to do this, but has not been tested. Of course, the function does not initialise crc value. It should be feasible to use the same function for both sending and receiving serial data packets.
/* Update 8-bit CRC value
using polynomial X^8 + X^5 + X^4 + 1 */
#define POLYVAL 0x8C
void update_crc(unsigned char new, unsigned char *crc)
{
unsigned char c, i;
c = *crc;
for (i = 0; i < 8; i++) {
if ((c ^ new) & 1) c = (c >> 1 ) ^ POLYVAL;
else c >>= 1;
new >>= 1;
}
*crc = c;
}
Regards,
Mac
Message Edited by bigmac on 05-14-200612:58 AM