CRC 8 bits

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

CRC 8 bits

49,178 次查看
Eneko
Contributor II
Hello:
 
I work with HCS08AW micro and I need to develop a 8bit cyclic redundancy control routine. I know that there are 2 wave to do that. One is making the software routine to check all the values and the second one is using a given table depending of the choosen polynomic.
 
I would like to develop this second wave in c lenguaje but I don´t know where start.
 
Can anybody help me? Has anybody developed a CRC 8 bit routine with this table system?
 
let me know.
 
Regards
 
Eneko
标签 (1)
标记 (1)
11 回复数

6,270 次查看
noodle7
Contributor I
Eneko,

Theory:
Be sure to check out the EXCELLENT article on CRCs found everywhere on the internet:
A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS
by Ross Williams.

Practice:
Here the table driven crc-8 we use. Not sure what the polynomial is.
First is the table:

const int8 crc8_Table[ ] =
{
     0,  94, 188, 226,  97,  63, 221, 131, 194, 156, 126,  32, 163, 253,  31,  65,
   157, 195,  33, 127, 252, 162,  64,  30,  95,   1, 227, 189,  62,  96, 130, 220,
    35, 125, 159, 193,  66,  28, 254, 160, 225, 191,  93,   3, 128, 222,  60,  98,
   190, 224,   2,  92, 223, 129,  99,  61, 124,  34, 192, 158,  29,  67, 161, 255,
    70,  24, 250, 164,  39, 121, 155, 197, 132, 218,  56, 102, 229, 187,  89,   7,
   219, 133, 103,  57, 186, 228,   6,  88,  25,  71, 165, 251, 120,  38, 196, 154,
   101,  59, 217, 135,   4,  90, 184, 230, 167, 249,  27,  69, 198, 152, 122,  36,
   248, 166,  68,  26, 153, 199,  37, 123,  58, 100, 134, 216,  91,   5, 231, 185,
   140, 210,  48, 110, 237, 179,  81,  15,  78,  16, 242, 172,  47, 113, 147, 205,
    17,  79, 173, 243, 112,  46, 204, 146, 211, 141, 111,  49, 178, 236,  14,  80,
   175, 241,  19,  77, 206, 144, 114,  44, 109,  51, 209, 143,  12,  82, 176, 238,
    50, 108, 142, 208,  83,  13, 239, 177, 240, 174,  76,  18, 145, 207,  45, 115,
   202, 148, 118,  40, 171, 245,  23,  73,   8,  86, 180, 234, 105,  55, 213, 139,
    87,   9, 235, 181,  54, 104, 138, 212, 149, 203,  41, 119, 244, 170,  72,  22,
   233, 183,  85,  11, 136, 214,  52, 106,  43, 117, 151, 201,  74,  20, 246, 168,
   116,  42, 200, 150,  21,  75, 169, 247, 182, 232,  10,  84, 215, 137, 107,  53
} ;

To use the table:

crc = initial_Value ;
// The initial value is usually zero, but non-zero values are better.
// You can pick any byte you want, just be sure it's the same every time.

for ( each byte_Value in the message or file )
{
   crc = crc8_Table[ crc ^ byte_Value ] ;
}

// Some CRC implementations xor the final value with some other value.
crc = crc ^ xor_Value

Good luck,
Noodle7

6,270 次查看
sunil_linus
Contributor I

Hello Noodle7,

 

I am also looking for 8-bit CRC.

How to generate the look up table, that you have used in the code given.

 

thank you.

 

Regards

Sunil

0 项奖励
回复

6,270 次查看
Eneko
Contributor II

Hello:

 

Attached you´ll find the examples that we used to develop the CRC. I hope this will be helpfull for you.

 

EM

0 项奖励
回复

6,270 次查看
UK_CF_FAE
NXP Employee
NXP Employee
Hello Eneko,
 
