Shop OBEX P1 Docs P2 Docs Learn Events
Controlling a Pin — Parallax Forums

Controlling a Pin

NewzedNewzed Posts: 2,503
edited 2006-06-23 18:20 in Propeller 1
I have a little program that says:
If InA[noparse][[/noparse]1] == Low'High······················ 'Be sure to indent under the If
····· SetScreenToWhiteOnDarkBlue
····· VideoDisplay.str(String("INPUT AT PIN A1"))
····· OutA[noparse][[/noparse]16] := High
··· Else··································· 'Be sure to indent under the Else
····· SetScreenToWhiteOnRed
····· VideoDisplay.str(String("NO INPUT"))
····· OutA[noparse][[/noparse]16] := Low
As long as I hold my PB down, the LED on Pin 16 lights, but if I let go of the PB the LED goes off.· Is there a "Toggle" equivalent in Spin.· I would like to press the PB to turn it on, then press it again to turn it off.

Sid
'····

Comments

  • CJCJ Posts: 470
    edited 2006-06-22 23:11
    I think it is the assignment not ! operator

    !OutA[noparse][[/noparse]16]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who says you have to have knowledge to use it?

    I've killed a fly with my bare mind.
  • NewzedNewzed Posts: 2,503
    edited 2006-06-22 23:54
    Sorry, CJ - that didn't work.· I tried all combinations of !OutA[noparse][[/noparse]16] and the best I could do was to dim the LED a little in one combination.

    Chapter 5, which is not out yet, is supposed to contain a list of operators.· Is this list available anywhere?

    Sid
  • cgraceycgracey Posts: 14,206
    edited 2006-06-23 00:14
    To make a pin an output, there are TWO registers which must be affected: DIRA and OUTA. DIRA holds the directions (or output enables) for each pin and OUTA holds the output states (apparent when corresponding DIRA bits are set) for each pin.



    To make P0 a high output:

    DIRA[noparse][[/noparse]0] := 1

    OUTA[noparse][[/noparse]0] := 1



    To make P1 a low output:

    DIRA[noparse][[/noparse]1] := 1

    OUTA[noparse][[/noparse]0] := 0



    To make P7..P0 count upwards:

    DIRA[noparse][[/noparse]7..0] := $FF

    REPEAT

    ··· OUTA[noparse][[/noparse]7..0]++



    To make P1 output the opposite of P0:

    DIRA[noparse][[/noparse]0] := 0

    DIRA[noparse][[/noparse]1] := 1

    REPEAT

    ··· OUTA[noparse][[/noparse]1] := !INA[noparse][[/noparse]0]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Chip Gracey
    Parallax, Inc.
  • NewzedNewzed Posts: 2,503
    edited 2006-06-23 12:24
    Chip, I understand pretty well what you are saying.

    Is DirA(7..0) = $FF the same as
    DirA(7..0) = %11111111 which means Pins 7 thru 0 are set as outputs?

    I'm not sure what OutA(7..0)++ means.· I can not find an expanation anywhere for the various operators.· I think it will be in Chapter 5, but Chapter 5 is not out yet.

    The little program I posted works just like it should, but what I want to do is toggle Pin 16 each time the PB is pressed.· In PBasic I would write:

    if IN1 = 0 then TOGGLE 16

    How would I write this in Spin?

    Thanks

    Sid
  • CJCJ Posts: 470
    edited 2006-06-23 13:12
    it is all in chapter four

    a huge chunk of it is operators

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Who says you have to have knowledge to use it?

    I've killed a fly with my bare mind.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-06-23 13:14
    OutA(7..0)++ counts upwards the eight bits, so you will see the values on the pins go through a binary succession.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Life is one giant teacup ride.
  • NewzedNewzed Posts: 2,503
    edited 2006-06-23 14:15
    Thanks, Paul

    If I write:

    DirA(7..0) = $FF
    OutA(7..0)++

    How could·I display OutA on my TV?

    Thanks

    Sid
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2006-06-23 14:51
    Sid -

    I am not familiar with PBASIC commands, so I can't say that my following thoughts directly implement the toggle command as PBASIC does it. But here are my thoughts...

    To do a toggle you need to detect the state change of the button and remember the last state the button was in. You can detect the rising edge (changing from a 0 to a 1) or a falling edge (changing from a 1 to a 0). I get to do a lot of PLC programming in my industry and we have a command called a one-shot that helps to detect the change of a button or switch. The one-shot means that when a state changes (high or low, depending on the one-shot command), a bit is on (or off) for one scan of the program, this is handy for doing latches and toggle buttons.

    Here is psuedocode to help your thinking (I have not checked Spin syntax or tested yet). I need to check the operators, because it can probably be simplified with the cool tricks available in Spin.

    if (ina == 1) & (laststate ==0)     'Note indentation for outa statement and no indentation for laststate :=ina
       outa[noparse][[/noparse]16] := !outa[noparse][[/noparse]16]
      'Place code here for anything else to do in the toggle on/off
      'code executes every time the input changes from a 0 to a 1
      'adjust the if test above for 1 to a 0
    
    laststate := ina
    
    



    You might want to also implement a debounce routine for the button; otherwise you might get an undetermined amount of toggles due to the noise in the button contacts and system when pressing the button.

    edit: Looks like my indentation does not show up when typing in text

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter
    tdswieter.com
    One little spark is all it takes for an idea to explode
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-06-23 15:07
    Newzed said...

    Thanks, Paul

    If I write:

    DirA(7..0) = $FF
    OutA(7..0)++

    How could·I display OutA on my TV?

    Thanks

    Sid

    I would display it indirectly by reading InA(7..0) then·using the hex function in TV_Terminal, though Im quite sure there are other ways of going about it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Life is one giant teacup ride.
  • NewzedNewzed Posts: 2,503
    edited 2006-06-23 16:54
    Paul, I wrote:

    Repeat 127
    ·· DirA[noparse][[/noparse]7..0]:= 0
    ·· OutA[noparse][[/noparse]7..0]++
    ·· term.hex(OutA[noparse][[/noparse]7..0],2)
    ·· term.str(string(" "))·

    The TV displayed 01 thru 7F so I think that part works.· So what am I reading - is 01
    the count on Pin 7, 02 the count on Pin 6, 03 the count on Pin 5, and so on, then 08
    is the new count on Pin 7, and so on?

    Sid
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-06-23 17:28
    Im not quite sure I follow, you're displaying the combined hexidecimal value of the entire group of 8 pins. Since this 8 bit value is incremented each iteration for 127 steps, the number will be sequential according to the value present on the pins. Pin 0 will be changing its state every iteration, Pin 1 will change its state every other iteration, Pin 2 will change its state every 4th iteration and so on up to pin 7 whose state is only changed once in the entire sequence.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Life is one giant teacup ride.
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2006-06-23 17:33
    Pins 7 6 5 4 3 2 1 0
    Data 0 0 0 0 0 0 0 0 (Hex: 00)
    Data 0 0 0 0 0 0 0 1 (Hex: 01)
    Data 0 0 0 0 0 0 1 0 (Hex: 02)
    Data 0 0 0 0 0 0 1 1 (Hex: 03)
    :
    :
    Data 0 0 0 0 1 1 1 1 (Hex: 0F)
    :
    :
    :
    Data 1 1 1 1 1 1 1 1 (Hex: FF)

    I am not following you either Sid, does this table help explain what is happening? Each line of data represents an increment (++). Think of the pin 7 to pin 0 as holding an 8-bit value and the code is incrementing that value.

    edited: Adjusted table to display better.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter
    tdswieter.com
    One little spark is all it takes for an idea to explode
  • SSteveSSteve Posts: 808
    edited 2006-06-23 17:36
    Sid:

    What you are reading back is the state of the eight pins in hex. "01" in hex is "00000001" binary, which means that only pin 0 is set. "02" hex = "00000010" binary, which means that only pin 1 is set. "03" hex = "00000011" binary, which means that pins 0 and 1 are set, etc.

    Try this:
    dira[noparse][[/noparse]7..0] := 0
    repeat 127
      outa[noparse][[/noparse]7..0]++
      term.bin(outa[noparse][[/noparse]7..0], 8)
      term.out(13)
      waitcnt(clkfreq / 2 + cnt)
    


    This will show you a binary representation of pins 7 through 0 so you can easily see what each pin is doing.

    Also, note that you don't need to include dira[noparse][[/noparse]7..0] := 0 in the loop. Once the direction of the bits is set, it stays that way until you change it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • SSteveSSteve Posts: 808
    edited 2006-06-23 17:40
    Or try this:
    dira[noparse][[/noparse]7..0] := 0
    repeat 127
      outa[noparse][[/noparse]7..0]++
      term.bin(outa[noparse][[/noparse]7..0], 8)
      term.out(" ")
      term.hex(outa[noparse][[/noparse]7..0], 2)
      term.out(13)
      waitcnt(clkfreq / 2 + cnt)
    


    This will show you the binary representation alongside the hex representation.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    OS-X: because making Unix user-friendly was easier than debugging Windows

    links:
    My band's website
    Our album on the iTunes Music Store
  • NewzedNewzed Posts: 2,503
    edited 2006-06-23 18:20
    Thanks to all - now I understand.· Remarkably simple once it is explained.

    Sid
Sign In or Register to comment.