Shop OBEX P1 Docs P2 Docs Learn Events
how to alternate pins? — Parallax Forums

how to alternate pins?

mikeamikea Posts: 283
edited 2012-05-12 08:28 in BASIC Stamp
Hi,
is there a way to alternate pins each time through a code. In other words pin 3 high/low the first time through the code, i would like to add pin 4 high/low the second time through. i thought toggle would accomplish this, but it seems to only deal with a single pin and toggle its state, not switch between two pins.-mike
Tag_Found:

  GOSUB Show_Name                    ' print name
  HIGH Latch                                    ' remove latch ,pin3
  pause 500
  LOW Latch                                     ' restore latch ,pin3
  GOTO Main

Comments

  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2012-05-12 08:11
    Hi, there are three things you need to do, one is Google the binary number system, two look at the DIRS,OUTS and INS in the PBasic help file under Memory and Variables and three look at PBasic operators in the PBasic help file.

    The OUTS,OUTA,OUTB,OUTC,OUTD,OUTL and OUTH are all instructions that directly affect the status of the I/O pins PO through P15. Simultaneously which is a really nice feature.

    OUTS is a 16 bit register, in notation the right hand side is the least significant bit (LSB) so P3 is the 4th bit from the right (P0 P1 P2 P3 = 4 bits). To set P3 high you would make the 4th bit a 1 eg: 0000000000001000 this binary number is equivalent to decimal 8. To set it low you would make bit 4 a zero eg: 0000000000000000 = dec 0

    With different values assigned to OUTS you can set any combination of outputs to high or low just by giving the respective bits a 1 or a 0.

    In your situation where you want to move onto the next output after each succesive loop through the program you could use the binary operator Bitshift Left which is represented "value=value<<1". Bitshifting left has the following effect, for example shifting bit 4 we would start with 0000000000001000 (dec 8 ) and end up with 0000000000010000 (dec 16 ).

    Here is an example of scrolling through all 16 ports setting each to 1 and then zero. To set the OUTS register to zero there is another operator used called XOR.
    DIRS =$FF
    
    x VAR Word
    Port VAR Byte
    x=1                 'Initialize x to P0
    
    DO
    PAUSE 500
    OUTS =x             'assign x to the OUTS register
    DEBUG "P",DEC Port,"  "
    DEBUG DEC ? OUTS   'display the port status
    OUTS= OUTS^x        'set the high port low with an XOR mask
    PAUSE 500           
    DEBUG "P",DEC Port,"  "
    DEBUG DEC ? OUTS   'display the port status
    PAUSE 500
      IF x<32768 THEN
        x=x<<1          'while x < 32768 (P15) keep bit shifting left
      ELSE
        x=1             'or else reset to P0
        Port=0
      ENDIF
    Port=Port+1
    LOOP
    
      'OUTS values
      'P0=1  P1=2  P2=4  P3=8  P4=16  P5=32  P6=64  P7=128
      'P8=256  P9=512  P10=1024  P11=2048 P12=4096 P13=8192
      'P14=16384  P15=32768
    

    This all might seem a little daunting at first sight but if you can grasp an understanding of controlling the DIRS, INS and OUTS registers then I think you will have control of one of the more powerful features of the BS2

    Jeff T.
  • mikeamikea Posts: 283
    edited 2012-05-12 08:28
    perfect, thank you Jeff T. -mike
Sign In or Register to comment.