Shop OBEX P1 Docs P2 Docs Learn Events
Ok... How to access the PINS as an array — Parallax Forums

Ok... How to access the PINS as an array

Kaos KiddKaos Kidd Posts: 614
edited 2006-03-13 19:11 in BASIC Stamp
Greets everyone.· Now that my new laptop and I have been aquainted, and thusly tought how to communicate with the BS2P that I own, A simmingly small thing, but apperently something I cant quite get·the grasp of issue has arrised.

What I'm trying to do is duplicate Beau's "shaker" program.· Sounds simple enough.· NOT!
First off all, I can't figure out how to address the output pins as if they are an array.
Once I get that right, I might be able to get the rest worked out, but my application is based on the following...

#· 1: Get the current time in 24 hour format· (not implimented yet, using a temp "00:00:00" string).
#· 2: For each character in the output string:
#· 3:··· compute the correct offset to the first bit in the array of bits containing the bit maps of the numbers· (each number is 5X7 bits long)
#· 4:··· For each Column in the character's bit array:· (there are 5 columns)
#· 5:····· For each Row in the character's bit array:··· (there are 7 rows)
#· 6:······· Set the correct PIN to the state found in the array at this offset.·· <--- this step is the one giving me the achs right now!
#· 7:····· Next Row············
#· 8:···Pause slightly so the entire column can be viewed
#· 9:·· Next Column
#10: Pause slightly so there is a space between each charactor being displayed
#11: Loop

Ok, this sounds simple enough, but I can't get past the first hump, addressing the PINs as if there were bits in an array.
The next hump I'll need to jump is taking the output from the RTC, ensure it's formatted in "00:00:00" format, and send it to this routine.

