Shop OBEX P1 Docs P2 Docs Learn Events
Determining status of input — Parallax Forums

Determining status of input

ahage16ahage16 Posts: 1
edited 2008-07-03 04:04 in Propeller 1
I am writing a program using the Propeller proto board. The purpose is to allow the user to set a variable equal to a certain number using a switch. After this, a motor is started and revolutions are counted and the motor stays on until the number of revolutions is equal to the number times the switch is pressed. Here is the full code which I could not get to work.

VAR
  word state
  word desrev
  word miss
  word turn
CON
  LED = 0
  Motor = 1
  SwitchOutput = 5 
PUB Toggle
  dira := %00000000_00000000_00000000_00010111 'P0=LED, P1=Motor, P2=Hall Effect Input, P3=Hall Effect Output, P4=Switch Input, P5=Switch Output
  outa := %00000000_00000000_00000000_00010100
  repeat                                       
    waitcnt (15_000_000 + cnt)
    state := outa[noparse][[/noparse]SwitchOutput]            'set state = to 1 if switch is pressed
    If state == 1               'if pin5 = 1
      desrev++                                          'add one to desrev
      outa[noparse][[/noparse]LED]~~                                         'turn LED on to show it is counted 
      waitcnt (15_000_000+ cnt)
    Elseif state == 0           'if pin5 = 0
      miss++                                            'add one to miss
      If miss == 4                                      'if miss = 4
        quit                                                                    'quit loop
  repeat while turn < desrev    
    outa[noparse][[/noparse]Motor]~~                   'turn motor on 
    waitpeq ( %00000000_00000000_00000000_00000100, %00000000_00000000_00000000_00000100, 0)
    turn++




I tried breaking it down to figure out what is wrong and this is the simplest code that doesn't work, and here it is:

VAR
  word state
PUB Toggle  
  dira := %00000000_00000000_00000000_00000101 'P0=Switch Input, P1=Switch Output, P2=LED
  outa := %00000000_00000000_00000000_00000001
  repeat
    [b]state := outa            'set state = to pin position[/b]
    If state == 1               'if pin1 = 1
      outa~~
      waitcnt (7_000_000 + cnt)



essentially, my question is, can the bolded part be used to determine the status of an input? or is that only for outputs?

Comments

  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-03 03:51
    It looks like you want to read the actual state of the pins? If so use ina this gives you the actual pin state even for output pins
  • Chuck RiceChuck Rice Posts: 210
    edited 2008-07-03 03:53
    I use these (can't remember where I found them, but they make reading and setting pins easy.


    PRI readPin(pin)
    '   &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;
    '   &#9474; Reads the value of a pin  
    '   &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;
    
      dira[noparse][[/noparse] pin ]~                                           ' make an input
      return ina[noparse][[/noparse] pin ]                                       
      
    PRI high(pin)
    '   &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;
    '   &#9474; Sets a pin to a high state  
    '   &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;
    
      dira[noparse][[/noparse] pin ]~~                                           ' make an output
      outa[noparse][[/noparse] pin ]~~                                           ' write "1" to pin
    
    
    PRI low(pin)
    '   &#9484;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;
    '   &#9474; Sets a pin to a low state  
    '   &#9492;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;&#9472;
    
      dira[noparse][[/noparse] pin ]~~                                           ' make an output
      outa[noparse][[/noparse] pin ]~                                            ' write "0" to pin
    
    
    
    



    You might also look at page 98 in the Propeller Manual. It discusses using ! to toggle a pin.

    Post Edited (Chuck Rice) : 7/3/2008 4:10:40 AM GMT
  • TimmooreTimmoore Posts: 1,031
    edited 2008-07-03 04:04
    Page 27 of the prop manual says you can read an output pin without changing it to an input pin and I have found this works.
Sign In or Register to comment.