- 22 -
Reference: CRC-16 calculation program
/***** CRC-16 calculation program (C language) *****/
#include <stdio.h>
#include <conio.h>
void main(void)
{
/*** Internal change declaration ***/
unsigned int iLoopCnt; /* Loop counter */
unsigned short usData; /* Input data */
unsigned short usCrcData; /* CRC-16 data */
unsigned short usErrChkData; /* Error check data */
int iDummy; /* Dummy variable */
/* Initialize the output result of CRC-16 data */
usCrcData = 0xffff;
printf“Enter hexadecimal data.(End using [q]) >\n”);;
while( scanf(“%x”,&usData) != 0 )
{
/* Get the exclusion of CRC output result and the data that is input */
usCrcData = usData ^ usCrcData;
/*** Do the CRC calculation ***/
/* Repeat till shifting up to 8 bits is done */
for( iLoopCnt = 0 ; iLoopCnt < 8 ; iLoopCnt++ )
{
/* Check the existence of carry */
if( usCrcData & 0x0001 )
{
/* When carry occurs */
/* Shift CRC output result 1 bit to the right */
usCrcData = usCrcData >> 1;
/* Get the exclusion with A001H */
usCrcData = usCrcData ^ 0xa001;
}
else
/* When carry does not occur */
/* Shift CRC output result 1 bit to the right */
usCrcData = usCrcData >> 1;
} /* for */
} /* while */
printf( “CRC-16 data is %xH..\n”, usCrcData );
/* Create error check data */
usErrChkData = ( usCrcData >> 8) | ( usCrcData << 8 );
printf( “Data for error check is %xH.”, usErrChkData );
iDummy = getch();
}