Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Cyclic Redundancy Check (CRC8 Calculation)

2020-07-01
Support Note SN-IMC-1-036

Author(s) Himmel, Steve


Restrictions Public Document

Table of Contents
1 Description ....................................................................................................................................... 2
2 Disclaimer ........................................................................................................................................ 2
3 Requirements................................................................................................................................... 2
4 Source Code .................................................................................................................................... 2
4.1 Calc_CRC ................................................................................................................................ 2
4.2 Check_CRC ............................................................................................................................. 3
4.3 Output-Function ....................................................................................................................... 4
5 Handling ........................................................................................................................................... 4
5.1 Theory ...................................................................................................................................... 4
5.1.1 Figures ........................................................................................................................... 5
6 Contacts ........................................................................................................................................... 5
Cyclic Redundancy Check (CRC8 Calculation)

1 Description
Computation of an 8-bit binary CRC for error detection to accidental changes to raw data. For every
measurement point in the measurement file a check value, based on the remainder of a polynomial
division is computed and returned by the function. The hits will be generated when an error occurred!

2 Disclaimer
Permission is hereby granted, free of charge, to any person obtaining a copy of this function and/or
script and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

3 Requirements
Be sure the generator polynomial used in both functions doesn't differ. Access to raw data only with
the .BLF format.

4 Source Code
4.1 Calc_CRC
/*---------------------------------------------------------------------------
| Name: Calc_CRC
|
| Description: Computation of an 8-bit binary CRC for Error Detection to
| accidental changes to raw data.
| For every measurement point in the measurement file a check
| value, based on the remainder of a polynomial division is
| computed and returned by the function.
| Hits will be generated when an error occurred!
|
| Arguments: ----
|
| Glob. Variables: ----
| Functions: The return-value is used as an input of the function "checkCRC",
| which computes the checksum
|
| Attention: Be sure the generator polynomial used in both functions, doesn't
| differ. Access to raw data only with the .blf format.
|
| Execution: Data Mining
|
| Developed by: Vector / Steve Himmel
| --------------------------------------------------------------------------*/
function calcCRC (byte0)//, byte1, byte2, byte3, byte5, byte6, byte7)
{
#define CRCMASK 0xd5; //generator polynomial
unsigned char reg; //shift register
unsigned char data=0;
int i;

Copyright © 2020 – Vector Informatik GmbH 2


Contact Information: www.vector.com or +49-711-80 670-0
Cyclic Redundancy Check (CRC8 Calculation)

data=byte0;

reg = data ^ CRCMASK;

for(i=0; i<7; i++)


{
data <<= 1;
if(reg&0x80)
{
reg ^= CRCMASK;
}
reg <<= 1;
}
return reg;
}

4.2 Check_CRC
/*---------------------------------------------------------------------------
| Name: Check_CRC
|
| Description: Computation of an 8-bit binary CRC for Error Detection to
| accidental changes to raw data.
| For every measurement point in the measurement file a check
| value, based on the remainder of a polynomial division is
| computed and returned by the function.
| Hits will be generated when an error occurred!
|
| Arguments: Result from calcCRC
|
| Glob. Variables: ----
| Functions: calcCRC
|
| Attention: Be sure the generator polynomial used in both functions, doesn't
| differ.
| Access to raw data only with the .blf format.
|
| Execution: Data Mining
|
| Developed by: Vector / Steve Himmel
--------------------------------------------------------------------------*/

function checkCRC(byte0, crc)


{
#define CRCMASK 0xd5;
unsigned char reg;
unsigned char data;
int i;

data = byte0;
reg = byte0 ^ CRCMASK;

for(i=0;i<7;++i)
{
data <<= 1;
if(reg&0x80)
{
reg ^= CRCMASK;
}
reg <<= 1;

if(crc & (1<<i))


reg |= 1;
else
reg &= 0;
}
return 1;
}

Copyright © 2020 – Vector Informatik GmbH 3


Contact Information: www.vector.com or +49-711-80 670-0
Cyclic Redundancy Check (CRC8 Calculation)

4.3 Output-Function
/*---------------------------------------------------------------------------
| Name: test
|
| Description: Output, Handling for generated hits
|
| Arguments: ----
|
| Glob. Variables: ----
| Functions: The return-value is used as an input of the function "checkCRC",
| which computes the checksum
|
| Attention: Be sure the generator polynomial used in both functions, doesn't
| differ.
| Access to raw data only with the .blf format.
|
| Execution: Data Mining
|
| Developed by: Vector / Steve Himmel
|
--------------------------------------------------------------------------*/

function test (crc_checksum)


{
if((crc_checksum) ? 1 : 0)
{
Write("CRC_Sequence OK");
return 0;
}
else
{
Write("CRC ERROR");
return 1;
}

5 Handling
5.1 Theory

A generator polynomial with r digits leads to r-1 = n digits to fill up in the frame.

1101100000 (data + added zeros = frame)


110101
------
0000110000
110101
------
101 (remainder)
---------------------------------------
1101100101 (data + added remainder)
110101
------
110101
110101
------
000000 (checksum = 0 --> no errors occured)
(checksum != 0 --> errors occured)

Copyright © 2020 – Vector Informatik GmbH 4


Contact Information: www.vector.com or +49-711-80 670-0
Cyclic Redundancy Check (CRC8 Calculation)

5.1.1 Figures

Figure 1 – Data Mining setup

6 Contacts
For support related questions please address to the support contact for your country
https://www.vector.com/int/en/company/contacts/support-contact/.

Copyright © 2020 – Vector Informatik GmbH 5


Contact Information: www.vector.com or +49-711-80 670-0

You might also like