#include
unsigned short CalculateCRC( unsigned char *buf, short num );
/**************** CalculateCRC ************************/
unsigned short CalculateCRC( unsigned char *buf, short num )
{
short i,j;
unsigned long sum;
unsigned short crc;
unsigned long polynomial = 0x01102100; //CCITT-16
// 0x01800500; //CRC-16
sum = 0;
for( i=0; i< num; i++)
{
sum |= *(buf+i);
for( j=0; j< 8; j++)
{
sum <<= 1;
if( sum & 0x01000000 )
sum ^= polynomial;
}
}
for( i=0; i< 2; i++) // last 2 byte zero
{
sum |= 0x00;
for( j=0; j< 8; j++)
{
sum <<= 1;
if( sum & 0x01000000 )
sum ^= polynomial;
}
}
crc = ( sum>>8 ) & 0x0000FFFF;
return( crc );
}