Shop OBEX P1 Docs P2 Docs Learn Events
Pbasic Varables — Parallax Forums

Pbasic Varables

ArchiverArchiver Posts: 46,084
edited 2002-03-31 05:48 in General Discussion
I would like to learn about the Pbasic Variables for the stamp. Is
there a place that would list the variations and give examples of each?
I have the stamp editor version 1.3 and couldn't find anything there.
It would be nice is I could get some comparisons to BASICA or GW BASIC,
I know those pretty well. Suggestions please..


thanks for your help.

Leroy

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-03-24 20:18
    Leroy, download a copy of the PICBasic Pro Manual. It has all the asnwers.

    Sid


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-24 20:20
    Sorry, Leroy, wrong reference. You might be able to download a copy of the
    Stamp Manual from Parallax, or at least a portion of it.

    Sid


    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-24 20:29
    The 2.0 manual (available for download) has a very good discussion of Stamp
    variables.

    Here's some quick stuff:

    PBASIC variables are limited (26 bytes) but incredibly flexible. You can
    define variables as word (16-bit), byte (8-bit), nib (4-bit) and bit. You
    simply declare your variable and its type; the compiler takes care of
    allocating RAM. With this much type flexibility, the 26 bytes can go a long
    way.

    For example, if you have a counter that goes from 0 to 9, there's no reason
    to declare it a byte or word since for bits (0..15) will handle it.

    The compiler allows you to alias (rename / reuse) variables or even portions
    of them. For example:

    flags VAR Byte
    overTmp VAR flags.Bit7

    You can now deal with a variable called overTmp which is, of course, pointing
    to the msb of flags.

    One last tidbit that isn't really explained anywhere (in depth). All Stamp
    memory is an implicit array. Example:

    manny VAR Byte
    moe VAR Byte
    jack VAR Byte
    x VAR Nib

    FOR x = 0 TO 2
    manny(x) = $FF
    NEXT

    You can access bits in a nib, byte or word with LowBit(x). Likewise, nibbles
    in a byte or word with LowNib(x) and, finally, bytes in a word with
    LowByte(x).

    FOR x = 0 TO 7
    manny.LowBit(x) = 1
    NEXT

    HTH ... or, at least, piques your interest.

    -- Jon Williams
    -- Applications Engineer, Parallax


    In a message dated 3/24/02 11:50:23 AM Central Standard Time, leroy@f...
    writes:


    > I would like to learn about the Pbasic Variables for the stamp. Is
    > there a place that would list the variations and give examples of each?
    > I have the stamp editor version 1.3 and couldn't find anything there.
    > It would be nice is I could get some comparisons to BASICA or GW BASIC,
    > I know those pretty well.




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-26 06:07
    Dear Jon,

    Thanks in advance so much for your response and yes your comments did
    seem rather interesting. I looked through the portions of the manual
    that discusses variables and seen reference to much of what you said
    below. The bits, bytes, and nibbles I am ok with, the array is the part
    about the following code that is still a bit foggy:

    One last tidbit that isn't really explained anywhere (in depth). All
    Stamp memory is an implicit array.
    Example:

    manny VAR Byte
    moe VAR Byte
    jack VAR Byte
    x VAR Nib

    FOR x = 0 TO 2
    manny(x) = $FF
    NEXT


    thanks again,

    Leroy
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-29 16:00
    Leroy:

    I'm not sure how else to follow up -- doing some simple experiments (like I
    illustrated below) is probably the best way to clarify this for yourself.
    One thing I can add the pertains to the Manny-Moe-Jack example is that it
    works because all three variables are the same type and declared in a
    specific order. The compiler arranges variables by size, starting with
    words, then bytes, then nibs and finally, bits -- and then in the order they
    appear in your declarations section. If you declare several variables of the
    same type [noparse][[/noparse]in order], you can treat them as an array of the first item. Like
    this:

    counters VAR Byte
    counter0 VAR counters ' counter0 occupies same location as
    counters
    counter1 VAR Byte ' counter1 occupies location after
    counter0
    counter2 VAR Byte
    counter3 VAR Byte

    These two lines of code accomplish the same thing:

    counters(2) = 10
    counter2 = 10

    Why would you want to do variables manually instead of declaring an array?:

    counters VAR Byte(4)

    Because PBASIC won't let you alias to an array element and sometimes that is
    a useful thing to do, so the technique presented above handles it.

    This is legal (referencing the declarations above):

    redCount VAR counter0 ' occupies same location as counter0
    grnCount VAR counter1
    bluCount VAR counter2
    blkCount VAR counter3

    This is not:

    redCount VAR counter(0)
    grnCount VAR counter(1)
    bluCount VAR counter(2)
    blkCount VAR counter(3)

    ...because an array index cannot be used in a variable declaration.

    Using the aliases, like I did above, can make the program a little more
    readable. Sure, it's a bit more work on setup, but I think it's worth it.

    In the end, though, the declaration would be simplest like this:

    counters VAR Byte
    redCount VAR counters ' redCount is same as counters(0)
    grnCount VAR Byte
    bluCount VAR Byte
    blkCount VAR Byte

    Now, these two lines of code do the same thing:

    counters(3) = 0
    blkCount = 0

    ... and I can use the array to set all values in a loop:

    FOR x = 0 TO 3
    counters(x) = 10
    NEXT

    ... and

    DEBUG DEC redCount

    ... displays "10" on the DEBUG screen.


    So ... in general, a PBASIC program will allow you to add an array index
    (parenthesis) to ANY variable [noparse][[/noparse]except in a declaration] -- even if it wasn't
    explicitly declared an array. The index will offset into the Stamp's memory
    from the [noparse][[/noparse]physical] location of the variable (0 = no offset).

    I hope this has helps lift the fog a bit and has not made it thicker....

    -- Jon Williams
    -- Applications Engineer, Parallax


    In a message dated 3/29/02 2:53:31 AM Central Standard Time, leroy@f...
    writes:


    > Dear Jon,
    >
    > Thanks in advance so much for your response and yes your comments did
    > seem rather interesting. I looked through the portions of the manual
    > that discusses variables and seen reference to much of what you said
    > below. The bits, bytes, and nibbles I am ok with, the array is the part
    > about the following code that is still a bit foggy:
    >
    > One last tidbit that isn't really explained anywhere (in depth). All
    > Stamp memory is an implicit array.
    > Example:
    >
    > manny VAR Byte
    > moe VAR Byte
    > jack VAR Byte
    > x VAR Nib
    >
    > FOR x = 0 TO 2
    > manny(x) = $FF
    > NEXT
    >
    >
    > thanks again,
    >
    > Leroy
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-30 17:43
    Dear Jon,

    Thanks so much for your response and assistance about my variable
    questions. Here's a couple of more for you:

    Here's the program you suggested (or close save the debug's):

    '{$STAMP BS2}
    manny VAR Byte
    moe VAR Byte
    jack VAR Byte
    x VAR Nib

    FOR x = 0 TO 2
    manny(x) = $F0
    NEXT

    DEBUG bin manny
    debug cr
    DEBUG bin moe
    debug cr
    DEBUG bin jack

    Two questions:

    1.) Looks like all three variables get the same values i.e. 1111 0000
    or hex f0. Why do I need separate names? This seems rather useless
    unless I am missing a point. That is, "why would I want to store a
    number in the variable manny and then have the same number also appear
    in moe & jack"?

    2.) Let's say I have the following:

    '{$STAMP BS2}
    manny VAR Byte
    moe VAR Byte

    These are byte variables i.e. they are 8 bits wide or

    B7 B6 B5 B4 B3 B2 B1 B0

    Is it possible for me to move bits: B7 B6 B5 B4 of the variable manny

    into B3 B2 B1 B0 of the variable moe?

    Can I move bit 6 of manny into bit 2 of moe?

    thanks so much for your help. I hope to master this variable thing
    soon.

    Happy easter,

    Leroy
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-30 18:06
    Leroy:

    All of this has to do with a programmer's style. On to your questions:

    (1) Of course, you don't have to have separate names, but I find on many
    occassions that separate names make my code easier to read and maintain. I
    work hard to make my code easy to read (for others) and maintain (for me).

    (2) This is where the variable modifiers help. You can do this.

    moe.LowNib = manny.HighNib

    Moving something other than 1, 4 or 8 bits requires a bit of math or logical
    operations. Let's say you want to move the upper six bits of manny into the
    lower six bits of moe. The upper two bits of moe must be maintained. This
    is one way do it:

    moe = (moe & %11000000) | (manny >> 2)

    Please consult the manual if some of these operators are new to you.

    -- Jon Williams
    -- Parallax


    In a message dated 3/30/02 11:44:08 AM Central Standard Time, leroy@f...
    writes:


    > Dear Jon,
    >
    > Thanks so much for your response and assistance about my variable
    > questions. Here's a couple of more for you:
    >
    > Here's the program you suggested (or close save the debug's):
    >
    > '{$STAMP BS2}
    > manny VAR Byte
    > moe VAR Byte
    > jack VAR Byte
    > x VAR Nib
    >
    > FOR x = 0 TO 2
    > manny(x) = $F0
    > NEXT
    >
    > DEBUG bin manny
    > debug cr
    > DEBUG bin moe
    > debug cr
    > DEBUG bin jack
    >
    > Two questions:
    >
    > 1.) Looks like all three variables get the same values i.e. 1111 0000
    > or hex f0. Why do I need separate names? This seems rather useless
    > unless I am missing a point. That is, "why would I want to store a
    > number in the variable manny and then have the same number also appear
    > in moe & jack"?
    >
    > 2.) Let's say I have the following:
    >
    > '{$STAMP BS2}
    > manny VAR Byte
    > moe VAR Byte
    >
    > These are byte variables i.e. they are 8 bits wide or
    >
    > B7 B6 B5 B4 B3 B2 B1 B0
    >
    > Is it possible for me to move bits: B7 B6 B5 B4 of the variable manny
    >
    > into B3 B2 B1 B0 of the variable moe?
    >
    > Can I move bit 6 of manny into bit 2 of moe?
    >
    > thanks so much for your help. I hope to master this variable thing
    > soon.
    >
    > Happy easter,
    >
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-30 18:27
    '{$STAMP BS2}
    manny VAR Byte
    moe VAR Byte
    jack VAR Byte
    x VAR Nib

    So with the above definitions / declarations

    manny(0) = manny
    manny(1) = moe
    Manny(2) = jack

    If I am on the right track here, this seems very confusing.. I.E. in
    GWBASIC once an array is defined, each defined sections of that array
    are separate entities. Such as an array defined as manny(10) has ten
    elements that are all a byte or a word wide and are called manny(0)
    through manny(10).


    thanks,

    Leroy
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-30 18:46
    Yes, you are on the right track. Just remember that PBASIC is an embedded
    BASIC with far fewer resources than any PC-oriented BASIC would have. The
    BASIC Stamp designer (ironically, a guy named "Chip") created an incredibly
    flexible memory architecture to help engineers manage complex tasks with few
    resources.

    You can, of course, do as you suggested and define a standard array as you
    would in GWBASIC.

    manny VAR Byte(4)

    Doing it this way, you always have to refer to manny(0), manny(1), etc. This
    is fine. Sometimes, though, I find the implicit array capability and
    aliasing variables makes my code easier to read.

    Again:

    counters VAR Byte
    redCount VAR counters
    grnCount VAR Byte
    bluCount VAR Byte
    blkCount VAR Byte

    In my opinion, using:

    grnCount = 3

    ...is more clear than:

    counters(1) = 3

    Of course, there are many ways to skin a cat. If you want to stick to PC
    conventions, you could add this.

    Red CON 0
    Green CON 1
    Blue CON 2
    Black CON 3

    Now, you could do this:

    counters(Green) = 3

    ...and the code becomes very readable and easy to maintain.

    In the end, there are only 26 bytes of user RAM. Variable aliasing
    (renaming) is one part of PBASIC that I find particularly helpful -- and
    often find myself wishing I had the same flexibility with my PC programs.

    -- Jon Williams
    -- Parallax


    In a message dated 3/30/02 12:28:17 PM Central Standard Time, leroy@f...
    writes:


    > '{$STAMP BS2}
    > manny VAR Byte
    > moe VAR Byte
    > jack VAR Byte
    > x VAR Nib
    >
    > So with the above definitions / declarations
    >
    > manny(0) = manny
    > manny(1) = moe
    > Manny(2) = jack
    >
    > If I am on the right track here, this seems very confusing.. I.E. in
    > GWBASIC once an array is defined, each defined sections of that array
    > are separate entities. Such as an array defined as manny(10) has ten
    > elements that are all a byte or a word wide and are called manny(0)
    > through manny(10).
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-30 21:55
    Your example:

    moe = (moe & %11000000) | (manny >> 2)

    In English this says: moe = (moe AND %11000000 OR (manny (Logical Shift
    right by 2)))

    Does this:

    Let's say moe= 0001 1011
    Let's say manny = 0111 1011

    if I AND moe with %1100 0000 I get 0000 0000

    If I logical shift right manny by 2 I get 0001 1110

    If I then Bitwise OR 0000 0000 with 0001 1110 I get 0001 1110 and this
    value get placed in moe

    This look right?

    thanks,

    Leroy

    Moving something other than 1, 4 or 8 bits requires a bit of math or
    logical operations. Let's say you want to move the upper six bits of
    manny into the lower six bits of moe. The upper two bits of moe must be
    maintained. This is one way do it:

    moe = (moe & %11000000) | (manny >> 2)

    Please consult the manual if some of these operators are new to you.
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-30 22:42
    Yes, your example is correct. The easiest way to confirm this is with a bit
    of code and using DEBUG statements along the way to show you the status of
    things.

    Just be aware that there is a difference between bit-wise operators and
    logical operators.

    -- Jon Williams
    -- Parallax

    In a message dated 3/30/02 3:56:19 PM Central Standard Time, leroy@f...
    writes:


    > Your example:
    >
    > moe = (moe & %11000000) | (manny >> 2)
    >
    > In English this says: moe = (moe AND %11000000 OR (manny (Logical Shift
    > right by 2)))
    >
    > Does this:
    >
    > Let's say moe= 0001 1011
    > Let's say manny = 0111 1011
    >
    > if I AND moe with %1100 0000 I get 0000 0000
    >
    > If I logical shift right manny by 2 I get 0001 1110
    >
    > If I then Bitwise OR 0000 0000 with 0001 1110 I get 0001 1110 and this
    > value get placed in moe
    >
    > This look right?
    >
    > thanks,
    >
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-31 04:55
    Dear Jon,

    These byte variables i.e. they are 8 bits wide or

    B7 B6 B5 B4 B3 B2 B1 B0

    Suppose we have this:

    manny VAR Byte

    is it possible to output B3 B2 B1 B0 to pins 4 5 6 7, or must I do it
    one pin at a time?

    thanks in advance for your help..

    Leroy
  • ArchiverArchiver Posts: 46,084
    edited 2002-03-31 05:48
    Yes. This stuff is covered pretty well in the manual so I'll try to be brief.

    The architecture of the PIC and SX chips causes the I/O pins to behave like
    variables. The BASIC Stamp has three (16-bit) variables that are associated
    with I/O pins: Dirs (pin directions), Outs (outputs) and Ins (inputs).

    All three variables are conveniently aliased into managable chunks (bytes,
    nibbles and bits). Dirs, for example:

    Dirs (16 bits)
    DirL and DirH (8-bits ea.)
    DirA, DirB, DirC and DirD (4 bits ea.)
    Dir0 .. Dir15 (1 bit ea.)

    The same holds true for Ins and Outs.

    To get to your specific question, first you need to set Pins 4 - 7 as
    outputs. Here's how:

    DirB = %1111

    Then you can do this:

    statusLeds VAR OutB ' alias for output
    pins 4 - 7
    manny VAR Byte

    .. finally...

    statusLeds = manny.LowNib

    Actually, the LowNib modifier isn't required -- since statusLeds is only
    four-bits wide the program will take the lower four bits of manny. It's best
    to be clear though.

    HTH.

    -- Jon Williams
    -- Parallax


    In a message dated 3/30/02 9:55:23 PM Central Standard Time, leroy@f...
    writes:


    > Dear Jon,
    >
    > These byte variables i.e. they are 8 bits wide or
    >
    > B7 B6 B5 B4 B3 B2 B1 B0
    >
    > Suppose we have this:
    >
    > manny VAR Byte
    >
    > is it possible to output B3 B2 B1 B0 to pins 4 5 6 7, or must I do it
    > one pin at a time?
    >
    >




    [noparse][[/noparse]Non-text portions of this message have been removed]
Sign In or Register to comment.