CRC Library

Post your requests and ideas on the future development of mikroPascal for AVR.
Post Reply
Author
Message
pykedgew
Posts: 164
Joined: 10 Dec 2005 02:22
Location: Brisbane Australia

CRC Library

#1 Post by pykedgew » 07 Apr 2007 13:28

Hi

Is there any plan to make a new CRC Library?

It could include crc8 crc16 crc32 routines.

This could be very useful when using for communication.

This is only a suggestion, but it would be nice if it can be done.

Regards
Ken

User avatar
zristic
mikroElektronika team
Posts: 6608
Joined: 03 Aug 2004 12:59
Contact:

Re: CRC Library

#2 Post by zristic » 07 Apr 2007 13:52

There are many ways to do CRC, one of the possibilities is what MODBUS does:
http://www.modbus.org/ wrote: The CRC field checks the contents of the entire message. It is applied regardless of any parity checking method used for the
individual characters of the message.
The CRC field contains a 16–bit value implemented as two 8–bit bytes.
The CRC field is appended to the message as the last field in the message. When this is done, the low–order byte of the field is
appended first, followed by the high–order byte. The CRC high–order byte is the last byte to be sent in the message.
The CRC value is calculated by the sending device, which appends the CRC to the message. The receiving device recalculates a
CRC during receipt of the message, and compares the calculated value to the actual value it received in the CRC field. If the two
values are not equal, an error results.
The CRC calculation is started by first pre-loading a 16–bit register to all 1’s. Then a process begins of applying successive 8–bit
bytes of the message to the current contents of the register. Only the eight bits of data in each character are used for generating the
CRC. Start and stop bits and the parity bit, do not apply to the CRC.

During generation of the CRC, each 8–bit character is exclusive ORed with the register contents. Then the result is shifted in the
direction of the least significant bit (LSB), with a zero filled into the most significant bit (MSB) position. The LSB is extracted and
examined. If the LSB was a 1, the register is then exclusive ORed with a preset, fixed value. If the LSB was a 0, no exclusive OR takes
place.
This process is repeated until eight shifts have been performed. After the last (eight) shift, the next 8–bit byte is exclusive ORed with
the register’s current value, and the process repeats for eight more shifts as described above. The final content of the register, after all
the bytes of the message have been applied, is the CRC value.
Translated into mP it looks like this:

Code: Select all

unit uncrc;

implementation

// calculates 8 bit crc for 11 bytes of data
function CRC_Get(var data : array[11] of byte): byte;
var i, j: byte;
    LSB1 : byte;
    CRC1:word;
begin
  CRC1 := $FFFF;  //Initial value of CRC

  for i:=0 to 9 do  // calc CRC for the first 10 bytes of data
   begin
    CRC1 := CRC1 xor data[i];
    for j:=0 to 7 do // for each bit in data
     begin
      CRC1 := CRC1 shr 1;   //
      LSB1 := CRC1 and $01; // take carry bit
      if(LSB1 <> 0)then // if carry <> 0 then
        CRC1 := CRC1 xor $A001; // chose your constant here
     end;
   end;
   result := (CRC1 and 0xFF);  //return lower byte
end;

// check if CRC is correctly calculated
function CRC_Validate(var data: array[11] of byte; crc: byte):byte;
  var gen_crc:byte; 
begin
  gen_crc := CRC_Get(data); // calculate CRC
  if(gen_crc = crc) // compare
   then
     result := TRUE   // they are the same
   else
     result := FALSE;  // they are different
end;

end.
Now, you can expand this to work as CRC16 and CRC32.

pykedgew
Posts: 164
Joined: 10 Dec 2005 02:22
Location: Brisbane Australia

#3 Post by pykedgew » 08 Apr 2007 00:11

Hi zristic

Many thanks for the explanation(theory) and the code.

Cheers
Ken

Post Reply

Return to “mikroPascal for AVR Wish List”