Page 1 of 2
Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 11:29 am
by p.erasmus
Hi Team.
I am running in a circle here and hope someone can pull me out.
I am getting 3 bytes from an ADC over I2C which is declared as Data_Hi ,Data_Mid and Data_lo in the correct order as the ADC send them out ,
from the screen print you will see that the data is correct Hi_byte = 0 ,mid_byte = 0xdc and Lo_byte = 0xd5
I use the following to combine the 3 bytes
Code: Select all
ChannelResult = 0 // Long variable
ChannelResult = ( Data_Hi <<16) OR ( Data_MID << 8) OR ( Data_Lo)
The High byte always come out as FF and the 4 byte from the long is also FF , mid and Low bytes are always correct ? what am I doing wrong ??
the variable is cleared so all bytes should be zero(0) before the shifting starts how come it change to FF when shifting the byte and only happening on the 3rd byte (bit 16-23) and seems also on bit 24-32)
I am pulling the little bit hair I am having also out now

- P3.jpg (28.79 KiB) Viewed 9948 times
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 11:34 am
by kersing
Hi Peter,
Please try using the ‘|’ in stead of OR (or use +) to combine values.
Best regards,
Jac
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 11:46 am
by LeighM
You need to use unsigned longs, for the intermediate Data_xxx too (depending upon target)
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 1:06 pm
by p.erasmus
Hi Leigh/Jac
Thanks a million for the input however
The variable is a Ulong as here

- P1.jpg (11.65 KiB) Viewed 9937 times
I tried Jacs advise as well "+" and ‘|’ all results in the same result.

- P2.jpg (12.43 KiB) Viewed 9937 times

- P3.jpg (17.94 KiB) Viewed 9937 times
Is it possible for some one just to plug these 3 values in bytes and shift them and see what your result is Please guys I am desperate I don't understand what is going on
Thank a million
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 1:37 pm
by mnfisher
Data high needs to be a 32 bit value too, or assign it to one before shifting.
Something like:
Result = hi
Result = (result << 16) + (mid << 8) + lo
Can use + or | (or) to combine
Martin
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 1:52 pm
by p.erasmus
Martin
Thanks for your input,
I will give it another go Thanks
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 1:58 pm
by mnfisher
There's definitely something awry - the upper bits should be 0 rather than 0xFF
Is there a signed shift going in somewhere by mistake (compiler/FC)
What value does Hi actually have? (ie is bit 7 set?)
Martin
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 2:21 pm
by p.erasmus
There's definitely something awry - the upper bits should be 0 rather than 0xFF
This is the problem the upper bit should be zero as the the result is positive now it is already negative (by Flowcode or XC8) the next step should be to check the MSB if =1 then result is negative and the two's compliment must be done how shall I do this when the tools already give me a nearly maximum negative number
What value does Hi actually have? (ie is bit 7 set?)
No at the moment it is 0 as the result is Positive (Hi byte = 0)when it is negative the bit will be 1 now it is zero the tools make it negative automatically

MAY BE I AM JUST TO STUPID
Any case I tired your method too same issue

- P1.jpg (49.53 KiB) Viewed 9923 times
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 2:57 pm
by p.erasmus
Hi All.
Plugging in my code and all the suggestion here in mikroC compiler all result in the expected outcome
there is an issue in FC or in XC8 which as you guys should help please
The ADC is returning the conversion result in as positive because the differential voltage is VI+ > then VIN- by 0,88V
which is 56418 bits or 0xDC63 as captured in the 3 bytes at gain of 15,625uV = 0.88 correctly read by the I2C bus and calculated by the compiler

- P3.jpg (21.51 KiB) Viewed 9918 times

- P1.jpg (12.67 KiB) Viewed 9918 times

- P2.jpg (19.6 KiB) Viewed 9918 times

- P4.jpg (12.44 KiB) Viewed 9918 times
Re: Combining bytes to form 32 bit value (24 bit)
Posted: Mon Mar 29, 2021 3:00 pm
by LeighM
I cannot replicate this (in simulation I get 0xdc5d)
Do you get the 0xff... in simulation and on target?
Could you send the program?
What version of Flowcode?