# CRC explanation and coding

Status
Not open for further replies.

#### kit_714

##### Member level 4
crc calculation explanation

Hi all,

i am currently studying the principle of CRC-8 since i am handling the temperature sensor from Maxim (DS18S20).

Can anyone kindly explain the principle of this checking system?

The polynomial of CRC is x^8 + x^5 + x^4 + 1

and also, i found a source code about generating CRC-8 as follows:

BYTE by_ACRC8 ( BYTE byIn , BYTE bycrc)
{
BYTE i ;
for ( i = 0 , i < 8 , i++)
{
if ( ( byIn & 1) ^ ( bycrc & 0x 80 ) == 0 )
{
bycrc <<= 1 ;
}
else
{
bycrc = bycrc ^0x 18 ;
bycrc <<=1 ;
bycrc | = 1 ;
}
bycrc >>= 1 ;
}
return bycrc ;
}

2 questions for the coding...

1. bycrc = bycrc ^0x 18 .....what;s the operation for this line of coding?
2. Can anyone briefly explain the coding with CRC-8 operation principles?

Kit

crc explanation

Pretty good explanation here;

https://en.wikipedia.org/wiki/Cyclic_redundancy_check

I use the following assembly language algorithm for PIC microcontrollers;

Code:
;******************************************************************
;
;  Mike McLaren's Dallas one-wire 8-bit CRC code for the DS18B20
;
;  clear the CRC variable then run each byte from the scratchpad
;  final CRC value of '0' indicates "good data".
;
;    void CRC_Calc ()
;    { crc ^= IOByte;
;      for (i = 0; i < 8; i++)
;      { if (crc & 0x01)
;          crc = (crc >> 1) ^ 0x8C;
;        else
;          crc =  crc >> 1;
;      }
;    }
;
;  entry: IOByte contains data byte
;   exit: CRC contains cumulative CRC data
;
CRC_Calc
movlw   d'8'            ;                                 |B0
movwf   BitCtr          ; setup bit counter               |B0
movf    owByte,W        ;                                 |B0
xorwf   CRC,F           ;                                 |B0
movlw   h'8C'           ; W = x8+x5+x4+1 polynomial       |B0
CRC_Next
clrc                    ;                                 |B0
rrf     CRC,F           ;                                 |B0
skpnc                   ;                                 |B0
xorwf   CRC,F           ; toggle b7, b3, and b2 bits      |B0
decfsz  BitCtr,F        ; all done? yes, skip, else       |B0
goto    CRC_Next        ; process the next IOByte bit     |B0
return                  ;                                 |B0
;

crc_calc( )+pic

Thanks...

However...one more question...

What is the logical operation of the following code?

crc ^= IOByte;

Kit

8 bit crc 8c dallas polynomial

That means : crc = crc XOR IOByte.

"^" is the symbol for logic xor in C language.

Regards,
Franck.

mike mclaren crc

Get it!

Thanks the help of you guys!

Kit

crc_calc()+pic

Here is the sample code i used for CRC calculation

#include <stdio.h>
#include <conio.h>
unsigned int checksum(unsigned char*,int);
void main()
{
unsigned int crc1=0;
int size1 =6;
unsigned char dpacket={1,3,0,1,0,0x0a};
clrscr();
crc1 = checksum(dpacket,size1);
printf("crc = %X",crc1);
getch();
}

unsigned int checksum(unsigned char* dpacket1,int size)
{
unsigned int i,j;
unsigned int crc = 0xffff;
unsigned int poly = 0xa001;
for(i=0;i<size;i++)
{
crc^= dpacket1;
for(j=0;j<8;j++)
{
if(crc & 0x01)
{
crc >>=1;
crc ^= poly;
}
else
crc >>= 1;
}
}
return crc;
}

Status
Not open for further replies.