I've put the code inline here, as well as an attachment.· I'm sure you grueus will spot a number of errors, and I welcome any and all crits and comments on it, as well as shortcuts or better ways to do things (for example, the MOD command at the bottom of the loop, I know there's a better way, I·just cant find it [noparse]:)[/noparse] ), Please let me know.· I also think I need to work on my method of decoding what number to display (look at the lookdown / up combination).· Again, once I can get the heart of the program working, I'll get lots more done.


' {$STAMP BS2p}
' {$PBASIC 2.5}
'  Shaker clock by Fred Kerber
'  Special thanks to David and Beau for posting and coding the SPIN equevlent!
 
  'Program vars
  Pointer            VAR     Byte                                                 'pointer into current number being displayed
  Offset             VAR     Byte                                                 'Index into output string
  CurrentChr         VAR     Byte                                                 'index into current char we are displaying
  Columns            VAR     Byte                                                 'Holds current column
  Rows               VAR     Byte                                                 'holds the current row
  Text               DATA    "00:00:00"                                           'Temp data (soon to be RTC data)
  InterCharPause     CON     50                                                   'pause for inter charactor display
  InterColumnPause   CON     10                                                   'pause for inter column display
  SpacerPause        CON     100                                                  'Pause for the ":" in the time format
  OutLength          CON     8                                                    'Length of output string
 
  INIT:
    DIRS = %1111111000000000                                                      'Set pins 0~7 = outputs
    OUTH = %0000000000000000                                                      'set all pins to low
    Pointer = 0                                                                   'Set pointer to first char in Text
 
  MAIN:
    DO                                                                            'Main program loop
      CurrentChr = Text[noparse][[/noparse]Pointer]                                                  'Get the next position into the array 'text'
      LOOKDOWN CurrentChr, [noparse][[/noparse]48,49,50,51,52,53,54,55,56,57,58],Offset              'Get ascii to index offset
      LOOKUP Offset,[noparse][[/noparse]Numbers],Offset                                              'Convert index
      FOR Columns = 0 TO 4                                                        '5 columns
        FOR Rows = 0 TO 6                                                         '7 rows
          OUTH[noparse][[/noparse]Rows] = Numbers[noparse][[/noparse] ((Offset * 34) + ( Columns * 5 ) + Rows) ]        'Set the pin
        NEXT Rows                                                                 'Finish all 7 rows...
        PAUSE InterColumnPause                                                    'temp pause to verify
      NEXT Columns                                                                'Finish all the columns
      PAUSE InterCharPause                                                        'Pause for a space between each char
      Pointer = (Pointer + 1) // OutLength                                        'move to next char, looping at end
    LOOP

  'Display data.  Each number is a 5 X 7 array
  Numbers DATA  "01110100011000110001100011000101110"                              'Number 0
          DATA  "00100011000010000100001000010011111"                              'Number 1
          DATA  "01110100010000100010001000100011111"                              'Number 2
          DATA  "01110100010000100110000011000101110"                              'Number 3
          DATA  "00010001100101010010111110001000010"                              'Number 4
          DATA  "01111100001000001110000010000101110"                              'Number 5
          DATA  "01110100011000010110110011000101110"                              'Number 6
          DATA  "11111000100010001000100001000010000"                              'Number 7
          DATA  "01110100011000101110100011000101110"                              'Number 8
          DATA  "01110100011001101101000011000101110"                              'Number 9
          DATA  "00000001000010000000001000010000000"                              'Character ':'

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket


KK

Comments

  • Lee HarkerLee Harker Posts: 104
    edited 2006-03-09 20:18
    Kaos,
    I think you are doing more than you have to to get the character patterns. You have a list of numbers in your data statements and although they "look" like binary to our eyes, each of those ones and zeros take up a whole byte of space. I notice you are looking at the character matrix as horizontal slices 7 times for the whole character. You may try to think of things a little differently. Imagine the character as 5 vertical slices. 7 bits fit nicely in a byte and it only takes 5 bytes to represent one character.
    For example the character "0" can be represented by, $3E, $41, $41, $41, $3E.

    0 0 0 0 0
    0 1 1 1 0
    1 0 0 0 1
    1 0 0 0 1
    1 0 0 0 1
    1 0 0 0 1
    1 0 0 0 1
    0 1 1 1 0

    See each of the hexidecimal bytes above will represent all the leds in a column. You can just ignore the data in the MSB. Lots less data to store. Also it takes less time to display 5 columns than it does to display 7 rows.
    Hope this helps.

    Lee
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-03-09 20:33
    Hmmm, very interesting point... I'll check that out ASAP... THanks Lee...
    But still, the actual setting of the outputs I can't seem to fine the answer... but I'll pump more time into it later today...

    THanks again!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-03-09 21:29
    Lee, your right...
    By adjusting the way I stored the bit patterns, It makes better usage of memory, is easier to read, and I bet I can figure out how to set the pins now... [noparse]:)[/noparse]
    Thanks...

    (Ok, I've done a lot of editing, and later I'll repost the code... even if I get it to work..· [noparse]:)[/noparse]· )



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK


    Post Edited (Kaos Kidd) : 3/9/2006 9:49:11 PM GMT
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-03-13 14:41
    Following Lee's advise, I rewritten the program.
    I've added vars to assist in the debugging of the app, but still I'm somewhat lost.
    I can get it to compile, but It's not doing the lookup correctly.

    In particular, the line:

    LOOKUP Pointer,[noparse][[/noparse]Text],CurrentChr····'Get current char

    What I was expecting is the ASCII number of the at the position Pointer.
    WHat I get, no matter how I try it, its returning·0, which is causing the rest of the app to misbehave.

    Now, if I change the line to:

    LOOKUP Pointer,[noparse][[/noparse]"00:00:00"],CurrentChr··········································· 'Get current char
    It works as expected.

    I'm sure it's how I'm dealing with the data, but I'm not sure how else to proceede.·
    The end result is I think need the ASCII for each digit so I can display them.· The last stages of this app is to insert the RTC and have it supply the time, then make the bottom mouted pendulum to hold the leds.· It's for this reason I think· I need the ascii, so I can break down the output of the RTC correctly.·

    BTW, the app does work, I recenty started working on the pauses to get it to look right, but I ran out of time.
    For what it's worth, here's the code.· Any crits or comments, OR HELP would be great!

    KK

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    '  Shaker clock by Fred Kerber
    '  Special thanks to David and Beau for posting and coding the SPIN equevlent!
     
    'Program vars
      Pointer            VAR     Byte                                                 'pointer into current number being displayed
      Offset             VAR     Byte                                                 'Index into output string
      CurrentChr         VAR     Byte                                                 'index into current char we are displaying
      Columns            VAR     Byte                                                 'Holds current column
      InterCharPause     CON     50                                                   'pause for inter charactor display
      InterColumnPause   CON     10                                                   'pause for inter column display
      OutLength          CON     8                                                    'Length of output string
      Index              VAR     Byte                                                 'Index into memory for current read
      BaseIndex          VAR     Byte                                                 'Base index (calculated non changing part)
      TheData            VAR     Byte                                                 'Output Data
     
      INIT:
        DIRS = %0000000011111111                                                      'Set pins 0~7 = outputs
        OUTS = %0000000000000000                                                      'set all pins to low
        Pointer = 0                                                                   'Set pointer to first char in Text
     
      MAIN:
        DO                                                                            'Main program loop
          LOOKUP Pointer,[noparse][[/noparse]Text],CurrentChr                                            'Get current char
          Offset = CurrentChr - 48                                                    'Take ASCII and make #
          BaseIndex = Offset * 5                                                      '5 bytes in each column
          FOR Columns = 0 TO 4                                                        '5 columns
            Index = BaseIndex + Columns                                               'Get the offset for this set of bits
            READ Index, TheData                                                       'Read the data
            OUTS = 0 + TheData                                                        'Set the output bits
            PAUSE InterColumnPause                                                    'give time for column to be seen
          NEXT                                                                        'Finish all 5 columns
          PAUSE InterCharPause                                                        'Pause for a space between each char
          Pointer = (Pointer + 1) // OutLength                                        'move to next char, looping at end
        LOOP
     
      'Display data.  Each number is a 5 X 7 array
      Numbers DATA 062,065,065,065,062                                                'Number 0
              DATA 000,066,127,064,000                                                'Number 1
              DATA 098,081,073,069,002                                                'Number 2
              DATA 034,065,065,073,054                                                'Number 3
              DATA 048,040,036,098,063                                                'Number 4
              DATA 046,073,073,073,049                                                'Number 5
              DATA 062,073,073,073,048                                                'Number 6
              DATA 001,113,009,005,003                                                'Number 7
              DATA 054,073,073,073,054                                                'Number 8
              DATA 006,073,073,073,063                                                'Number 9
              DATA 000,054,054,000,000                                                'Seperator
      Text    DATA "00:00:00"                                                         'Display Data
    


    And just for the record, can someone point out the ovious 'Mask' needed to AND my output with the OUTS var so as to NOT change any of the other pins ?



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
  • Beau SchwabeBeau Schwabe Posts: 6,557
    edited 2006-03-13 17:19
    Kaos Kidd,

    See if this helps...· The problem that I saw was that the data was indexed incorrectly.

    Your·line that reads...

    LOOKUP Pointer,[noparse][[/noparse]Text],CurrentChr····'Get current char

    ...Does NOT point to what is in Text.· In fact, you don't even need to use the LOOKUP command.
    All you need to do is use the READ command with the proper·pointer locations.· By changing your
    LOOKUP command to this...

    READ (Text + Pointer),CurrentChr

    ...was all that was really necessary.· The only other line I changed was the line that reads...

    BaseIndex = Offset * 5

    ...I changed this to...

    BaseIndex = Offset * 5 + Numbers

    ...in case you added more DATA above "Numbers" you would still be able to maintain a reference.
    You said...
    As far as getting it to "look right", I had originally made mention the use of an accelerometer, so that you had some sort of reference
    in space as to when you switched direction (shaking).· Without some sort of reference point, you will be limited to symetrical patterns
    that you can display and be able to percieve with any meaning.· "00:00:00" <--Seems to work, because it looks the same forward as it
    does backward, thus it is a symetrical pattern.
    As far as getting it to "look right", I had originally made mention the use of an accelerometer, so that you had some sort of reference
    in space as to when you switched direction (shaking).· Without some sort of reference point, you will be limited to symmetrical patterns
    that you can display and be able to perceive with any meaning.· "
    00:00:00" <--Seems to work, because it looks the same forward as it
    does backward, thus it is a symmetrical pattern.

    You also said...
    And just for the record, can someone point out the ovious 'Mask' needed to AND my output with the OUTS var so as to NOT change any of the other pins ?
    Typically you OR the data to not change any of the other pins.
    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}
    '  Shaker clock by Fred Kerber
    '  Special thanks to David and Beau for posting and coding the SPIN equevlent!
      'Program vars
      Pointer            VAR     Byte                                                 'pointer into current number being displayed
      Offset             VAR     Byte                                                 'Index into output string
      CurrentChr         VAR     Byte                                                 'index into current char we are displaying
      Columns            VAR     Byte                                                 'Holds current column
      InterCharPause     CON     50                                                   'pause for inter charactor display
      InterColumnPause   CON     10                                                   'pause for inter column display
      OutLength          CON     8                                                    'Length of output string
      Index              VAR     Byte                                                 'Index into memory for current read
      BaseIndex          VAR     Byte                                                 'Base index (calculated non changing part)
      TheData            VAR     Byte                                                 'Output Data
      INIT:
        DIRS = %0000000011111111                                                      'Set pins 0~7 = outputs
        OUTS = %0000000000000000                                                      'set all pins to low
        Pointer = 0                                                                   'Set pointer to first char in Text
      MAIN:
        DO                                                                            'Main program loop
          READ (Text + Pointer),CurrentChr                                            'Read Character                             -Edited by Beau Schwabe
          Offset = CurrentChr - 48                                                    'Take ASCII and make #
          BaseIndex = Offset * 5 + Numbers                                            '5 bytes in each column                     -Edited by Beau Schwabe
          FOR Columns = 0 TO 4                                                        '5 columns
              Index = BaseIndex + Columns                                             'Get the offset for this set of bits
              READ Index, TheData                                                     'Read the data
              OUTS = 0 + TheData                                                      'Set the output bits
              'DEBUG DEC Pointer," - ",DEC TheData,"          ",CR                    'DEBUG test                                 -Edited by Beau Schwabe
              PAUSE InterColumnPause                                                  'give time for column to be seen
          NEXT                                                                        'Finish all 5 columns
          PAUSE InterCharPause                                                        'Pause for a space between each char
          Pointer = (Pointer + 1) // OutLength                                        'move to next char, looping at end
          'IF Pointer=0 THEN                                                          'DEBUG test                                 -Edited by Beau Schwabe
          '   DEBUG HOME                                                              'DEBUG test                                 -Edited by Beau Schwabe
          'ENDIF                                                                      'DEBUG test                                 -Edited by Beau Schwabe
        LOOP
      'Display data.  Each number is a 5 X 7 array
      Numbers DATA 062,065,065,065,062                                                'Number 0
              DATA 000,066,127,064,000                                                'Number 1
              DATA 098,081,073,069,002                                                'Number 2
              DATA 034,065,065,073,054                                                'Number 3
              DATA 048,040,036,098,063                                                'Number 4
              DATA 046,073,073,073,049                                                'Number 5
              DATA 062,073,073,073,048                                                'Number 6
              DATA 001,113,009,005,003                                                'Number 7
              DATA 054,073,073,073,054                                                'Number 8
              DATA 006,073,073,073,063                                                'Number 9
              DATA 000,054,054,000,000                                                'Seperator
      Text    DATA "00:00:00"                                                         'Display Data
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Beau Schwabe

    IC Layout Engineer
    Parallax, Inc.
  • Kaos KiddKaos Kidd Posts: 614
    edited 2006-03-13 19:11
    Beau...
    Thanks for pointing the OR and the incorrect indexing. That did the trick. What I did next was move the whole thing to scratchpad ram (out of eeprom) so when the rtc starts updating it, I don't endup killing the eeprom cell. Well, your indexing 'fix' works here as well. I'v coded my first BCD to ASCII routine, and made the allocations for the RTC (PFC8583). Once I get this done, it's time for the 'hardware' of the project...
    At some point I'll recover some VAR ram and recycle some of the VAR's used, to make room for the last two code segments (motor control for the pendulem) and sensor
    inputs (to indicate the start of a left-> right swing, or the start of a right<-left swing).

    Thanks again for the help... I'll include you in the credits!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Just tossing my two bits worth into the bit bucket


    KK
    ·
Sign In or Register to comment.