How to combine high and low bytes into an integer

Any general or miscellaneous queries that do not fit into the other forum catagories

Moderators: Benj, Mods

Post Reply
jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

How to combine high and low bytes into an integer

Post by jimhumphries »

Is there a Flowcode routine out there that takes the high byte and low byte from, say, the Thermometer component and outputs an integer value?

Thanks in advance,

Jim

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to combine high and low bytes into an integer

Post by Benj »

Hi Jim

you can do this via a calculation icon. Here is the way to combine the two byte variables.

INT_Var = lo_Var
INT_Var = INT_Var + (hi_Var << 8)

and to get the int variable back into two bytes.

lo_Var = INT_Var
hi_Var = INT_Var >> 8

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Re: How to combine high and low bytes into an integer

Post by jimhumphries »

Thanks Ben! I didn't know it was already available. You guys are truly wizards.

Jim

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Re: How to combine high and low bytes into an integer

Post by jimhumphries »

Benj wrote:Hi Jim

you can do this via a calculation icon. Here is the way to combine the two byte variables.

INT_Var = lo_Var
INT_Var = INT_Var + (hi_Var << 8)

and to get the int variable back into two bytes.

lo_Var = INT_Var
hi_Var = INT_Var >> 8
Ben:

When I use the calculation that you recommended I get an unexpected result. For some reason the calculated integer value of the combined ADC high and low bytes is -32768 while the ADC integer value is 512.

I've attached a v3.6 test program. Can you help with this?
Int Var Test.fcf
(5.5 KiB) Downloaded 458 times
Thanks,

Jim

User avatar
Dan81
Valued Contributor
Valued Contributor
Posts: 268
Joined: Sun Jan 15, 2006 4:07 pm
Location: Albi France
Been thanked: 60 times
Contact:

Re: How to combine high and low bytes into an integer

Post by Dan81 »

Hello Jim

The right calculation is

SIGINT = ( SIGL >> 6 ) + ( SIGH << 2 )

I think ADC component is better than Thermometer.

Daniel

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Re: How to combine high and low bytes into an integer

Post by jimhumphries »

Thanks Daniel - I'll try that.

I use the thermometer because it allows the high and low byte read whereas the ADC component (potentiometer) only allows a single byte read or the integer read (in v3.2 and earlier, the thermometer didn't allow the integer read). I do a peak detection using successive reads of the ADC in a tight loop. I regard a peak as having occurred whenever a second ADC value is less than the previous value (the signal level is descending). To minimize the lag I first compare the high bytes of two successive reads. If the second read is smaller than the first the peak has already passed and I don't bother to compare the low bytes. If the high bytes are equal, I go ahead and compare the low bytes for better precision. When the second high byte is less than the first I have my peak with lower precision but fast and when the high bytes are equal (I haven't missed the peak yet) I can go ahead and compare the low bytes to find the peak with higher precision. It turns out that, at least in V3.2, this approach was quite a bit faster than using the ADC component and comparing the integers.

I run my 16F876 at 4 MHz to reduce power and I'm hoping that Matrix will revisit their ADC code with a concern for maximizing the acquisition rate (consistent with reliable sampling) for any given clock frequency. As it is they they have a sampling period that is about 20 times the specification in the data sheet for operation at 4 MHz (it is fixed) and they don't adjust the ADCON0 register to adjust the acquisition clock with the processor clock (they leave it at divide by thirty-two instead of changing it to divide by eight). Another concern is that the number of instructions in the compiled ADC code has increased in v3.6. This increases the inter-sample interval and, hence, adds to the detection lag.

Ben has offered to help with this so we'll see how it goes.

Thanks again for your reply,

Jim

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: How to combine high and low bytes into an integer

Post by Benj »

Hi Jim

You are right we did play with the analogue routines a bit to get a nice setting that would suit 99% of applications. Faster sampling is very much possible but at the moment in Flowcode V3 it requires a bit of fiddling with the target FCD definition file. The current ADC routine always has an acquisition time of 200 program cycles to allow for swapping between channels however if you are sampling a single channel then the acquisition time can be completely removed. There is also the acquisition rate which can be modified if need be. If you need help with this to get the main analogue routines sampling faster then let me know.

The analogue component in Flowcode V4 is much more flexible and allows for a lot of settings to be made directly from within Flowcode.

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Re: How to combine high and low bytes into an integer

Post by jimhumphries »

Ben:

I think my best bet is to wait for the improvements in the v4 release.

Thanks,

Jim

Post Reply