zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    javascript Object的长度
    java新手笔记9 类的封装示例
    java新手笔记8 包
    java新手笔记7 找最小、最大、排序
    jdk8 新特性
    Spring Boot: 加密应用配置文件敏感信息
    并发之Fork/Join框架使用及注意点
    Java并发编程:CountDownLatch、CyclicBarrier和 Semaphore
    spring 事件监听器
    观察者模式(订阅模式)
  • 原文地址:https://www.cnblogs.com/iBoundary/p/11502317.html
Copyright © 2011-2022 走看看