Shop OBEX P1 Docs P2 Docs Learn Events
OUT command question — Parallax Forums

OUT command question

Spiral_72Spiral_72 Posts: 791
edited 2010-12-24 22:30 in BASIC Stamp
I'm playing with the BS2....

The following code doesn't work, but I hope it's pretty apparent what I want to do. How else can I do this?
x   VAR   Byte

FOR x=7 TO 4
OUTx = 1
NEXT

The "1" can actually be different values read from an array.

Comments

  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-21 19:59
    In plain ol' BASIC I'd have a base address: 100h for example then add a pointer.

    FOR x=7 TO 4

    OUT &h100+x, 1
    NEXT X

    but in PBASIC I can't find a way to do that just yet.

    Actually that was a bad example, but hopefully you still understand the question
  • FranklinFranklin Posts: 4,747
    edited 2010-12-21 21:07
    OUTx = 1
    Can you explain what this does and what your program does not do?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-12-21 23:04
    if you want to address the outputs as an array, try the following:
    FOR x = 7 to 4
         OUT0(x) = 1
         PAUSE 1000
    NEXT
    


    OUT0(7) is the same as OUT7 etc.
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-22 05:55
    Tracy,

    Thank you! That's brilliant... I spent a lot of time in the manual last night and didn't see this!
  • davejamesdavejames Posts: 4,047
    edited 2010-12-22 10:26
    Spiral - you didn't see it because it's not there; one of them thar undocementad thingums.

    If I could find a previous email thread, I'd copy the information related from Mr. Allen on how the I/O bits are treated like an array (maybe he'll read this and comment again).

    Regards,

    DJ
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-22 11:15
    Oh, thanks Dave.... That's awesome news and makes my routine even tighter. I went from hard coded and lots of EEPROM space to this:
    (The program wont run as is... the data must be loaded into an array first!)
    'Layout the array as follows:
    Keyp    1,2,3,F,
            4,5,6,E,
            7,8,9,D,
            A,0,B,C
    
    x    VAR    nib
    y    VAR    nib
    
    Kloop:
    FOR x=0 TO 3
        OUT0(x)=1
        FOR y=4 TO 7
            IF IN0(y)=1
                DEBUG Keyp(X*4 +Y -4)
        NEXT
        OUT0(x)=0
    NEXT
    GOTO Kloop
    
    keyp.jpg
    794 x 632 - 81K
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-22 11:18
    I can't wait to get home to try it now :)

    It's a 16 button keypad read routine, which if I have it figured right will report simultaneous button presses. SWEET

    I'm trying to make a BigTrak..... just like we used to have as kids.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-12-22 12:42
    You probably discovered this aready, but the program will need to set the pin directions to outputs:
    DIRA = %1111
    That before the OUTx commands to those pins will be effective.

    Another way to set one row at a time high uses OUTA for pins p0 to p3, and the DCD operator.
    DIRA = $f
    DO
      FOR x=0 TO 3
        OUTA = DCD x    ' set one row at a time to high level, p0 to p3
        DEBUG CR, bin4 INB  ' read bits on p4 to p7
      NEXT
    PAUSE 1000
    LOOP
    
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-22 13:22
    Hmmmm, that certainly is cleaner. With that method I'll have to logically test each of the four bits separately after I have my INB result.

    IF (INB AND 1) = 1 THEN DEBUG "Button 1 pressed"
    ......
    IF (INB AND 8) = 1 THEN DEBUG "Button F pressed"


    I plan to store the operator entered steps in an array, with the first token as a direction, followed by a time from 0-9. I hope to store eight steps, which should be 8*2 nibbles or 16 bytes. Anyways, I suppose that's outside the reason for this post.



    Thanks to all for the help!
  • davejamesdavejames Posts: 4,047
    edited 2010-12-22 13:50
    ...or use SELECT/CASE:
       SELECT INB
          CASE $0001
             DEBUG "Button 1 Pressed"
          CASE $0010
             DEBUG "Button 2 Pressed"
          .
          .
          .
          CASE $1000
             DEBUG "Button 8 Pressed"
       ENDSELECT
    

    I hope the syntax is correct (pulling if from memory after having a HUGE lunch! zzZZZZzzzzZZ)
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-12-22 14:50
    It may be useful to put the scanning and the decoding in separate subroutines. I show this as another application of how to use implicit arrays. This time the state of all 16 keys are stored in one word variable, and the scan routine accesses that word variable as an array of nibs, an subsequently the decode routine accesses it as an array of bits.
    x VAR Nib
    keys VAR Word
    keyA  VAR keys.nib0
    key0 VAR keys.bit0
    DIRA = $f
    
    DO
         GOSUB keyScan
         DEBUG CR, IBIN16 keys
         IF keys THEN GOSUB keyDecode 
      PAUSE 256
    LOOP
    
    keyScan:
         FOR x=0 TO 3
             OUTA = DCD x    ' set one row at a time to high level, p0 to p3
             keyA(x) = INB  ' read bits on p4 to p7, keys as an array of nibs
         NEXT
         RETURN
    
    keyDecode:
      DEBUG CR, "The keys pressed are: "
         FOR x=0 TO 15
             IF key0(x) THEN   ' read individual bits of word keys as array  
         DEBUG DEC x, " "
           NEXT
         RETURN
    
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-22 19:06
    I have a preliminary working routine. I think it will make a lot of sense to use Mr. Allen's idea for the final product because the checks and routines will be easier to call. Either way, it works.

    I dunno how to display a hex number, so key "A" shows as 10, "B" as 11 and so on.

    I could easily call this routine on demand. I'd probably check periodically for a keypress by bringing all the outputs high, and look for a non-zero on INA, then call this routine if needed.
    ' {$STAMP BS2px}
    ' {$PBASIC 2.5}
    
    'Layout the array as follows:   (Note that $A will print as 10, $B as 11, etc)
    Kyval DATA  1,2,3,$F
          DATA  4,5,6,$E
          DATA  7,8,9,$D
          DATA  $A,0,$B,$C
    
    keyp  VAR Nib(16)
    ptr VAR Word
    x  VAR  Nib
    y  VAR  Nib
    
    'I can use the DIR command once I settle on a circuit
    OUTPUT 7
    OUTPUT 6
    OUTPUT 5
    OUTPUT 4
    INPUT 3
    INPUT 2
    INPUT 1
    INPUT 0
    
    'Load Key value data into keyp array
    FOR x=0 TO 15
      READ ptr,keyp(x)
      ptr=ptr+1
      DEBUG DEC keyp(x)," "
    NEXT
    DEBUG CR, "Array loaded",CR
    PAUSE 2000                                  'Do nothing but hold the message
    
    
    GetKeyVals:
    FOR y=0 TO 3                                'Cycle through output lines P4 - P7
      OUT0(y+4)=1                               'Pull the line high
      FOR x=0 TO 3                              'Cycle through the input lines
        IF IN0(x)=1 THEN DEBUG DEC keyp(y*4 +x) 'Test current input line for high, blit key
      NEXT                                      'Next input line
      OUT0(y+4)=0                               'Close output line
    NEXT                                        'Next output line
    PAUSE 250                                   'A little key debounce
    GOTO GetKeyVals                             'or RETURN
    
  • davejamesdavejames Posts: 4,047
    edited 2010-12-24 09:20
    Spiral - look in the latest version of the Stamp syntax reference (v2.2) in the DEBUG chapter. There's a description of formatting characters that will meet your need.

    You can find it here: http://www.parallax.com/tabid/440/Default.aspx

    DJ
  • Spiral_72Spiral_72 Posts: 791
    edited 2010-12-24 16:00
    Dave, you're the man. "IHEX"...... never saw that before!
  • davejamesdavejames Posts: 4,047
    edited 2010-12-24 22:30
    ...more than welcome!

    I'm just happy to be able to help.

    DJ
Sign In or Register to comment.