⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc16lut.cpp

📁 ti-Chipcon CC1010 1G以下Soc源码库。包括rf,powermodes,clockmodes,flashRW,interrupts,timer,pwm,uart...所有底层驱动源码
💻 CPP
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                            CHIPCON CC1010                    *
 *      ***   + +   ***                  CRC16 Look Up Table                 *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 *                                                                           *
 *****************************************************************************
 * Author:              JOL                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: CRC16LUT.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 CRC16_POLY 0x1021

// CRC16 LUT
word crc16LUT[256];

//----------------------------------------------------------------------------
//	void CRC16InitTables(const char* fileName)
//
//	Description:
//		A helper function used to initialize the CRC look-up table used
//		by the CUL FAST_CRC16 macro. The function dumps the initialized tables 
//		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 createCrc16LutFile(char* fileName) {

    int i, j;
    word topByte, modReg;
    FILE *f;

	// Precalculate the LUTs.
    for (i = 0; i < 256; i++) {
        topByte = i << 8;
        modReg = 0;
        for (j = 0; j < 8; j++) {
            if ( (topByte ^ modReg) & 0x8000 ) {
                topByte <<= 1;
                modReg = (modReg<<1) ^ CRC16_POLY;
            } else {
                topByte <<= 1;
                modReg <<= 1;
            }
        }
        crc16LUT[i] = modReg;
    }
    
    // Write the tables to a file if desired
    if (!fileName)
        return;

    if ((f=fopen(fileName, "wt"))==NULL) {
        fprintf(stderr, "Unable to open file \"%s\" for writing.\n", fileName);
        return;
    }


    fprintf(f, "// CRC16 LUT (newCRC = (oldCRC << 8) ^ crc16LUT[((byte)(crcReg >> 8)) ^ dataByte])\n"
               "word code crc16LUT[256] = {\n    "
    );

    for (i=0; i<256; i++) {
        fprintf(f, "0x%04X%s", crc16LUT[i], (i==255) ? "\n};\n\n" : (i%8==7) ? ",\n    " : ", ");
    }

    fclose(f);
} // createCrc16LutFile



//----------------------------------------------------------------------------
//	MAIN PROGRAM
//----------------------------------------------------------------------------
int main(int argc, char* argv[]) {
    createCrc16LutFile("CRC16LUT.txt");
         
	return 0;
} // main

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -