Raspberry Pi Pico 实例之 PWM输出和PWN捕获
使用Pico 做一些简单的控制,本篇就是简单的PWM输出和捕获,为后面的一些项目做准备
参考资料
首先参考的是树莓派官方给的手册,使用的是Python 控制树莓派,用的软件是Thonny,在这就是在CSDN上面各种找资料,对于这个单片机来说,功能应该是挺齐全的,就是资料匮乏,手册上我只发现他的函数使用实例,但是应该还用很多参数可以去改变但是寻找不到,比如想要在程序中关闭中断,我没有找到相关的资料,如果有大佬知道,其务必告知。
Pico Python开发手册raspberry-pi-pico-python-sdk
先上代码,再将过程
from machine import Pin,PWM,Timer
class wave_detect:
def __init__(self,PWMoutPin,PWMinPin,Freq,Duty):
self.PWMoutPin = PWMoutPin
self.PWMinPin = PWMinPin
self.Freq = Freq
self.Duty = Duty
self.count = 0
self.tim=Timer()
def PWM(self):
pwm = PWM(Pin(self.PWMoutPin))
pwm.freq(self.Freq)
pwm.duty_u16(self.Duty)
def PWM_in(self):
pwmin = Pin(self.PWMinPin,Pin.IN,Pin.PULL_UP)
pwmin.irq(trigger=Pin.IRQ_FALLING,handler=self.handle_interrupt)
def handle_interrupt(self,event):
self.count+=1
def timer(self):
self.tim.init(period=1000,mode=Timer.PERIODIC,callback=self.time_interrupt)
self.PWM_in()
self.count=0
def time_interrupt(self,event):
print(self.count)
self.count=0
def main():
w1=wave_detect(3,4,4000,30000) ## 3,4为端口号,4000频率值,30000占空比值,占空比值最大好像是256*256,我随便设的
w1.PWM()
w1.timer()
if __name__ == '__main__':
main()
因为我某些需要,要写成面向对象的形式,如有不便,请见谅
开启PWM很简答,一个函数就写好了,我把它写在PWM函数
里面了,剩下就是用定时器捕获上升沿,计数,得到PWM频率
PWM_in函数
就是开启外部输入中断,先将输入端上拉,然后判断其下降沿,进入handle_interrupt函数
,中断处理函数,很简单的计数作用,再者就是timer函数
起到定时中断的作用,里面的定时器初始化时间单位是ms,毕竟有测频率,我设置的是1秒,然后读出多少上升沿就是多大频率。我在这里就是想中断几次就退出,但是不知道方法是什么,请大家不吝赐教