Shop OBEX P1 Docs P2 Docs Learn Events
Variable alias question — Parallax Forums

Variable alias question

Dave WDave W Posts: 19
edited 2005-06-02 18:31 in BASIC Stamp
Hi,

My first post.· A search didn't turn up anything.· I'm just learning the Stamp Microcontroller and have a couple of homework boards with the Stamp 2 on them.· I've built a device which discharges rechargeable batteries under heavy load, one cell at a time.· It's a very simple program and I do have it working.· It puts a 1 ohm load on the battery and counts the seconds until the cell reaches the 1 volt level.

Now my question.· In order to make the the program readable, I tried to assign the variable name, "LED_Bat_Level" to the OUTB reserved variable.· That works, but only if I use decimal constants for setting the values.· If I try to use binary values (more convenient), the program runs, but the LED which indicates the status of the discharge never turns on.· Here is the code snippet which sets this up:

Red CON 4
Grn CON 1
Off CON 0
LED_Bat_Level VAR OUTB
LED_Bat_Level = Off······ 'turn battery level LED off

It's a bi-color LED and I'm using I/O pins 4 and 6 to drive it.· So, I would like to make Red = %0010 and Grn = %1000.· It's much clearer, at least to me, to do it that way, because I can more easily change the pins I'm using.· The decimal values work, the binary ones don't.· What am I missing?· Right now my controller only discharges one cell.· I plan to add 3 more cells and discharge 4 at a time, all independently, and measure the time it takes each cell to drop to 1 volt.· This tester has already uncovered one NiMh cell out of a 4 cell group which is way too weak to be used.· The other 3 cells are fine.· I think you can see the advantage of using binary numbers to specify the states of the output pins.

I'm sure I'm not using the alias assignment correctly, but just can't figure out what it is.· Can someone give me a hint?· I can easily add the entire program is that's of any value.· If so, should I use the editor file or submit it simply as a text file?· Thanks

Dave W

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-02 00:26
    Dave,

    There is no problem using binary numbers in constant definitions; I do this all the time.· The BASIC Stamp editor/compiler doesn't care -- no matter what format, a number is a number.

    I think the problem that you're having is you're reading the bits left-to-right instead of right-to-left.· Given the values you want to use (Red is %0010 [noparse][[/noparse]P5] and Green is %1000 [noparse][[/noparse]P7]).··Finally, are you initializing the LED pins as outputs?· Before you try to set the LED state, you should have something like this in your program:

    · DIRB = %1010

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Dave WDave W Posts: 19
    edited 2005-06-02 00:37
    I just did a DIR4 = 1 and DIR6 = 1.· Must I assign the entire NIB when I'm not using the other pins?· Also, to debug, I inserted a DEBUG command to printout the values of LED_Bat_Level and got all sorts of strange characters until I did the decimal thing.· When I get a chance, I'll try your suggestion.· Thanks.

    Dave W
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-06-02 01:45
    Well, you could say

    HIGH 4
    HIGH 6

    And do the same things. The 'HIGH' and 'LOW' commands turn the pins into outputs, and 'HIGH' then outputs a 1, and "LOW" then outputs a 0. (Well, actually it doesn't "output" a zero, instead it turns on a transistor which provides a low-resistance path to ground. The result of this is that the signal at the pin goes to ground.

    And typically I name my pins, so:

    LED_Pin1 CON 4
    LED_Pin2 CON 6

    HIGH LED_Pin1
    HIGH LED_Pin2

    All of which would do the same thing, but make it MUCH easier to re-map your pins in the future.

    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-02 02:23
    Using....

    · DIRB = %0101

    is exactly the same as....

    · DIR4 = 1
    · DIR6 = 1

    except that it only takes one line of code.

    DEBUG normally prints characters, so if you're trying to print the value of variables you'll need to use the BIN, DEC, or HEX output modifiers.· Like this:

    · DEBUG BIN4 Red
    Dave W said...

    I just did a DIR4 = 1 and DIR6 = 1.· Must I assign the entire NIB when I'm not using the other pins?· Also, to debug, I inserted a DEBUG command to printout the values of LED_Bat_Level and got all sorts of strange characters until I did the decimal thing.· When I get a chance, I'll try your suggestion.· Thanks.

    Dave W

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Dave WDave W Posts: 19
    edited 2005-06-02 17:55
    Thanks, Jon and Allen.· I changed the bit order in my binary number and it worked fine.· I know that in any numbering system, the most significant digit/bit comes first.· However, I got confused by the order of the pin breakdown when going from word to byte to nibble.· The Stamp editor is set to name the low order pin numbers first:· pins 0 to 3 are outA and 12 through 15 are outD.· That would logically tell me to assign the binary pin assignments in the same order, low to high.· It was only by chance that I converted the binary to decimal using the wrong bit order.· Anyway, I understand it now.· I guess there is no choice in the binary bit order for pins.· If it wasn't done that way, the software would have a problem when specifying binary, decimal, or hex from the same byte or nibble.· Nevertheless, it's still confusing.

    Thanks again.

    Dave W.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-02 18:31
    Just remember that in all numbers, the left-most digit is the "most-significant" and the right-most digit is the "least-significant". For this reason, the digit or bit order is from right to left, beginning with digit (or bit) zero. So, in %0100 it is BIT2 that is "1" (bit order is 3-2-1-0).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.