板载 PWM:呼吸灯

本样例使用了一个连接到板载 PWM 接口的 LED 灯模块。Python 程序将会控制该 LED 灯“呼吸”(逐渐变亮后变暗)。

硬件连线

模块角标

龙芯派接口

Python 引脚常量名

VCC/IN (LED 正极)

P45: PWM0

PWM0

GND (LED 负极)

P9: GND

代码

import time
import board
import pulseio

led = pulseio.PWMOut(board.PWM0, frequency=5000, duty_cycle=0)

while True:
    for i in range(100):
        # PWM LED up and down
        if i < 50:
            led.duty_cycle = int(i * 2 * 65535 / 100)  # Up
            print("up    ", end="\r")
        else:
            led.duty_cycle = 65535 - int((i - 50) * 2 * 65535 / 100)  # Down
            print("down  ", end="\r")
        time.sleep(0.01)

程序运行后,LED 灯将会循环逐渐变亮后变暗。

Last updated