zoukankan      html  css  js  c++  java
  • Arduino驱动无源蜂鸣器发声

    tone()函数

    tone(pin, frequency)
    tone(pin, frequency, duration)
    # 参数
    pin: the pin on which to generate the tone
    frequency: the frequency of the tone in hertz - unsigned int
    duration: the duration of the tone in milliseconds (optional) - unsigned long

    tone()的使用说明

    产生指定频率的占空比为50%的方波. 可以指定持续时间, 若未指定, 则持续到调用noTone().
    在同一时间只能产生一个频率的方波, 如果tone正执行在一个pin上, 在其他pin上调用tone()将不起作用. 如果在同一个pin上再次调用tone, 则会使用新指定的频率.
    除了Mega开发板以外, 使用tone()会与pin 3 和 pin 11 脚的 PWM输出冲突.
    输出的最低频率为31Hz.

    noTone函数

    noTone(pin)
    # 参数
    pin: the pin on which to stop generating the tone

    注意: 如果在不同的pin脚上有多个喇叭/蜂鸣器, 在对下一个pin调用tone()前必须对前一个pin调用noTone().

    接线

    蜂鸣器的+脚接Arduino D6, -脚接GND

    测试代码

    #define TONEPIN 6
    #define TONE_BASE 294
    
    void setup() {
      pinMode(TONEPIN, OUTPUT);
    }
    void loop() {
      for(int i = 0; i < 120; i ++)   {
        tone(TONEPIN, TONE_BASE + i * 15, 100);
        delay(2000);
        noTone(TONEPIN);
      }
      delay(2000);
    }

    测试可以看到, tone方法是非阻塞的, 如果delay时间比tone的duration短, 则duration不起作用, 实际时间是delay的时间.

  • 相关阅读:
    __dict__和dir()的区别:未完
    [leetcode] Subsets II
    [leetcode] Decode Ways
    [leetcode] Gray Code
    [leetcode] Merge Sorted Array
    [leetcode] Partition List
    [leetcode] Scramble String
    [leetcode] Maximal Rectangle
    [leetcode] Remove Duplicates from Sorted List II
    [leetcode] Remove Duplicates from Sorted List
  • 原文地址:https://www.cnblogs.com/milton/p/8860291.html
Copyright © 2011-2022 走看看