Variable table


Closed Thread
Results 1 to 2 of 2

Thread: Variable table

  1. #1
    Join Date
    Oct 2006
    Location
    Edmonton AB Canada
    Posts
    52

    Default Variable table

    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

  2. #2
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    699


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by rsocor01; - 9th January 2010 at 05:21.

Similar Threads

  1. EEPROM Variables (EE_Vars.pbp)
    By Darrel Taylor in forum Code Examples
    Replies: 79
    Last Post: - 26th October 2012, 00:06
  2. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36
  3. PIC 18F452 Table Pointer/ Table Read
    By sougata in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 14th March 2006, 03:07
  4. Real Time Clock & Eeprom
    By smart_storm in forum General
    Replies: 8
    Last Post: - 17th February 2006, 19:03
  5. WORD vs BYTE variable lengths
    By bartman in forum General
    Replies: 0
    Last Post: - 28th November 2005, 21:16

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts