tone()
描述
在指定的 pin 脚上产生一个指定频率的方波 (工作周期是 50% 的 PWM 信号),可以指定持续时间或者持续直到呼叫 noTone(),在这个指定的 pin 脚上可以接蜂鸣器或者喇叭来发出一段音频。
同一时间只能产生一段音频,如果这段音频已经在别的 pin 脚上拨放则呼叫 tone() 是无效的;如果是在同一个 pin 脚上拨放时,呼叫 tone() 则是设定它的频率。
注意: 如果你想要在多个 pin 脚上拨放不同的音频,在呼叫下一次 tone() 前你需要先呼叫 noTone()。
语法
tone(pin, frequency)
tone(pin, frequency, duration)
参数
pin: 指定要产生音频的 pin 脚
frequency: 声音的频率 (Hz),unsigned int 型别
duration: 音频的持续时间,单位为毫秒,unsigned long 型别 (此参数为非必需)
回传
无回传值
使用限制
tone() 使用了 pin 32 脚的 PWM 计时器,因此不可与 analogWrite(32) 同时被呼叫,以避免冲突;此外,tone() 亦不可与共用相同计时器的 MsTimer2 或 IRremote 函式库同时使用。
See also
- noTone()
- Tutorial: Tone
- Tutorial: Pitch follower
- Tutorial: Simple Keyboard
- Tutorial: multiple tones
本页由热血青年 LBU 译自英文版。
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.