Not a table-based CRC and not a 8-bit CRC, but here is an example:
Code:
/* calculates 16-bit CRC of given data *//* based on the polynomial x^16+x^15+x^2+1 */unsigned int calc_crc_core(unsigned int *start,unsigned int *end) { unsigned int crc=0,c; int i; while (start<=end) {  c=*start;  for(i=0;i<16;i++) {   if((crc ^ c) & 1) crc=(crc>>1)^0xA001;   else crc>>=1;   c>>=1;  }  start++; } return(crc);}

 
(Alban: code format)

Message Edited by Alban on 05-11-2006 10:32 AM

0 项奖励
回复

6,270 次查看
Eneko
Contributor II

Hello:

Starting with your code, please, let me know if my code would be o.k.

/* calculates 8-bit CRC of given data */
/* based on the polynomial  X^8 + X^5 + X^4 + 1 */
unsigned int calc_crc_core(unsigned int *start,unsigned int *end) {
 unsigned int crc=0,c;
 int i;
 while (start<=end) {
 c=*start;
 for(i=0;i<8;i++) {
  if((crc ^ c) & 1) crc=(crc>>1)^0x8c;
  else crc>>=1;
  c>>=1;
 }
 start++;
 }
 return(crc);
}

Regards

Eneko

0 项奖励
回复

6,270 次查看
bigmac
Specialist III

Hello Eneko,

I think you will need to change all references to  unsigned int  type to read  unsigned char (including the function parameters).

Rather than calculating the CRC from a data table, for some applications it may be useful to update the CRC value "on the fly", perhaps within a SCI interrupt routine.  The following code is intended to do this, but has not been tested.  Of course, the function does not initialise crc value.  It should be feasible to use the same function for both sending and receiving serial data packets.

/* Update 8-bit CRC value
  using polynomial  X^8 + X^5 + X^4 + 1 */

#define POLYVAL 0x8C

void update_crc(unsigned char new, unsigned char *crc)
{
  unsigned char c, i;

  c = *crc;
  for (i = 0; i < 8; i++) {
     if ((c ^ new) & 1) c = (c >> 1 ) ^ POLYVAL;
     else c >>= 1;
     new >>= 1;
  }
 *crc = c;
}

Regards,
Mac

 

Message Edited by bigmac on 05-14-200612:58 AM

0 项奖励
回复

6,270 次查看
Tess
Contributor I
Hi Mac,
 
I'm just curious why the Poly=x^8 + x^5 + x^4 +1 will become 0x8C?
Shouldn't it be 0x131/256 = 0x31?
 
Gary
p.s. Do you know if there's any other 8-bit Poly G that are standard or popular?
0 项奖励
回复

6,270 次查看
bigmac
Specialist III
Hello Gary,
 
I am not an expert on the associated mathematics, but here is my take on the process -
 
With the polynomial X^8 + X^5 + X^4 + 1 (or X^0), each bit of the data byte is tested, starting with the bit-0 position.  This is effectively the X^8 term of the polynomial.  Only if the modulo-two addition of this bit with the same bit position of the current CRC value is 1, is the CRC value modified by the remainder of the polynomial (X^5 + X^4 +  X^0).  However, because we test the bit-0 position, we shift to the CRC value to the right prior to each modulo-two addition (exclusive-OR).  Therefore, the bit order of the polynomial remainder byte needs to be reversed,
i.e. 10001100 = 0x8C
     01234567  
 
I hope this expanation make sense.
 
Regards,
Mac
 

Message Edited by bigmac on 2007-02-1206:02 PM

0 项奖励
回复

6,271 次查看
Eneko
Contributor II
Hello Mac:
 
See enclosed a information that may be interesting for you.
 
Regards
 
Eneko
0 项奖励
回复

6,271 次查看
bigmac
Specialist III

Hello Eniko,

It should be relatively easy to adapt the code supplied by UK_CF_FAE for other polynomials, including 8-bit.  Do you know which polynomial you wish to use for your application?

The method shown will consume much less code space than the look-up table method, but will require longer execution time.

Regards,
Mac

0 项奖励
回复

6,271 次查看
Eneko
Contributor II

Hi Mac:

I have choosen a standar one.  X^8 + X^5 + X^4 + 1 . Can you tell me how can I built my CRC code 8 bit ?

Thanks in advance.

Eneko

 

 

0 项奖励
回复