Skip to main content
Graduate II
January 2, 2024
Solved

Cast Byte array to long "occasionally" not working

  • January 2, 2024
  • 1 reply
  • 1142 views

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 

    This topic has been closed for replies.
    Best answer by TDK

    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.

    1 reply

    TDKAnswer
    Super User
    January 2, 2024

    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.

    Graduate II
    January 2, 2024

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