I don't understand completely the description about CRC calculation in ADXL288 datasheet. How can I find an example code to understand this?

I don't understand completely the description about CRC calculation in ADXL288 datasheet. How can I find an example code to understand this?

Hi MattHazel,

This is my code. Thanks for your help.

unsigned char CRC_Calculation(unsigned short MISO, unsigned char CRC_Ini)

{

unsigned char CRC_Array[8];

unsigned char CRC_Cal[8];

unsigned char MISO_Array[16];

unsigned char CRC_temp=0;

char i,j;

unsigned short k;

for(i=0;i<8;i++) //put CRC Ini to array

{

if((CRC_Ini & 0x01)==1)

{

CRC_Array[i]=1;

}

else

{

CRC_Array[i]=0;

}

CRC_Ini>>=1;

}

for(i=0;i<16;i++) // put MISO to array

{

if((MISO & 0x0001)==1)

{

MISO_Array[i]=1;

}

else

{

MISO_Array[i]=0;

}

MISO>>=1;

}

for(i=0;i<16;i++) //CRC calculation

{

CRC_Cal[0]=MISO_Array[i]^CRC_Array[7];

CRC_Cal[1]=CRC_Array[0];

CRC_Cal[2]=CRC_Array[1]^MISO_Array[i]^CRC_Array[7];

CRC_Cal[3]=CRC_Array[2]^MISO_Array[i]^CRC_Array[7];

CRC_Cal[4]=CRC_Array[3]^MISO_Array[i]^CRC_Array[7];

CRC_Cal[5]=CRC_Array[4];

CRC_Cal[6]=CRC_Array[5];

CRC_Cal[7]=CRC_Array[6];

for(j=0;j<8;j++)

{

CRC_Array[i] = CRC_Cal[i];

}

}

for(i=0;i<8;i++) // put array in a char type value

{

if(CRC_Cal[i]>0)

{

CRC_temp|=0x80;

CRC_temp>>=1;

}

else

{

CRC_temp>>=1;

}

}

return CRC_temp;

}

Hello Ian,

I've given your code a look over, and there are a few things that don't quite add up for me.

First, this code is only written to execute on a single MISO value. do you call this function iteratively from your main program? Figures 47 through 49 of the ADXL288 datasheet show that all previous MISO responses are used in the calculation of the CRC value. so, if you have 5 previous MISO responses since the last time that you received the CRC value, then you need to call this function 5 times so that you can fully process the CRC.

Second, the MISO value needs to be run through the CRC calculation from MSB to LSB. this is made quite clear in the datasheet. Based on your code, it appears that you are processing through the MISO value in the reverse order (from LSB to MSB). Notice how the pseudo-code in the datasheet has a for loop that is written as:

for(i = 15; i>=0; i--)

this is done explicitly so that the MISO[i] array is processed from MSB (MISO[15]) to LSB (MISO[0]).

Third, the datasheet states that once you calculate the new CRC value, that this value must then be inverted in order to comply with the SAEJ1850 specification. i do not see an inversion step executed on the CRC_Cal[i] value in your code.

Fourth, i dont understand why you perform the following step in your code:

for(j=0;j<8;j++)

{

CRC_Array[i] = CRC_Cal[i];

}

this follows the pseudo code written in the ADXL288 datasheet, but you aren't actually doing anything with the CRC_Array[i] value once you finish this for loop. Hence, this code has no purpose. the datasheet includes this step because it expects that you will iterate the previous for loop, and the CRC_Array[i] value needs to be updated in order initialize the next CRC calculation. because your code only processes a single MISO command, its not necessary to implement this particular for loop. you return "CRC_temp" at the end of your program, so you could use that value and pass it to this function the next time you call this routine.

Fifth, when converting the CRC_Cal[i] array back to a char value, i don't think your implementation is correct. The following code is flawed because it will right shift the CRC_temp value one more time than you need to.

for(i=0;i<8;i++) // put array in a char type value

{

if(CRC_Cal[i]>0)

{

CRC_temp|=0x80;

CRC_temp>>=1;

}

else

{

CRC_temp>>=1;

}

}

You are going to have an issue with this architecture when you process the final CRC_Cal[i] value. When putting the final CRC_Cal[i] value into the CRC_temp variable, you should NOT perform any additional right shifting of the CRC_temp variable. because your code performs a right shift with *every* iteration of the loop, you end up shifting the CRC_temp variable one more time than you need to, which will corrupt the CRC value itself.

That should be enough to get you started. Let me know if you still have issues once you have worked out some of these bugs.

regards,

-Matt Hazel

Hi Ian,

Where are you seeing the information about CRC calculations? Is it on the product page?

Regards,

Venkat