#include <stdio.h>
typedef unsigned char U8;
typedef unsigned int U16;
typedef unsigned long U32;
typedef signed char S8;
typedef signed int S16;
typedef signed long S32;
typedef unsigned char uint8;
typedef unsigned int uint16;
#define BUF_LEN 5
uint8 CommCalcCRC(uint8 *ptr, uint8 len)
{
uint8 i;
uint8 crc=0x7a;
while(len--)
{
crc ^= *ptr++;
for (i=8; i>0; --i)
{
if (crc & 0x80)
crc = (crc << 1) ^ 0x97;
else
crc = (crc << 1);
}
}
return (crc);
}
int main () {
uint8 i = 0;
uint8 UsartRecBuff[BUF_LEN] = {0x5A, 0x00, 0x00, 0x00, 0x00};
UsartRecBuff[BUF_LEN-1] = CommCalcCRC(&UsartRecBuff[0], BUF_LEN-1);
printf("DATA: ");
for (i = 0; i < BUF_LEN; i++)
{
if (0 == (UsartRecBuff[i] & 0xF0))
{
if (0 == (UsartRecBuff[i] & 0x0F)) printf("00 ");
else printf("0%x ", UsartRecBuff[i]);
}
else
{
printf("%x ", UsartRecBuff[i]);
}
}
printf("\n");
return 0;
}