cancel
Showing results for 
Search instead for 
Did you mean: 

Cast Byte array to long "occasionally" not working

Rob Ashworth
Senior

I am casting a Byte array (results from ADC) to a long, but occasionally - about 1:100000, it get the sums wrong.  I've got to the line in the code where it goes wrong, and it is when it is casting a byte array to a long (I use union Method).

 

//Signed ADC Value - so 24-bit value can be shifted in
union
{
  uint8_t b[4];
  int32_t l;
}I32ADCValue;

 

 

 

  //Populate the Raw mV Array
   for(i=0;i<8;i++)//Loop for each channel
   {
      I32ADCValue.b[3]=ads1298rData.ADCMsg.Channel[i][0];
      I32ADCValue.b[2]=ads1298rData.ADCMsg.Channel[i][1];
      I32ADCValue.b[1]=ads1298rData.ADCMsg.Channel[i][2];
      I32ADCValue.b[0]=0;

      //Channels are not in sequence - create temporary array here.
      mVx[i]=(I32ADCValue.l*Gain_mV);
   }

 

 

When I run the debugger, it is having too bit a blue in I32ADCValue.l - in fact where I set .b[0] to 0, it is not after it has cast it.....any clues?

Union conversion1.png 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Looks correct to me. 756497664 = 0x2D_17_3D_00. Those are the values in the array. Values are stored in little-endian format. Perhaps you are assuming big-endian, but then it would not be working ever, so it's unclear.

> it is having too bit a blue in I32ADCValue.l

Huh? Proofread a bit, please.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Guru

Looks correct to me. 756497664 = 0x2D_17_3D_00. Those are the values in the array. Values are stored in little-endian format. Perhaps you are assuming big-endian, but then it would not be working ever, so it's unclear.

> it is having too bit a blue in I32ADCValue.l

Huh? Proofread a bit, please.

If you feel a post has answered your question, please click "Accept as Solution".

Ah, I found this not actually the problem.  I checked the Big-Little Endian, and I had made an incorrect assumtion.