PWM 的使用
关于 PWM 详细介绍请参考PWM-API 文档.
1. 使用方法
- 从 machine 导入 PWM, Timer 模块
from machine import Timer,PWM
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
ch = PWM(tim, freq=500000, duty=50, pin=boad_info.LED_G)
- 改变占空比, 设置的 pin 脚将输出不同占空比的波形
ch.duty(duty)
2. 示例
控制 LED_G 灯亮度
from machine import Timer,PWM
import time
from board import board_info
from fpioa_manager import fm
tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
ch = PWM(tim, freq=500000, duty=50, pin=boad_info.LED_G)
duty=0
dir = True
while True:
if dir:
duty += 10
else:
duty -= 10
if duty>100:
duty = 100
dir = False
elif duty<0:
duty = 0
dir = True
time.sleep(0.05)
ch.duty(duty)
|