Pyboard explore basic functions --- LED

MicroPython provides an online test environment on the official website, you can let go and experience MicroPython run through the browser. This online demo environment can run a variety of routines, and view a variety of peripheral modules, such as LED, GPIO, ADC, buttons, servo drive, delay, math, etc., we can see the LED changes, but does not support I2C, SPI, UART, timer and other hardware features, because this online demo is a software emulation by QEMU, not real development board running (early online demo is running in real development board, but access is very slow, because only a development board and there may be many users access the same time also be limited by the speed).

Online simulation runs URL : https://microPython.org/unicorn

Earlier versions : http://microPython.org/live/

LED

REPL use for information:

>>> import pyb
>>> help(pyb.LED)
object <class 'LED'> is of type type
  on -- <function>
  off -- <function>
  toggle -- <function>
  intensity -- <function>
>>> dir(pyb.LED)
['__class__', '__name__', 'intensity', 'off', 'on', 'toggle']
>>>

LED board has four ( Red , Green , Orange , Blue ) , Pin:

>>> help(pyb.Pin.board)
object <class 'board'> is of type type
...

  LED_RED -- Pin(Pin.cpu.A13, mode=Pin.OUT)
  LED_GREEN -- Pin(Pin.cpu.A14, mode=Pin.OUT)
  LED_YELLOW -- Pin(Pin.cpu.A15, mode=Pin.OUT)
  LED_BLUE -- Pin(Pin.cpu.B4, mode=Pin.OUT)
...

All pins get Pin.cpu.A13 name:

>>> orange = pyb.Pin(Pin.cpu.A13,Pin.OUT)
>>> orange.names()
['A13', 'LED_RED']
>>> A13 = pyb.Pin('A13',Pin.OUT)
>>> A13
Pin(Pin.cpu.A13, mode=Pin.OUT)

 Conclusion : definition help (pyb.Pin.board) aliases and output pins on the chip board good pin names, such as: LED_YELLOW - Pin (Pin.cpu.A15, MODE = Pin.OUT), this pin alias: LED_YELLOW, pin names defined on the chip: A15.

Get pyboard pins on the alias and the pins of chip-defined methods: help (pyb.Pin.board)

>>> help(pyb.Pin.board)
object <class 'board'> is of type type
  X1 -- Pin(Pin.cpu.A0, mode=Pin.ALT, af=Pin.AF2_TIM5)
  X2 -- Pin(Pin.cpu.A1, mode=Pin.IN)
  X3 -- Pin(Pin.cpu.A2, mode=Pin.IN)
  X4 -- Pin(Pin.cpu.A3, mode=Pin.IN)
  X5 -- Pin(Pin.cpu.A4, mode=Pin.ANALOG)
  X6 -- Pin(Pin.cpu.A5, mode=Pin.ALT, pull=Pin.PULL_UP, af=Pin.AF5_SPI1)
  X7 -- Pin(Pin.cpu.A6, mode=Pin.ALT, pull=Pin.PULL_UP, af=Pin.AF5_SPI1)
  X8 -- Pin(Pin.cpu.A7, mode=Pin.ALT, pull=Pin.PULL_UP, af=Pin.AF5_SPI1)
  X9 -- Pin(Pin.cpu.B6, mode=Pin.ALT_OPEN_DRAIN, pull=Pin.PULL_UP, af=Pin.AF4_I2C1)
  X10 -- Pin(Pin.cpu.B7, mode=Pin.ALT_OPEN_DRAIN, pull=Pin.PULL_UP, af=Pin.AF4_I2C1)
  X11 -- Pin(Pin.cpu.C4, mode=Pin.IN)
  X12 -- Pin(Pin.cpu.C5, mode=Pin.IN)
  X17 -- Pin(Pin.cpu.B3, mode=Pin.IN, pull=Pin.PULL_UP)
  X18 -- Pin(Pin.cpu.C13, mode=Pin.IN)
  X19 -- Pin(Pin.cpu.C0, mode=Pin.ANALOG)
  X20 -- Pin(Pin.cpu.C1, mode=Pin.IN)
  X21 -- Pin(Pin.cpu.C2, mode=Pin.IN)
  X22 -- Pin(Pin.cpu.C3, mode=Pin.IN)
  Y1 -- Pin(Pin.cpu.C6, mode=Pin.IN)
  Y2 -- Pin(Pin.cpu.C7, mode=Pin.IN)
  Y3 -- Pin(Pin.cpu.B8, mode=Pin.ALT, pull=Pin.PULL_UP, af=Pin.AF9_CAN1)
  Y4 -- Pin(Pin.cpu.B9, mode=Pin.ALT, pull=Pin.PULL_UP, af=Pin.AF9_CAN1)
  Y5 -- Pin(Pin.cpu.B12, mode=Pin.IN)
  Y6 -- Pin(Pin.cpu.B13, mode=Pin.IN)
  Y7 -- Pin(Pin.cpu.B14, mode=Pin.IN)
  Y8 -- Pin(Pin.cpu.B15, mode=Pin.IN)
  Y9 -- Pin(Pin.cpu.B10, mode=Pin.IN)
  Y10 -- Pin(Pin.cpu.B11, mode=Pin.IN)
  Y11 -- Pin(Pin.cpu.B0, mode=Pin.IN)
  Y12 -- Pin(Pin.cpu.B1, mode=Pin.IN)
  ....

 The following is a list of pyboard pin development board is already occupied:

  help(pyb.Pin.board)
 ...
