Betriebsanleitung
NAV300
8012527/TB56/2009-07-20
Anhang
*/
PUBLIC WORD crc16_word
(
WORD data,
WORD initial_crc
)
{
register WORD crc = initial_crc;
crc = ( (crc << 8) | ((BYTE)( data >> 8 ) ) ) ^ crctab[crc>>8];
crc = ( (crc << 8) | ((BYTE) data ))
return crc;
}
//
===================================================================
// local scope function definitions (type modifier: PRIVATE)
//
===================================================================
// EOF crc16c.c
Example C code to generate the CRC table used in the example above:
#include <stdio.h>
#define CRC_POLY 0x1021
typedef unsigned short WORD;
WORD get_crctab_val
(
int idx
)
{
WORD value;
WORD old_val;
int k;
value = ( (WORD) idx ) << 8;
for( k=0; k<8; k++ )
{
old_val = value;
value <<= 1;
if( old_val & 0x8000 ) value ^= CRC_POLY;
}
return value;
}
void main( void )
{
FILE *out;
WORD value;
int k, i;
out = fopen( "crctab.c", "wt" );
if( out == NULL )
{
puts( "\ncannot generate crctab.c !!\n\n" );
return;
}
fprintf( out, "// put header here\n\n" );
fprintf( out, "#include \"cpu-dep.h\"\n\n" );
fprintf( out, "// XOR table for CRC algorithm, CRC-16, ITU.T X.25\n" );
fprintf( out, "// polynomial: h%4x\n\n", CRC_POLY );
fprintf( out, "const WORD crctab[256] = \n" );
© SICK AG · Division Auto Ident · Germany · All rights reserved
^ crctab[crc>>8];
Kapitel 10
69