📄 crc8lut.cpp
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 *
* *** + + *** CRC8 Look Up Table *
* *** +++ *** *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* *
*****************************************************************************
* Author: JOL *
*****************************************************************************
* Revision history: *
* *
* $Log: CRC8LUT.cpp,v $
* Revision 1.1 2002/10/14 12:26:36 tos
* Initial version in CVS.
*
* *
****************************************************************************/
#include "stdafx.h"
#include <stdio.h>
typedef unsigned char byte;
typedef unsigned short word;
#define CRC8_POLY 0x18
// CRC8 LUT
byte crc8LUT[256];
//----------------------------------------------------------------------------
// byte CRC8Small(byte crcData, byte crcReg)
//
// Description:
// A CRC-8 (DOW) implementation optimized for small code size.
// The function should be called once for each byte in the data
// the CRC is to be performed on. For the invocation on the first byte
// the value CRC8_INIT should be given for _crcReg_. The value returned
// is the CRC-8 of the data supplied so far. This CRC-value should be
// added at the end of the data to facilitate a later CRC check. During
// checking the check should be performed on all the data AND the CRC-16
// value appended to it. The data is intact if the value returned is 0.
//
// Arguments:
// byte crcData
// The data to perform the CRC-8 operation on.
// byte crcReg
// The current value of the CRC register. For the first byte the
// value CRC8_INIT should be supplied. For each additional byte the
// value returned for the last invocation should be supplied.
//
// Return value:
// The updated value of the CRC8 register. This corresponds to the
// CRC-8 of the data supplied so far. During CRC checking, after working
// through all the data and the appended CRC-8 value, the value will be
// 0 if the data is intact.
//----------------------------------------------------------------------------
byte CRC8Small(byte crcData, byte crcReg) {
int i;
for (i=0; i<8; i++) {
if ( (crcReg&0x80) ^ (crcData&0x80) )
crcReg=(crcReg<<1)^CRC8_POLY;
else
crcReg=(crcReg<<1);
crcData<<=1;
}
return crcReg;
} // CRC8Small
//----------------------------------------------------------------------------
// void createCrc8LutFile(const char* fileName)
//
// Description:
// A helper function used to initialize the CRC look-up table used
// by the CUL FAST_CRC8(...) macro. The function dumps the initialized
// table in C-syntax to a file so that they can be included in a source
// file.
//
// Arguments:
// const char* fileName
// The name of a file to receive the initialized tables in C-syntax
// or NULL to perform table initialization only.
//
// Return value:
// void
//----------------------------------------------------------------------------
void createCrc8LutFile(char* fileName) {
int i;
FILE *f;
// Precalculate the LUTs.
for (i = 0; i < 256; i++) {
crc8LUT[i] = CRC8Small(0, i);
}
// Write the tables to a file
if (!fileName) return;
if ((f = fopen(fileName, "wt")) == NULL) return;
fprintf(f, "// CRC8 LUT (newCRC = crc8LUT[dataByte ^ oldCRC])\nbyte code crc8LUT[256] = {\n ");
for (i=0; i<256; i++) {
fprintf(f, "0x%02X%s", crc8LUT[i], (i==255) ? "\n};\n\n" : (i%8==7) ? ",\n " : ", ");
}
fclose(f);
} // createCRC8LUT
//----------------------------------------------------------------------------
// MAIN PROGRAM
//----------------------------------------------------------------------------
int main(int argc, char* argv[]) {
createCrc8LutFile("CRC8LUT.txt");
return 0;
} // main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -