Shop OBEX P1 Docs P2 Docs Learn Events
read 3 buttons in parallel — Parallax Forums

read 3 buttons in parallel

sicoloco55sicoloco55 Posts: 6
edited 2011-04-12 03:09 in Propeller 1
I have 3 buttons and i want to read the pulse in 3 different pins... but in parallel
i think how to but my code dosen't work

here is what i do
CON
  _clkmode = rcfast
  high = 1
  low = 0
  out = %111
  in = %000
  

VAR
  BYTE cogIzq
  BYTE cogDer
  BYTE cogCen
  LONG stack0[20]
  LONG stack1[20]
  LONG stack2[20]
  
PUB Start  

dira[0] := 1
  dira[1] := 1
  dira[2] := 1
  dira[3] := 0
  dira[4] := 0
  dira[5] := 0

  cognew(girarizq(0,3),@stack0)
  cognew(girarder(1,4),@stack1)
  cognew(centrar(2,5),@stack2)
  
  
PRI girarizq(Pin, Puls1)
repeat while true
  if ina[Puls1] == high
    outa[Pin] := high
  
                           
PRI girarder(Pin, Puls2)
repeat while true
  if ina[Puls2] == high
    outa[Pin] := high
  

PRI centrar(Pin, Puls3)
repeat while true
  if ina[Puls3] == high
    outa[Pin] := high
  
                

Comments

  • JomsJoms Posts: 279
    edited 2011-04-09 19:44
    I think the problem may be in you have to tell the pin to go low if it isn't a high. Someone may correct me, but I don't think the output pin defaults high or low unless you tell it to...
    CON
      _clkmode = rcfast
      high = 1
      low = 0
      out = %111
      in = %000
      
    
    VAR
      BYTE cogIzq
      BYTE cogDer
      BYTE cogCen
      LONG stack0[20]
      LONG stack1[20]
      LONG stack2[20]
      
    PUB Start  
    
    dira[0] := 1
      dira[1] := 1
      dira[2] := 1
      dira[3] := 0
      dira[4] := 0
      dira[5] := 0
    
      cognew(girarizq(0,3),@stack0)
      cognew(girarder(1,4),@stack1)
      cognew(centrar(2,5),@stack2)
      
      
    PRI girarizq(Pin, Puls1)
      repeat
        if ina[Puls1] == 1
          outa[Pin] := 1
        else
          outa[Pin] := 0
      
                               
    PRI girarder(Pin, Puls2)
      repeat  
        if ina[Puls2] == 1
          outa[Pin] := 1
        else
          outa[Pin] := 0
      
    
    PRI centrar(Pin, Puls3)
      repeat  
        if ina[Puls3] == 1
          outa[Pin] := 1
        else
          outa[Pin] := 0
    
    

    Maybe this will help... But I am not sure I completely understand what you are wanting to do...
  • sicoloco55sicoloco55 Posts: 6
    edited 2011-04-09 20:11
    the situation is this.. i need to move a crane 90º to left from a center position, 90º right from the same position and then center it again
    so... i have 3 pulses pin 3, pin 4 and pin 5 i need to check the 3 of them at the same time

    i simulate the movement of the crane with 3 leds if pin 3 is high the left leds goes on and so one...
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-09 20:32
    All the I/O pins appear in a single 32 bit register (INA). If you're trying to access them one at a time, you can use a subscript to refer to one of the bits like INA[ 5 ]. If the I/O pins you want are adjacent, you can read a group of them at once by giving a range. If the 3 pins are #3, #4, and #5, you can write INA[ 3..5 ] or INA[ 5..3 ]. This will get you a 3 bit number containing the values of all 3 I/O pins. The order of the bits in the number depends on which order you've specified. Read the Propeller Manual's section on INA, OUTA, and DIRA for details. As Joms mentioned, you have to set the corresponding bits in DIRA to zeros to specify input mode for the I/O pins you're using.
  • dogmagnetdogmagnet Posts: 3
    edited 2011-04-09 21:09
    CON
      _clkmode = rcfast
      
    
    VAR
     
      LONG stack0[20]
      
    PUB Start  
    
      dira := %00000000_00000000_00000000_00000111
    
      cognew(girarizq,@stack0)
      
      
    PRI girarizq
      repeat
        outa[0..2] := ina[3..5] >> 3
    

    Do you need each button to go into a different cog to run completely independently?

    If not you can have one cog read all the buttons then start other cogs to carry out actions.

    The code here should read the port pins then shift the bits to align with the out put pins and set them.

    I am however new to propeller! I just got a demo board but don't have it with me as I am in Malaysia this week, but live in England so I have not tried this code!!
  • kuronekokuroneko Posts: 3,623
    edited 2011-04-10 04:18
    Pins are set as inputs by default (dira == 0). If you need an output pin (dira[n] == 1) then it has to be set as an output in the cog which is using the output (each cog has its own dira/outa register pair). All listed code fragments so far set the output direction in the primary cog but try to use the pin as an output in a different cog. NG
    VAR
      long  stack0[20]
      long  stack1[20]
      long  stack2[20]
      
    PUB Start  
    
      cognew(response(0,3), @stack0{0})
      cognew(response(1,4), @stack1{0})
      cognew(response(2,5), @stack2{0})
      
    PRI response(Pin, Puls1)
    
      [COLOR="red"]dira[Pin]~~[/COLOR]
      repeat
        outa[Pin] := ina[Puls1]
    
    DAT
    
  • dogmagnetdogmagnet Posts: 3
    edited 2011-04-10 05:35
    Thank you!

    So if I wanted to read the port and write it in just one cog would my code be correct if I move the dira statement?

    Does spin manage the bit operations in outa[0..2] := ina[3..5] >> 3 or would I need individually test and set the bits?

    Many thanks for your assistance!

    (I have many years of programming experience in PIC16, 18 and 24 in assembler and C, so I have to learn to think differently for the Propeller.).

    Andy

    VAR
     
      LONG stack0[20]
      
    PUB Start  
    
      cognew(response,@stack0)
        
    PRI response
        dira := %00000000_00000000_00000000_00000111
        repeat
           outa[0..2] := ina[3..5] >> 3
    
    DAT
    ....
    
    
  • kuronekokuroneko Posts: 3,623
    edited 2011-04-10 05:42
    VAR
      LONG stack0[20]
      
    PUB Start  
    
      cognew(response,@stack0)
        
    PRI response
        dira[0..2] := %111
        repeat
           outa[0..2] := ina[3..5]
    
    DAT
    ....
    
    
    With the dira setup in the right cog it will work provided you remove the >> operation. outa[0..2] as well as ina[3..5] are considered 3bit values. So there is no point shifting the latter right be 3. You could also reverse the pin order, i.e. outa[0..2] := ina[5..3]. This preserves the assignment for pin 1 (4) but swaps the assignments for pins 0 (5) and 2 (3). HTH
  • dogmagnetdogmagnet Posts: 3
    edited 2011-04-12 03:09
    Thank you Kuroneko.
    :smile:
Sign In or Register to comment.