本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
51单片机的CRC程序,此程序是通过查表的办法进行计算,对于51单片机相当适用
-
- #define BYTE_BITS 8 // Number of bits in byte<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
- #define TABLE_SIZE 1<<BYTE_BITS // Size of table
- #define WIDTH 16 // width of poly(number of most significnt bit)
- #define MSB_MASK 1<<(WIDTH-1) // Mask for high order bit in word
- #define POLY 0x1021 // Ploy. Bit #16 is set and hidde
- typedef unsigned short Crc16;
- Crc16 table[TABLE_SIZE];
- void crc16init()
- {
- Crc16 i, val,t1,t2;
- int j;
- for(i=0;i<TABLE_SIZE;++i)
- {
- val=i<<(WIDTH-BYTE_BITS);
- for(j=0;j<BYTE_BITS;++j)
- {
- t1= val<<1;
- t2=((val&MSB_MASK) ? POLY : 0);
- val=t1^t2;
- }
- table[i]=val;
- }
- }
- Crc16 crc16(Crc16 crc, void const* src, int cnt)
- {
- unsigned char const* s=(unsigned char const*)src;
- while(cnt--)
- crc=(crc<<BYTE_BITS)^table[(crc>>(WIDTH - BYTE_BITS)) ^ *s++];
- return crc;
- }
- void main()
- {
- unsigned char data[12]={0x54, 0xD3, 0x07, 0x0C, 0x17, 0x45, 0x0B,0x0E ,0x19,0,0};
- Crc16 result;
- crc16init();
- result=crc16(0,&data[0],9);
- data[9]=(result>>8)&0xff;
- data[10]=result&0xff;
- result=crc16(0,&data[0],11);
- }<!--EndFragment-->
复制代码 |