Combining bytes to form 32 bit value (24 bit)

For general Flowcode discussion that does not belong in the other sections.
User avatar
p.erasmus
Posts: 434
http://meble-kuchenne.info.pl
Joined: Thu Dec 03, 2020 12:01 pm
Location: Russia / Россия
Has thanked: 104 times
Been thanked: 88 times

Combining bytes to form 32 bit value (24 bit)

Post 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 :D

P3.jpg
P3.jpg (28.79 KiB) Viewed 5027 times
Regards Peter - QME Electronics

kersing
Valued Contributor
Posts: 169
Joined: Wed Dec 02, 2020 7:28 pm
Has thanked: 72 times
Been thanked: 63 times

Re: Combining bytes to form 32 bit value (24 bit)

Post by kersing »

Hi Peter,

Please try using the ‘|’ in stead of OR (or use +) to combine values.

Best regards,

Jac

LeighM
Valued Contributor
Posts: 402
Joined: Mon Dec 07, 2020 1:00 pm
Has thanked: 75 times
Been thanked: 220 times

Re: Combining bytes to form 32 bit value (24 bit)

Post by LeighM »

You need to use unsigned longs, for the intermediate Data_xxx too (depending upon target)

User avatar
p.erasmus
Posts: 434
Joined: Thu Dec 03, 2020 12:01 pm
Location: Russia / Россия
Has thanked: 104 times
Been thanked: 88 times

Re: Combining bytes to form 32 bit value (24 bit)

Post by p.erasmus »

Hi Leigh/Jac

Thanks a million for the input however :(

The variable is a Ulong as here

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

P2.jpg
P2.jpg (12.43 KiB) Viewed 5016 times
P3.jpg
P3.jpg (17.94 KiB) Viewed 5016 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
Regards Peter - QME Electronics

mnfisher
Valued Contributor
Posts: 1044
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 107 times
Been thanked: 538 times

Re: Combining bytes to form 32 bit value (24 bit)

Post 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

User avatar
p.erasmus
Posts: 434
Joined: Thu Dec 03, 2020 12:01 pm
Location: Russia / Россия
Has thanked: 104 times
Been thanked: 88 times

Re: Combining bytes to form 32 bit value (24 bit)

Post by p.erasmus »

Martin

Thanks for your input,
I will give it another go Thanks
Regards Peter - QME Electronics

mnfisher
Valued Contributor
Posts: 1044
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 107 times
Been thanked: 538 times

Re: Combining bytes to form 32 bit value (24 bit)

Post 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

User avatar
p.erasmus
Posts: 434
Joined: Thu Dec 03, 2020 12:01 pm
Location: Russia / Россия
Has thanked: 104 times
Been thanked: 88 times

Re: Combining bytes to form 32 bit value (24 bit)

Post 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 :D MAY BE I AM JUST TO STUPID

Any case I tired your method too same issue

P1.jpg
P1.jpg (49.53 KiB) Viewed 5002 times
Regards Peter - QME Electronics

User avatar
p.erasmus
Posts: 434
Joined: Thu Dec 03, 2020 12:01 pm
Location: Russia / Россия
Has thanked: 104 times
Been thanked: 88 times

Re: Combining bytes to form 32 bit value (24 bit)

Post 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
P3.jpg (21.51 KiB) Viewed 4997 times
P1.jpg
P1.jpg (12.67 KiB) Viewed 4997 times
P2.jpg
P2.jpg (19.6 KiB) Viewed 4997 times
P4.jpg
P4.jpg (12.44 KiB) Viewed 4997 times
Regards Peter - QME Electronics

LeighM
Valued Contributor
Posts: 402
Joined: Mon Dec 07, 2020 1:00 pm
Has thanked: 75 times
Been thanked: 220 times

Re: Combining bytes to form 32 bit value (24 bit)

Post 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?

Post Reply