Log in

View Full Version : Variable table



Meriachee
- 9th January 2010, 04:52
Hi gang,

I'm looking for a creative way of managing a variable. Creative being a minimal amount of code. I need to store the state of 5 outputs on the fly, so that the last known state can be loaded upon startup.

With the 5 outputs, each can have 2 states (high or low). Each of the outputs can be changed independently, so the actual variable, including all high or all low, as best I can figure, has 17 possible states.

Is it better to do an evaluation of all the ports every time an output changes, and keep a small variable, or is it better to keep 5 variables and compare them together to get a single variable to write to memory?

Which method results in the smaller (and thus faster) code?

Thanks for any ideas
Gary

rsocor01
- 9th January 2010, 05:19
I'm looking for a creative way of managing a variable. Creative being a minimal amount of code. I need to store the state of 5 outputs on the fly, so that the last known state can be loaded upon startup.

With the 5 outputs, each can have 2 states (high or low).

MY_VARIABLES VAR BIT[5]

Would do the trick.


Each of the outputs can be changed independently, so the actual variable, including all high or all low, as best I can figure, has 17 possible states.


2^5 = 32 posible states

So you can get 32 possible states with 5 variables defined as bits.


Is it better to do an evaluation of all the ports every time an output changes, and keep a small variable, or is it better to keep 5 variables and compare them together to get a single variable to write to memory?

If you want to monitor the state of the I/O ports then one way to do it is

SYMBOL VAR_0 = PORTB.0 'If you are using PORTB
SYMBOL VAR_1 = PORTB.1
SYMBOL VAR_2 = PORTB.2
SYMBOL VAR_3 = PORTB.3
SYMBOL VAR_4 = PORTB.4

Or you can do it like this

MY_VARIABLE VAR BYTE
MY_VARIABLE = PORTB

And then check the status of the bits in MY_VARIABLE

IF MY_VARIABLE.0 = .... THEN ...
IF MY_VARIABLE.1 = .... THEN ...
......

Robert