Appendix a, Appendix a. crc calculation – Rainbow Electronics AT42QT1110-AZ User Manual

Page 46

Advertising
background image

46

9570H–AT42–02/10

AT42QT1110-MZ/AT42QT1110-AZ

Appendix A.

CRC Calculation

If the use of a cyclic redundancy check (CRC) during data transmission is enabled, the host
must generate a valid CRC so that this can be correctly compared to the corresponding CRC
generated by the QT1110. This appendix gives example C code to show how the CRC can be
generated by the host.

/*=======================================================================

unsigned char calc_crc(unsigned char crc, unsigned char data)

---------------------------------------------------------------------------

Purpose: Calculate CRC for data packets

Input : CRC, Data

Output : Updated CRC

Notes : -

=========================================================================*/

unsigned char calc_crc(unsigned char crc, unsigned char data)

{

unsigned char index;

unsigned char fb;

index = 8;

do

{

fb = (crc ^ data) & 0x01u;

data >>= 1u;

crc >>= 1u;

if(fb)

{

crc ^= 0x8c;

}

} while(--index);

return crc;

}

/* Example Calling Routine */

unsigned char calculate_config_checksum(void)

{

int i;

unsigned char CRC_val = 0;

unsigned char setup_data[42] =

{

0xB2, 0x00, 0x38, 0x12, 0x06, 0x06, 0x12, 0x07, 0xFF, 0x80,

0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x32, 0xFF, 0x00, 0x29,

0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,

0X00, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A, 0x7A,

0x7A, 0x7A

};

for(i = 0; i < sizeof(setup_data); i++)

{

CRC_val = calc_crc(CRC_val, setup_data[i]);

}

return(CRC_val);

}

Advertising