#include <stdio.h>
unsigned char CRC8(unsigned char *u8_data,unsigned char u8_len)
{
unsigned char i, j;
unsigned char u8_crc8;
unsigned char u8_poly;
u8_crc8 = 0x00;
u8_poly = 0x1D;
for (i = 0; i < u8_len; i++)
{
u8_crc8 ^= u8_data[i];
for (j = 0; j < 8; j++)
{
if (u8_crc8 & 0x80)
{
u8_crc8 = (u8_crc8 << 1) ^ u8_poly;
}
else
{
u8_crc8 <<= 1;
}
}
}
u8_crc8 ^= (unsigned char)0x00;
return u8_crc8;
}
int main () {
unsigned char crc_byte,crc_byte1,crc_byte2;
unsigned char buf_byte[8]={0x3e,0x87,0x00,0x01,0x00,0x00,0x00,0x00};
unsigned short seedlength=8;
unsigned char buf_byte1[8]={0x21,0x34,0x00,0x01,0xff,0xff,0xff,0xff};
unsigned short seedlength1=8;
unsigned char buf_byte2[8]={0x2A,0x34,0x00,0x01,0xff,0xf2,0x10,0x83};
unsigned short seedlength2=8;
crc_byte = CRC8(buf_byte,seedlength);
crc_byte1 = CRC8(buf_byte1,seedlength1);
crc_byte2 = CRC8(buf_byte2,seedlength2);
printf("\r\nCRC8_SA=%02X\r\n", crc_byte);
printf("\r\nCRC8_SA=%x\r\n", crc_byte1);
printf("\r\nCRC8_SA=%x\r\n", crc_byte2);
return 0;
}