文章标题

来源:互联网 发布:网购秒杀软件哪个好 编辑:程序博客网 时间:2024/06/15 04:32

toneAC Library for Arduino

When you send a tone to a speaker with the standard tone library, the loudest is at 50% duty cycle (only on half the time). Which at 5 volts, is like sending only 2.5v to the speaker. With toneAC, we’re sending out of phase signals on two pins. So in effect, the speaker is getting 5 volts instead of 2.5, making it nearly twice as loud.

Disadvantages are that it must use certain pins and it uses two pins instead of one.
It exclusively uses port registers for the fastest and smallest code possible.
http://playground.arduino.cc/Code/ToneAC

Syntax
toneAC( frequency [, volume [, length [, background ]]] ) - Play a note.

  • frequency - Play the specified frequency indefinitely,
  • turn off with toneAC().

    • volume - [optional] Set a volume level(设置音量). (default: 10, range: 0 to 10 [0 = off])
  • length - [optional] Set the length to play in milliseconds. (default: 0 [forever], range: 0 to 2^32-1)

    • background - [optional] Play note in background or pause till finished? (default: false, values: true/false)
      toneAC() - Stop output.
void loop() {  for (unsigned long freq = 150; freq <= 15000; freq += 10){      toneAC(freq); // Play the frequency (150 Hz to 15 kHz).    delay(1);     // Wait 1 ms so you can hear it.  }

完整实例:

#include <toneAC.h>void setup() {} // Nothing to setup, just start playing!void loop() {  for (unsigned long freq = 150; freq <= 15000; freq += 10){      toneAC(freq); // Play the frequency (150 Hz to 15 kHz).    delay(1);     // Wait 1 ms so you can hear it.  }  toneAC(0); // Turn off toneAC, can also use noToneAC().  while(1); // Stop.}

toneAC(steerFreq, 10, 0, true); // Output pulse for stepper
可以同于电机的输出频率上,

tone函数可以在一个输出引脚上输出一个方波。
noTone()停止方波
tone(4,500)即:第4个引脚输出500HZ的方波

0 0