关于Arduino的 FreqCount Library

来源:互联网 发布:网游加速器免费版mac 编辑:程序博客网 时间:2024/04/27 02:05

FreqCount requires the input frequency as a digital level signal on a specific pin.
FreqCount requires the input frequency as a digital level signal on a specific pin.

An amplifier may be needed if the input signal is a sine wave or small AC signal which can not directly drive a TTL logic level input.

Basic UsageFreqCount.begin(GateInterval);Begin frequency counting. GateInterval is the time in milliseconds for each measurement. Using 1000 provides direct frequency output without mulitplying or dividing by a scale factor.FreqCount.available();Returns true when a new measurement is available. Only a single measurement is buffered, so it must be read before the next gating interval.FreqCount.read();Returns the most recent measurement, an unsigned long containing the number of rising edges seen within the gating interval. There is no delay between gating intervals, so for example a 1000.5 Hz input (and perfectly accurate crystal osciallator) will alternate between 1000 and 1001 when used with a 1 second gate interval.FreqCount.end();Stop frequency counting. PWM (analogWrite) functionality may be used again.

Example Program

Open from the menu: File > Examples > FreqCount > Serial_Output

#include <FreqCount.h>void setup() {  Serial.begin(57600);  FreqCount.begin(1000);}void loop() {  if (FreqCount.available()) {    unsigned long count = FreqCount.read();    Serial.println(count);  }}

Interrupt Latency Requirements

FreqCount uses a timer interrupt for the gate interval. If another interrupt is running, or interrupts are disabled by the main program, response to the timer could be delayed. That will lengthen the gate interval, perhaps leading to counting more cycles. The next gate interval will be shorter (if normal interrupt response occurs), so a corresponding decrease will occur in the next measurement.During USB startup on Teensy, activity from the PC can cause interrupts which interfere with FreqCount's gate interval. Other libraries which disable interrupts for long times, such as NewSoftSerial, can cause trouble.

FreqCount vs FreqMeasure
FreqCount: best for 1 kHz to 8 MHz (up to 65 MHz with Teensy 3.0 & 3.1)
FreqMeasure: best for 0.1 Hz to 1 kHz
FreqCount measures the number of cycles that occur during a fixed “gate interval” time. This works well for relatively high frequencies, because many cycles are likely to be counted during gate interval. At lower frequencies, very few cycles are counted, giving limited resolution.

FreqMeasure measures the elapsed time during a single cycle. This works well for relatively low frequencies, because a substantial time elapses. At higher frequencies, the short time can only be measured at the processor’s clock speed, which results in limited resolution.

原创粉丝点击