Introduction #
A common requirement for programmers is to determine the external state of an IO pin.
The Propeller 2 smart IO pins make this simple without needing any external components, and allow the programmer to determine if the pin is connected to an external signal that has a high, low, floating or has a rapidly changing state.
The attached SPIN2 code method "check_pinstate" returns a value from 0 to 3 to indicate the pin state.
The method enables a weak 10uA internal pull resistor and then reads the input. Any stronger external signal is able to pull the IO pin in either direction, and so by testing both the low and high states, the external signal can be determined.
A complete demo can be found at the OBEX link below.
Code Snippet #
PRI check_pinstate(pin) : result
''' Check_pinstate will reply with a value indiciating the state of the io pin
'''
''' 0 = pin is floating
''' 1 = pin is low
''' 2 = pin is high
''' 3 = pin is changing, high/low
result := 0 ' clear result
''' Test for low external state
PINCLEAR(pin)
WRPIN (pin, P_HIGH_10uA) ' enable weak internal pullup
PINHIGH (pin)
WAITUS (10) ' allow pin to settle
IFNOT PINREAD (pin) ' an external pulldown will pull pin low
result |= %01
''' Test for high external state
PINCLEAR(pin)
WRPIN (pin, P_LOW_10uA) ' enable weak internal pulldown
PINLOW (pin)
WAITUS (10) ' allow pin to settle
IF PINREAD (pin) ' an external pullup will pull pin high
result |= %10
PINCLEAR(pin) ' reset pin before returning result