Shop OBEX P1 Docs P2 Docs Learn Events
Multiple HIGH commands — Parallax Forums

Multiple HIGH commands

NB_NesquikNB_Nesquik Posts: 35
edited 2012-10-26 12:17 in BASIC Stamp
Hi,
I know this may sound like a stupid question, but is there a way to have multiple HIGH commands on one line of code?
So instead of having:

HIGH 15
HIGH 14
HIGH 13
HIGH 12

It would all be put on one line. I think I have read somewhere how to do this but I cannot find where I read it at. Thanks for your help.

Comments

  • ZootZoot Posts: 2,227
    edited 2012-10-06 09:18
    OUTS = OUTS | %1111000000000000 ' set pin 12-15 high
    OUTS = OUTS & %0000111111111111 ' set pin 12-15 low

    or

    OUTD = %1111 ' set pin 12-15 high
    OUTD = %0000 'set pin 12-15 low

    etc

    See OUTS, INS, and their brethren OUTA, INA, OUTB, INB, etc. in the Pbasic Manual.

    The above presume the pins are already outputs (HIGH makes pin an output as well). If the pins are not outputs, you need a second statement:
    OUTD = %1111 ' pin 12-15 will be high if they are outputs
    DIRD = %1111 ' make pins 12-15 outputs
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2012-10-06 09:20
    Check out the following commands:
    OUTS    - for WORD ; bits 0 to 15
    OUTL	- for BYTE ; bits 0 to 7
    OUTH    - for BYTE ; bits 8 to 15
    OUTA	- for NIBBLE ; bits 0 to 3
    OUTB    - for NIBBLE ; bits 4 to 7
    OUTC    - for NIBBLE ; bits 8 to 11
    OUTD    - for NIBBLE ; bits 12 to 15
    

    Edit: Dang Zoot beat me to it :-)
  • NB_NesquikNB_Nesquik Posts: 35
    edited 2012-10-06 11:12
    Wow, you guys respond really quickly. Thanks for the info.
  • NB_NesquikNB_Nesquik Posts: 35
    edited 2012-10-06 11:19
    I've got another question. Is there a way to have a number be input and the binary form of it be given in the OUTS form? It would be like having the user type in a number and then the program would light up a row of LEDs to match the corresponding binary digits.
  • ElectrodudeElectrodude Posts: 1,646
    edited 2012-10-06 13:18
    I haven't used a BS2 in a while, but to put a byte on the bottom 8 io pins in binary:
    number var byte
    
    dirl=111111
    do
     gosub get_number
     outl=number
    loop
    
    get_number:
     'get the number somehow
    return
    
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2012-10-06 14:31
    To fill in the "'get the number somehow", assuming you want to get it from the terminal
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    myByte VAR BYTE
    DIRL = $FF
    DO
     DEBUGIN BIN8 myByte
     OUTL = myByte
    LOOP
    
  • NB_NesquikNB_Nesquik Posts: 35
    edited 2012-10-07 11:18
    Ok, so with your guy's help, I got the program to do the binary for the bottom 8 I/O pins like Electrodude said, but is there any way to get the binary on more than the bottom 8? Also @Tracy Allen, what does the BIN8 command do? I have tried putting it in my program in variouse places, but it always has some problem with it.
  • ZootZoot Posts: 2,227
    edited 2012-10-07 13:15
    BIN8 means that incoming data (which is *always* ASCII) is converted to "binary" 1s and 0s. If you enter "11110000" you are not really entering the binary "byte" $F0 -- you are entering 8 bytes, where each byte is either the ASCII character "0" or the ASCII character "1". What the BIN8 command does is automatically take the next 8 characters and convert each character to a binary 1 or 0 then put the resulting 8 bits into a single byte.

    To set all 16 pins low/high or any combo, use DIRS and OUTS.

    OUTS = word value, all 16 pins
    OUTL = byte value, pins 0-7
    OUTH = byte value, pin 8-15
    OUTA = nib value, pins 0-3
    OUTB = nib value, pins 4-7
    OUTC = nib value, pins 8-11
    OUTD = nib value, pins 12-15
    OUT0 = pin 0
    ...
    OUT15 = pin 15
  • NB_NesquikNB_Nesquik Posts: 35
    edited 2012-10-07 15:14
    I have tried to use the OUTS command and it works if I use it like OUTS = OUTS|%1011010111000000, but it doesnt work in my program like OUTS = number. I want the number to be Read from a Data group at the beginning of the program. It can go up to 8 digits but it wont go any more than that. Heres my program:

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    originaltime VAR Word
    newtime VAR Word
    number VAR Word
    index VAR Word
    number_storage DATA 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768 "Q"
    time_storage DATA 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
    OUTS = %0000000000000000
    DIRS = %1111111111111111 'set all pins HIGH
    DO
    GOSUB get_number
    GOSUB get_time
    OUTS = number
    PAUSE newtime
    OUTS = OUTS&%0000000000000000 'turn off leds
    PAUSE 100
    LOOP
    get_number:
    READ number_storage + index, number
    IF (number = "Q") THEN
    GOTO finish 'stop program when "Q" is reached
    ENDIF
    RETURN

    get_time:
    READ time_storage + index, originaltime
    newtime = originaltime * 100 'make time a multiple of a tenth of a second
    index = index + 1
    RETURN
    finish:
    DEBUG "Program ended"
    END

    I dont know what I am missing. Please help. Thanks.
  • ZootZoot Posts: 2,227
    edited 2012-10-07 15:27
    You need to do
    DATA Word 11234, Word %1010101010101010
    

    otherwise you will have bytes. Your read also would need to be
    READ Address, Word WordSizedVariable
    
  • NB_NesquikNB_Nesquik Posts: 35
    edited 2012-10-07 20:31
    I tried adding Word where you said but it didnt work right, the leds flashed randomly. Heres the changes I made to the code:

    number_storage DATA Word 1,2,4,8,16,ect

    And

    READ number_storage + index, Word number

    I think I did it right, or did I?
  • SapphireSapphire Posts: 496
    edited 2012-10-07 21:29
    No. You need "Word" in front of each number in the DATA statement, and you need to multiply the index by 2 in the READ statment.
    number_storage DATA Word 1, Word 2, Word 4, Word 8, Word 16, Word 32, Word 64, Word 128, Word 256, Word 512, Word 1024, Word 2048, Word 4096, Word 8192, Word 16384, Word 32768, Word "Q"
    

    and
    READ 2*index + number_storage, Word number
    
  • NB_NesquikNB_Nesquik Posts: 35
    edited 2012-10-08 07:13
    THANK YOU! It finally worked right. Thanks to all of you for helping.
  • vaclav_salvaclav_sal Posts: 451
    edited 2012-10-10 15:54
    Nothing wrong with your program, but...
    The binary data sequence is ripe to be replaced with ... multiply "number " by 2...
    Just a sugestion.
  • NB_NesquikNB_Nesquik Posts: 35
    edited 2012-10-26 09:00
    I just want to say thanks for all the help you guys have been, but I have another question. Can the number and time be stored in the same DATA set? Heres my program Im trying to use, but the program will never read the right number. I have tried multiplying the indexes in number and time subroutines, but nothing seems to work right.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    originaltime VAR Word
    newtime VAR Word
    number VAR Word
    index VAR Word


    number_storage DATA Word 1,10, Word 2,10, Word 4,10, Word 8,10, Word 16,10,
    Word 32,10, Word 64,10, Word 128,10, Word 256,10, Word 512,10, Word "Q"


    OUTS = %0000000000
    DIRS = %1111111111 'set all pins HIGH


    DO
    GOSUB get_number
    GOSUB get_time
    OUTS = OUTS&%0000000000 'turn off leds from previous round
    OUTS = number
    PAUSE newtime
    LOOP


    get_number:
    READ 2 * index + number_storage, Word number
    IF (number = "Q") THEN
    GOTO finish 'stop program when "Q" is reached
    ENDIF
    RETURN




    get_time:
    PAUSE 500
    READ number_storage + 3 * index, originaltime
    newtime = originaltime * 50 'make time a multiple of a 20th of a second
    index = index + 1
    RETURN


    finish:
    OUTS = OUTS&%0000000000 'turn off leds
    DEBUG "Program ended"
    END

    Thanks for all your help.
  • ZootZoot Posts: 2,227
    edited 2012-10-26 12:17
    Each pair is three bytes, so your read under "get_number" should be like this:
    READ 3 * index + number_storage, Word number
    

    Then, your read for getting the second "number", or third byte of each pair, should be like below. Remember, the second number of each pair is a byte, and it will be 2 bytes up from the current index:
    READ 3 * index + number_storage + 1, originaltime
    

    Aside from not reading the proper byte, your code had "number_storage + 3 * index" which is NOT the same as "3 * index + number_storage". It helps to put parentheses in, sometimes, to make clear the order of operations:

    Yours:
    ( number_storage + 3 ) * index ' not what you want
    

    Correct:
    number_storage + ( 3 * index ) ' not what you want
    

    Personally, I wouldn't have to separate routines for the read, I'd just do it all at once and advance the index by 3:
    get_data:
    READ number_storage + ( 3 * index), Word number, originaltime
    IF (number = "Q") THEN
       GOTO finish 'stop program when "Q" is reached  ' off topic, but a goto in subroutine before a return is a bad habit
    ENDIF
    newtime = originaltime * 50 'make time a multiple of a 20th of a second
    index = index + 3
    RETURN
    
Sign In or Register to comment.