Hello all. I'm not a C coder at all. This is a short C routine that
Calculates a CRC16. The Poly and Seed are correct for what I need.
Could someone help me out converting this to PB. Thanks
------------------
Calculates a CRC16. The Poly and Seed are correct for what I need.
Could someone help me out converting this to PB. Thanks
Code:
word get_crc(ubyte *bufptr,word len) { register unsigned int newCRC; //Put the current byte in here. ubyte data; int bit_count; //This seed makes the output of this shift based algorithm match //the table based algorithm. The center 16 bits of the 32-bit //"newCRC" are used for the CRC. The MSb of the lower byte is used //to see what bit was shifted out of the center 16 bit CRC //accumulator ("carry flag analog"); newCRC=0x00F32100; while(len--) { //Get the next byte in the stream. data=*bufptr++; //Push this byte’s bits through a software //implementation of a hardware shift & xor. for(bit_count=0;bit_count<=7;bit_count++) { //Shift the CRC accumulator newCRC>>=1; //The new MSB of the CRC accumulator comes //from the LSB of the current data byte. if(data&0x01) newCRC|=0x00800000; //If the low bit of the current CRC accumulator was set //before the shift, then we need to XOR the accumulator //with the polynomial (center 16 bits of 0x00840800) if(newCRC&0x00000080) newCRC^=0x00840800; //Shift the data byte to put the next bit of the stream //into position 0. data>>=1; } } //All the data has been done. Do 16 more bits of 0 data. for(bit_count=0;bit_count<=15;bit_count++) { //Shift the CRC accumulator newCRC>>=1; //If the low bit of the current CRC accumulator was set //before the shift we need to XOR the accumulator with //0x00840800. if(newCRC&0x00000080) newCRC^=0x00840800; } //Return the center 16 bits, making this CRC match the one’s //complement that is sent in the packet. return((~newCRC)>>8); }
Comment