Shop OBEX P1 Docs P2 Docs Learn Events
Define byte as array of bits — Parallax Forums

Define byte as array of bits

George99George99 Posts: 16
edited 2006-07-22 23:55 in BASIC Stamp
I think I've had too much COBOL!

I want to create an array of bits, i.e. bits VAR Bit(8), change bits as needed, then use the result as a byte.

This works:
' {$STAMP BS2}
' {$PBASIC 2.5}

bits VAR Bit(8)
bits(6)=1
bits(0)=1
debug B0 ' displays A

...but I really don't like using that B0 to reference the byte.

I tried all sorts of things with var aliases and modifiers but couldn't hit on anything that worked.

Is there a better way?

Post Edited (George99) : 7/22/2006 7:27:47 PM GMT

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-07-22 20:52
    George -

    "Thou shalt never have enough COBOL, until thy hand falls off from a repititive use injury"!
    EXACTLY one of the reasons I switched from applications programming to systems programming many decades ago.

    The "B0 restriction" is ONLY required on the PBASIC Stamp BS-1, The BS-2 series permits individual addressment, or group addressment. This example happens to use a WORD variable, but the same technique can be used with a BYTE variable:

    switches VAR Word

    sw0 VAR switches.bit0
    sw1 VAR switches.bit1
    sw2 VAR switches.bit2
    sw3 VAR switches.bit3
    sw4 VAR switches.bit4
    sw5 VAR switches.bit5
    sw6 VAR switches.bit6
    sw7 VAR switches.bit7
    sw8 VAR switches.bit8
    sw9 VAR switches.bit9
    sw10 VAR switches.bi10
    sw11 VAR switches.bit11
    sw12 VAR switches.bit12
    sw13 VAR switches.bit13
    sw14 VAR switches.bit14
    sw15 VAR switches.bit15

    Also remember that ANY PBASIC variable can be addressed as an array. An array (per se) isn't really a special declaration. An example is shown below:

    datum VAR WORD

    datum(0) = 5

    Is a legitimate reference to the first BYTE of datum. Remeber too all arrays start with index = 0!

    I hope that helps.

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 7/22/2006 8:56:15 PM GMT
  • George99George99 Posts: 16
    edited 2006-07-22 21:11
    switches VAR Word

    sw0 VAR switches.bit0
    .
    .
    .
    sw15 VAR switches.bit15

    So in this example I could refer to sw0(5) to get at sw5?

    Edited to say I just tried it and it works. Thanks.
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2006-07-22 23:00
    George -

    Happy to hear that! Any further assistance, just shout.

    Bruce

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    <!--StartFragment -->
  • George99George99 Posts: 16
    edited 2006-07-22 23:55
    Thanks to you and others who answered my earlier question about servos.

    I've always been interested in uC's but somehow stayed unaware of the Basic Stamp. I realize there must be more powerful controllers out there, but I thought the BS was a good starting place once I found the kit with book and Homework Board in Radio Shack.

    I'm not sure I actually have a real application in mind, but I'm sure after I've fooled around with it for a while something will come to mind. I thought I'd start with a morse code sender of some sort. Then try something more heroic like a morse code receiver though it looks like there's a lot to solve with the board having no on board timer or interrupts. That may have to wait for a different chip.

    I'm not worried about reinventing the wheel. I know there are probably 1,000 morse code programs out there, I've written them in Microsoft BASIC, Sinclair BASIC, TurboPascal, JavaScript, and COBOL. I guess it's my form of Hello World.

    I'm thinking of interfacing it to my GPS. Maybe some sort of location averaging device to use in geocaching to help smooth out all the last few feet jumping around that occurs.

    Having stated all that, you'll probably be hearing a few (dozen) more questions here.
Sign In or Register to comment.