Pyboard基礎功能探索---LED


MicroPython在官方網站上提供了一個在線測試的環境,可以讓我們通過瀏覽器去運行和體驗MicroPython。這個在線演示環境可以運行各種例程,查看各種外設和功能模塊,如LED、GPIO、ADC、按鍵、舵機驅動、延時、數學計算等,可以看到LED的變化,但是不支持I2C、SPI、UART、定時器等硬件功能,因為這個在線演示是通過QEMU進行軟件仿真的,並不是真實開發板運行(早期的在線演示是在真正開發板上運行,但是訪問很慢,因為只有一個開發板而可能會有很多用戶訪問,同時還會受到網速的限制)。

在線仿真運行網址https://microPython.org/unicorn

早期版本http://microPython.org/live/

 

板載LED(3)和LED(4)分別是橙色的燈和藍色的燈,它們兩個都可以進行亮度調節,其它兩個(LED(1)、LED(2))沒有調節亮度功能,區別就在於LED(3)和LED(4)使用了定時器實現PWM調節亮度。LED(3)使用的是定時器2,LED(4)使用的是定時器3,所以在使用這兩個燈的亮度調節功能時不可以再使用這兩個定時器了,不然程序就會和預想的格格不入。

 LED_YELLOW -- Pin(Pin.cpu.A15, mode=Pin.ALT, af=Pin.AF1_TIM2)
 LED_BLUE -- Pin(Pin.cpu.B4, mode=Pin.ALT, af=Pin.AF2_TIM3)

LED

利用REPL獲取相關信息:

>>> 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有四個redgreenorangeblue)引腳:

>>> 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)
...

獲取Pin.cpu.A13引腳全部名稱:

>>> 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)

 結論: help(pyb.Pin.board)輸出板子上引腳的別名和芯片上定義好的引腳名,如:LED_YELLOW -- Pin(Pin.cpu.A15, mode=Pin.OUT),這個引腳的別名:LED_YELLOW,芯片上定義的引腳名:A15。

獲取pyboard上引腳的別名芯片定義的引腳名的方法: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)
  ....

 下面這些是pyboard開發板上已經占用的引腳名單:

  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, af=10) USB_DP -- Pin(Pin.cpu.A12, mode=Pin.ALT, af=10)

 獲取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']
>>>

在LED類里面有四個方法,on()、off()、toggle()、intensity([value]):

構造一個LED類的實例:pyb.LED(id)id = 1-4  (1-red、2-green、3-orange、4-blue)

紅燈:

led_red = pyb.LED(1)

LED類里面的方法使用示例: 

>>> from pyb import LED
>>> led_red = LED(1)  #構造一個對象(紅燈)
>>> led_red.on()   #點亮紅燈
>>> led_red.off()  #熄滅紅燈
>>> led_red.toggle()  #點亮/熄滅紅燈(引腳電平狀態翻轉)

 除了打開、關閉、翻轉功能外,部分 LED 還可以控制亮度。在 PYB V10上,LED3和LED4支持亮度調整功能,如可以這樣控制LED3的亮度:

pyb.LED(3).intensity(128)

亮度的范圍為0~255,0最暗(關閉),255最亮。對於那些不支持亮度功能的LED,在設置亮度時,0是關,大於0就是開。

流水燈:

>>> 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)

往返式流水燈

>>> from pyb import LED
>>> n = 1  #定義變量
>>> state = 1
>>> while True:
...     LED(n).toggle()  #翻轉LED
...     pyb.delay(500)   #延時
...     LED(n).toggle()  #再次翻轉
...     n = n + state   #改變LED序號
...     if (n > 3) or (n<2):
...         toggle = -state  #改變方向
...

異常時執行finally里面的程序段

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()

呼吸燈(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)

 下一章節:Pyboard基礎功能探索---按鍵、GPIO


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM