Tool/software: Code Composer Studio
Dear expert,
My customer has some questions about BQ76940 CRC example code. Please see the blue bold part below, there will be a direct calculation relationship between CRC and ptr. Does the red bold part as below have the same function as the blue bold part below? (In this CRC example code, the crc is operating independently, and I can't find the XOR relationship between crc and the data to be sent ptr, or is it something I misunderstand?) Thanks for your support.
- CRC Example code:
unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key)
{
unsigned char i;
unsigned char crc=0;
while(len--!=0)
{
for(i=0x80; i!=0; i/=2)
{
if((crc & 0x80) != 0)
{
crc *= 2;
crc ^= key;
}
else
crc *= 2;
if((*ptr & i)!=0) //
crc ^= key;
}
ptr++;
}
return(crc);
}
- Normal CRC dode:
unsigned char CRC8(unsigned char *ptr, unsigned char len,unsigned char key)
{
unsigned char i;
unsigned char crc=0;
while(len--)
{
crc ^= *ptr++; /* Each time XOR with the data that needs to be calculated, and the calculation will point to the next data */
for (i=8; i>0; --i) /* The following calculation process is the same as calculating a byte crc */
{
if (crc & 0x80)
crc = (crc << 1) ^ 0x31;
else
crc = (crc << 1);
}
}
return (crc);
}
------------------------------