SBAA456 August   2020  – MONTH  ADS7066

 

  1.   1
    1.     Example Code

Example Code

The following is a short software example of how to implement CRC for the ADS7066 device in the C language.

//*****************************************************************************
//! Calculates the 8-bit CRC for the selected CRC polynomial.
//! \fn uint8_t calculate CRC(const uint8_t dataBytes[], uint8_t numberBytes, uint8_t initial Value)
//! \param dataBytes[] pointer to first element in the data byte array
//! \param numberBytes number of bytes to be used in CRC calculation
//! \param initialValue the seed value (or partial crc calculation), use 0xFF when beginning a new CRC
computation
//! NOTE: This calculation is shown as an example and is not optimized for speed.
//! \return 8-bit calculated CRC word
//*****************************************************************************
uint8_t calculateCRC(const uint8_t dataBytes[], uint8_t numberBytes, uint8_t initialValue)
{
// Check that "dataBytes" is not a null pointer
assert(dataBytes != 0x00);
int bitIndex, byteIndex;
bool dataMSb; /* Most significant bit of data byte */
bool crcMSb; /* Most significant bit of crc byte */
// Initial value of crc register
// NOTE: The ADS7066 defaults to 0xFF,
// but can be set at function call to continue an on-going calculation
uint8_t crc = initialValue;
// ANSI CRC polynomial = x^8 + x^2 + x^1 + 1
const uint8_t poly = 0x07;
/* CRC algorithm */
// Loop through all bytes in the dataBytes[] array
for (byteIndex = 0; byteIndex < numberBytes; byteIndex++)
{
// Point to MSb in byte
bitIndex = 0x80u;
// Loop through all bits in the current byte
while (bitIndex > 0)
{
// Check MSB's of data and crc
dataMSb = (bool) (dataBytes[byteIndex] & bitIndex);
crcMSb = (bool) (crc & 0x80u);
www.ti.com
2 Implementation of CRC for ADS7066 SBAA456 – JULY 2020
Submit Document Feedback
Copyright © 2020 Texas Instruments Incorporated
crc <<= 1;
// Check if XOR operation of MSBs results in additional XOR operations
if (dataMSb ^ crcMSb)
{
crc ^= poly;
}
// Shift MSb pointer to the next data bit
bitIndex >>= 1;
}
}
return crc;
}