not logged in | [Login]
Here we show how to control Pins using the pyb.Pin interface. The programmers guide is here basic Pin, ADC, Interrupts
Setting a pin to a digital value
import pyb
g = pyb.Pin(pin_id, mode, pull) # mode and pull are optional arguments and default to input and no-pullup
# e.g. if:
# X1 is how the pin is labeled on the board,
# using X1 for output to control an LED,
# we want a pullup resistor on the output.
g = pyb.Pin('X1', pyb.Pin.OUT_PP, Pin.PULL_UP)
g.low() # sets the pin low
g.value(True) # sets it high,
g.value(False) # sets it low
Reading a digital value from a Pin
g = pyb.Pin('X1', pyb.Pin.IN)
state = g.value() # no argument to value means read.
There are several ways to identify the pin to control.
mypin = pyb.Pin.board.X1
mypin = pyb.Pin.cpu.C12
pyb.Pin.dict["LeftMotorDir"] = pyb.Pin.cpu.C12
mypin = pyb.Pin("LeftMotorDir")
and use it like so:
g = pyb.Pin(mypin, pyb.Pin.OUT_PP)
or g = pyb.Pin(mypin, pyb.Pin.OUT_PP, Pin.PULL_UP)
A Pin can act in one of the following modes.
Pin.IN
- configure the pin for inputPin.OUT_PP
- configure the pin for output, with push-pull controlPin.OUT_OD
- configure the pin for output, with open-drain controlPin.AF_PP
- configure the pin for alternate function, pull-pullPin.AF_OD
- configure the pin for alternate function, open-drainPin.ANALOG
- configure the pin for analog. You may prefer to use pyb.ADC instead.You would use Push-pull control to do ... and Open-drain to ...
The Altenate function settings are used when the specific chipset has allocated apin to do several functions internally. E.g. ... This alternate function is turned on by setting an internal register. This setting defines how the Pin mode will be handled if this is alternate function is active.
A Pin can have the following pullup settings:
Pin.PULL_NONE
- no pull up or down resistors - This is the defaultPin.PULL_UP
- enable the pull-up resistorPin.PULL_DOWN
- enable the pull-down resistorWe can use the Pin directly or use the ADC. The ADC gives you more control.
Reading an Analog value from a Pin directly:
g = pyb.Pin('X1', pyb.Pin.ANALOG)
state = g.value() # no argument to value means read.
For more details on using the ADC see here.
Pin's also have the ability to trigger an interrupt and do an action when the Digital value of the Pin changes. To do this we use Pin.ExtInt
import pyb
key_interrupt = pyb.ExtInt(pin_id, pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, callback)
#E.g. if:
# - You want to call the function count() when
# - the Pin X1 is pulled Low then
key_interrupt = pyb.ExtInt('X1', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP, count)
Note:
Valid modes are
pyb.ExtInt.IRQ_RISING
pyb.ExtInt.IRQ_FALLING
pyb.ExtInt.IRQ_RISING_FALLING
pyb.ExtInt.EVT_RISING
pyb.ExtInt.EVT_FALLING
pyb.ExtInt.EVT_RISING_FALLING
IRQ modes are used for ... EVT modes allow ...
Other useful functions for external interrupts are:
extint.disable()
Disable the interrupt associated with the ExtInt object. This could be useful for debouncing.extint.enable()
Enable a disabled interrupt.extint.line()
Return the line number that the pin is mapped to.extint.swint()
Trigger the callback from software.Last edited by Nicholas Herriot, 2019-05-22 11:34:51