calculatecrc.c
来自「Atmega8L控制CC1020 TI的另外一款使用很多的RF IC。」· C语言 代码 · 共 112 行
C
112 行
/*
demonstrates how the incorrect check value of 0x29B1 may be reported
for the test string "123456789" when it should be 0xE5CC.
*/
#include <stdio.h>
#include <string.h>
#define poly 0x1021 /* CRC-CCITT mask x^16 + x^12 +x^5 + 1 */
/* global variables */
/*char text[2];*/
unsigned int good_crc;
//unsigned char text_length = 9
void update_good_crc(unsigned int ch)
{
unsigned int v;
unsigned char i,xor_flag;
/*
Align test bit with leftmost bit of the message byte.
*/
v = 0x80;
for (i=0; i<8; i++)
{
if (good_crc & 0x8000)
{
xor_flag= 1;
}
else
{
xor_flag= 0;
}
good_crc = good_crc << 1;
if (ch & v)
{
/*
Append next bit of message to end of CRC if it is not zero.
The zero bit placed there by the shift above need not be
changed if the next bit of the message is zero.
*/
good_crc= good_crc + 1;
}
if (xor_flag)
{
good_crc = good_crc ^ poly;
}
/*
Align test bit with next bit of the message byte.
*/
v = v >> 1;
}
}
void augment_message_for_good_crc()
{
unsigned char i, xor_flag;
for (i=0; i<16; i++)
{
if (good_crc & 0x8000)
{
xor_flag= 1;
}
else
{
xor_flag= 0;
}
good_crc = good_crc << 1;
if (xor_flag)
{
good_crc = good_crc ^ poly;
}
}
}
/*****************************************************************************************
Parameters: text---->point to the start of the string that is going to be CRC calculated.
Text_length-----> the lenth of the string that you want to calculate
return value: 16 bit CRC value
******************************************************************************************/
unsigned int Calcu_CRC(unsigned char * text,unsigned char text_length)
{
// unsigned int ch;
unsigned char i;
good_crc = 0xffff;
/* while((ch=text[i])!=0) */
for (i=0; i<text_length; i++)
{
update_good_crc(text[i]);
}
augment_message_for_good_crc();
return(good_crc);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?