PWM_IN_OUT

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秒,然后读出多少上升沿就是多大频率。我在这里就是想中断几次就退出,但是不知道方法是什么,请大家不吝赐教

module pwm ( CLK, RSTn, AddDuty_In, SubDuty_In, AddPeriod_In, SubPeriod_In, Count_D_Display, Count_P_Display, Digitron_Out, DigitronCS_Out, PWM_LED_Out, PWM_EPI_Out ); input CLK; input RSTn; //SW0 input AddDuty_In; //KEY3 input SubDuty_In; //KEY2 input AddPeriod_In; //KEY1 input SubPeriod_In; //KEY0 input Count_D_Display; //SW1 input Count_P_Display; //SW2 output [7:0]Digitron_Out; output [5:0]DigitronCS_Out; output PWM_LED_Out; //LED0 output PWM_EPI_Out; //A6 assign PWM_EPI_Out = PWM_LED_Out; wire [7:0]Duty; wire [23:0]Count_P; wire [23:0]Count_D; Duty_Period_Adjust_module U1 ( .CLK( CLK ) , .RSTn( RSTn ) , .AddDuty_In( AddDuty_In ) , // input - from top .SubDuty_In( SubDuty_In ) , // input - from top .AddPeriod_In( AddPeriod_In ) , // input - from top .SubPeriod_In( SubPeriod_In ) , // input - from top .Duty( Duty ) , // output [7:0] - to U2, U3 .Count_P( Count_P ) // output [23:0] - to U2, U3 ); PWM_Generate_module U2 ( .CLK( CLK ) , .RSTn( RSTn ) , .Duty( Duty ) , // input [7:0] - from U1 .Count_P( Count_P ) , // input [23:0] - from U1 .PWM_Out( PWM_LED_Out ), // output - to top .Count_D( Count_D ) // output [23:0] - to U3 ); Digitron_NumDisplay_module U3 ( .CLK( CLK ) , .RSTn( RSTn ) , .Count_D_Display( Count_D_Display ) , // input - from top .Count_P_Display( Count_P_Display ) , // input - from top .Count_D( Count_D ) , // input [23:0] - from U2 .Count_P( Count_P ) , // input [23:0] - from U1 .Duty( Duty ) , // input [7:0] - from U1 .Digitron_Out( Digitron_Out ) , // output [7:0] - to top .DigitronCS_Out( DigitronCS_Out ) // output [5:0] - to top ); endmodule
06-11
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值