import pyb,time
from pyb import Pin, Timer
p = Pin('P5') #Pin5 是定时器 2 通道 4
tim = Timer(2, freq=50) #配置定时器 4 频率
ch = tim.channel(4, Timer.PWM, pin=p) #配置定时器通道 1
while (True):
ch.pulse_width_percent(5) #配置通道占空比
time.sleep(1000)
ch.pulse_width_percent(7.5) #配置通道占空比
time.sleep(1000)
在csdn上找的代码,发现占空比不能调为浮点数。编译会报错 cant convert float to int,不知如何是好。