Easy-Python  1.00.00Change to: 中文
Pin

Functions

 PIN (id, mode, pull, value, drive)
 Make new PIN object. More...
 
 init (mode, pull, value, drive)
 Initial the PIN. More...
 
 deinit ()
 Deinitialises the PIN. More...
 
 value (x)
 Get or set the PIN current value. More...
 
 mode (x)
 Get or set the pin mode. More...
 
 pull (x)
 Get or set the pin pull. More...
 
 drive (x)
 Get or set the pin drive. More...
 
 irq (trigger, handler)
 Set the PIN irq function. More...
 

Detailed Description

A pin object is used to control I/O pins (also known as GPIO - general-purpose input/output). Pin objects are commonly associated with a physical pin that can drive an output voltage and read input voltages. The pin class has methods to set the mode of the pin (IN, OUT, etc) and methods to get and set the digital logic level.

KEY Example of Micropython
Click here
Example Code of usage:
from machine import PIN
p=PIN('P12') #P0~P32, exclude : P17, P18
p.init(mode=PIN.OUT,vlaue=0)
p.value()
p.value(1)
p.value()
p.deinit()

Function Documentation

PIN ( id  ,
mode  ,
pull  ,
value  ,
drive   
)

Make new PIN object.

Access the pin peripheral (GPIO pin) associated with the given id. If additional arguments are given in the constructor then they are used to initialise the pin. Any settings that are not specified will remain in their previous state.

Parameters
[in]idis the PIN name ‘P0’~’P32’, exclude : P17, P18
[in]modespecifies the pin mode, which can be one of:
  • PIN.OUT
  • PIN.IN
[in]pullspecifies if the pin has a (weak) pull resistor attached, and can be one of:
  • PIN.PULL_UP
  • PIN.PULL_DOWN
  • PIN.INACTIVE
[in]valueget the PIN curren value, if not input vlaue. Input 0/1 to set PIN value, it is valid only for PIN.OUT mode.
[in]drivespecifies the output power of the pin and can be one of(default: PIN.NORMAL_POWER):
  • PIN.LOW_POWER
  • PIN.NORMAL_POWER
  • PIN.MED_POWER
  • PIN.HIGH_POWER
Returns
(no)
Example of usage:
from machine import PIN
p=PIN('P0',mode=PIN.OUT,pull=PIN.INACTIVE,value=0,drive=PIN.HIGH_POWER)
init ( mode  ,
pull  ,
value  ,
drive   
)

Initial the PIN.

Re-initialise the pin using the given parameters. Only those arguments that are specified will be set.

Parameters
[in]modespecifies the pin mode, which can be one of:
  • PIN.OUT
  • PIN.IN
[in]pullspecifies if the pin has a (weak) pull resistor attached, and can be one of:
  • PIN.PULL_UP
  • PIN.PULL_DOWN
  • PIN.INACTIVE
[in]valueget the PIN curren value, if not input vlaue. Input 0/1 to set PIN value, it is valid only for PIN.OUT mode.
[in]drivespecifies the output power of the pin and can be one of(default: PIN.NORMAL_POWER):
  • PIN.LOW_POWER
  • PIN.NORMAL_POWER
  • PIN.MED_POWER
  • PIN.HIGH_POWER
Returns
(no)
Example of usage:
from machine import PIN
p=PIN('P0')
p.init(mode=PIN.OUT,pull=PIN.INACTIVE,value=0,drive=PIN.HIGH_POWER)
deinit ( )

Deinitialises the PIN.

Parameters
no
Returns
(no)
Example of usage:
from machine import PIN
p=PIN('P0')
p.deinit()
value ( )

Get or set the PIN current value.

This method allows to set and get the value of the pin, depending on whether the argument x is supplied or not. If the argument is omitted then this method gets the digital logic level of the pin, returning 0 or 1 corresponding to low and high voltage signals respectively. The behavior of this method depends on the mode and pull of the pin:

  • PIN.IN
    • The method returns the actual input value currently present on the pin, when the pull is PIN.INACTIVE.
    • The value will return 1, when the pull is PIN.PULL_UP.
    • The value will return 0, when the pull is PIN.PULL_DOWN.
  • PIN.OUT - The method returns value, according the setting of value, default 0.
    If the argument is supplied then this method sets the digital logic level of the pin. The argument x can be anything that converts to a boolean. If it converts to True, the pin is set to state ‘1’, otherwise it is set to state ‘0’. The behavior of this method depends on the mode of the pin:
  • PIN.IN
    • The method returns the actual input value currently present on the pin, when the pull is PIN.INACTIVE.
    • The value will return 1, when the pull is PIN.PULL_UP.
    • The value will return 0, when the pull is PIN.PULL_DOWN.
  • PIN.OUT - The output buffer is set to the given value immediately.
    Parameters
    [in]xinput 0/1 to set PIN,value, if omitted it then will get the current PIN value.
    Returns
    (no)
    Example of usage:
    from machine import PIN
    p=PIN('P0',mode=PIN.OUT,pull=PIN.INACTIVE,value=0,drive=PIN.HIGH_POWER)
    p.value()
    p.value(1)
    p.value()
mode ( )

Get or set the pin mode.

Parameters
[in]xinput PIN.IN/PIN.OUT to set PIN mode, if omitted it then will get the current PIN mode.
Returns
(no)
Example of usage:
from machine import PIN
p=PIN('P0')
p.mode()
p.mode(PIN.OUT)
p.mode()
pull ( )

Get or set the pin pull.

Parameters
[in]xinput PIN.PULL_UP/PIN.PULL_DOWN/PIN.INACTIVE to set PIN pull, if omitted it then will get the current PIN pull.
Returns
(no)
Example of usage:
from machine import PIN
p=PIN('P0')
p.pull()
p.pull(PIN.PULL_UP)
p.pull()
drive ( )

Get or set the pin drive.

Parameters
[in]xinput PIN.LOW_POWER/PIN.NORMAL_POWER/PIN.MED_POWER/PIN.HIGH_POWER to set PIN drive, if omitted it then will get the current PIN drive.
Returns
(no)
Example of usage:
from machine import PIN
p=PIN('P0')
p.drive()
p.drive(PIN.LOW_POWER)
p.drive()
irq ( trigger  ,
handler   
)

Set the PIN irq function.

Configure an interrupt handler to be called when the trigger source of the pin is active.
If the pin mode is PIN.IN then the trigger source is the external value on the pin.
If the pin mode is PIN.OUT then the trigger source is the output buffer of the pin.

Parameters
[in]configuresthe event which can generate an interrupt. Possible values are:
  • PIN.IRQ_BOTH : Set irq falling and risging edge trigger mode.
  • PIN.IRQ_FALLING : Set irq falling edge trigger mode.
  • PIN.IRQ_RISING : Set irq risging edge trigger mode.
  • PIN.IRQ_LOW_LEVEL : Set irq low level trigger mode.
  • PIN.IRQ_HIGH_LEVEL : Set irq high level trigger mode.
[in]handleris an optional function to be called when the interrupt is triggered.
Returns
(no)
Example of usage:
from machine import PIN
p=PIN('P12')
p.mode(PIN.OUT)
p.value(0)
irq_count = 0
def count(t):
global irq_count
if irq_count < 5:
irq_count = irq_count+1
else :
irq_count = 0
p.irq(trigger=PIN.IRQ_BOTH,handler=count)
p.value(1)
p.value(0)
print(irq_count)