Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

ESP32(MicroPython) 蜂鸣器相关程序改PMW


ESP32(MicroPython) 蜂鸣器相关程序改PMW

本人之前发布了一些蜂鸣器相关程序,使用延时方式输出,这主要受例程影响。最近发现改用PMW输出后频率更准确。这里发出这部分程序改用PMW输出的版本。


PMW扫频


#导入Pin模块

from machine import Pin

import time

from machine import PWM  

 

a=19

#程序入口

while a<100:

    a+=1

    beep=PWM(Pin(22),freq=a,duty=512)

    time.sleep_ms(50)

    beep=PWM(Pin(22),freq=a,duty=0)

    print(a)

while a<1000:

    a+=10

    beep=PWM(Pin(22),freq=a,duty=512)

    time.sleep_ms(50)

    beep=PWM(Pin(22),freq=a,duty=0)

    print(a)

while a<10000:

    a+=100

    beep=PWM(Pin(22),freq=a,duty=512)

    time.sleep_ms(50)

    beep=PWM(Pin(22),freq=a,duty=0)

    print(a)    

串口电子琴PMW版


#导入Pin模块

from machine import Pin

import time

from machine import PWM   

    

#程序入口

while True:

    e=int(input("输入音调,第一位为升降调,其中5为中音;第二位(1到7)为音调"))

    a=0

    t=0

    if e%10==1 :

      a=4186

    elif e%10==2 :

      a=4698

    elif e%10==3 :

      a=5274

    elif e%10==4 :

      a=5587

    elif e%10==5 :

      a=6272

    elif e%10==6 :

      a=7040

    elif e%10==7 :

      a=7901

    if e//10<9 :

      t=e//10

      e=9-t

      t=2**e

      a=round(a/t)

    else :

        a=262

    beep=PWM(Pin(22),freq=a,duty=512)

    time.sleep_ms(250)

    beep=PWM(Pin(22),freq=a,duty=0)

电子琴扫频PMW版


#导入Pin模块

from machine import Pin

import time

from machine import PWM   

 

e=10

b=0

#程序入口

while True:

    e+=1

    if e%10==8 :

      e+=3

    if e>=101 :

      e=11

      time.sleep_ms(1000)

    if e%10==1 :

      a=4186

    elif e%10==2 :

      a=4698

    elif e%10==3 :

      a=5274

    elif e%10==4 :

      a=5587

    elif e%10==5 :

      a=6272

    elif e%10==6 :

      a=7040

    elif e%10==7 :

      a=7901

    if e//10<9 :

      t=e//10

      b=9-t

      t=2**b

    a=round(a/t)

    if a<1 :

        a=1

    print(e)    

    beep=PWM(Pin(22),freq=a,duty=512)

    time.sleep_ms(100)

    beep=PWM(Pin(22),freq=a,duty=0)

串口控制PMW输出频率


#导入Pin模块

from machine import Pin

import time

from machine import PWM  

    

#程序入口

while True:

    a=int(input("输入频率(1到500000的整数)"))

    beep=PWM(Pin(22),freq=a,duty=512)

    time.sleep_ms(500)

    beep=PWM(Pin(22),freq=a,duty=0)


————————————————

版权声明:本文为CSDN博主「路易斯720」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_74155302/article/details/131037534



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0095851421356201 seconds