SW -- Pin(Pin.cpu.B3, mode=Pin.IN, pull=Pin.PULL_UP) LED_RED -- Pin(Pin.cpu.A13, mode=Pin.OUT) LED_GREEN -- Pin(Pin.cpu.A14, mode=Pin.OUT) LED_YELLOW -- Pin(Pin.cpu.A15, mode=Pin.OUT) LED_BLUE -- Pin(Pin.cpu.B4, mode=Pin.OUT) MMA_INT -- Pin(Pin.cpu.B2, mode=Pin.IN) MMA_AVDD -- Pin(Pin.cpu.B5, mode=Pin.OUT) SD_D0 -- Pin(Pin.cpu.C8, mode=Pin.ALT, pull=Pin.PULL_UP, af=12) SD_D1 -- Pin(Pin.cpu.C9, mode=Pin.ALT, pull=Pin.PULL_UP, af=12) SD_D2 -- Pin(Pin.cpu.C10, mode=Pin.ALT, pull=Pin.PULL_UP, af=12) SD_D3 -- Pin(Pin.cpu.C11, mode=Pin.ALT, pull=Pin.PULL_UP, af=12) SD_CMD -- Pin(Pin.cpu.D2, mode=Pin.ALT, pull=Pin.PULL_UP, af=12) SD_CK -- Pin(Pin.cpu.C12, mode=Pin.ALT, pull=Pin.PULL_UP, af=12) SD -- Pin(Pin.cpu.A8, mode=Pin.IN, pull=Pin.PULL_UP) SD_SW -- Pin(Pin.cpu.A8, mode=Pin.IN, pull=Pin.PULL_UP) USB_VBUS -- Pin(Pin.cpu.A9, mode=Pin.IN) USB_ID -- Pin(Pin.cpu.A10, mode=Pin.ALT_OPEN_DRAIN, pull=Pin.PULL_UP, af=10) USB_DM - Pin (Pin.cpu.A11, mode = Pin.ALT, of = 10 ) USB_DP - Pin (Pin.cpu.A12, mode = Pin.ALT, of = 10)

 Get Class LED

>>> import pyb
>>> help(pyb.LED)
object <class 'LED'> is of type type
  on -- <function>
  off -- <function>
  toggle -- <function>
  intensity -- <function>
>>> dir(pyb.LED)
['__class__', '__name__', 'intensity', 'off', 'on', 'toggle']
>>>

There are four ways in which LED-based, on (), off (), toggle (), intensity ([value]):

A LED instance of the class structure: pyb.LED (ID) 1-4 = ID   (l- Red, 2- Green, 3- Orange, 4- Blue)

red light:

led_red = pyb.LED (1)

Inside the LED-based method Example: 

>>> from PYB Import the LED
 >>> the LED led_red = (. 1)   # configuration of an object (red) 
>>> led_red.on ()    # point red 
>>> led_red.off ()   # Off Red 
> led_red.toggle >> ()   # on / off red (inverted state of the pin)

 In addition to open, close, turning function, the luminance portion of the LED can also be controlled. On PYB V10, LED3 and LED4 support brightness adjustment function, such as can control the brightness of LED3:

pyb.LED (3) .intensity (128)

Range from 0 to 255, the luminance of the darkest (closed), 255 brightest. For those who do not support the LED luminance function, at the time of setting the brightness, where 0 is off, it is greater than 0 apart.

Light water:

>>> led_color = [pyb.LED(i) for i in range(1,5)]
>>> n = 0
>>> while True:
...     n = (n+1) % 4
...     leds[n].toggle()
...     pyb.delay(50)

Round-trip water lights

>>> from PYB Import the LED
 >>> # n-defined variable. 1 = 
>>> = State. 1 
>>> the while True: 
... the LED (n-) .toggle () # flipping the LED 
... pyb.delay ( 500 ) # delay 
... LED (n) .toggle () # turned over again 
... n- = n-+ State change the LED number # 
...      IF (n->. 3) or (n-<2 ): 
... Toggle = - State # change direction 
...

An exception when executing block finally inside

led_color = [pyb.LED(i) for i in range(1,5)]
try:
    while True:
        n = (n + 1) % 4
        leds[n].toggle()
        pyb.delay(50)
finally:
    for n in led_color:
        n.off()

Breathing light (LED (3), LED (4)

led_orange = pyb.LED(4)
intensity = 0
flag = True
while True:
    if flag:
        intensity = (intensity + 1) % 255
        if intensity == 0:
            flag = False
            intensity = 255
    else:
        intensity = (intensity - 1) % 255
        if intensity == 0:
            flag = True
    led_orange.intensity(intensity)
    pyb.delay(30)

 The next chapter : Pyboard explore basic functions --- button, GPIO

Guess you like

Origin www.cnblogs.com/iBoundary/p/11502317.html