使用 Arduino 测量频率
Measuring frequency with Arduino
我正在做一个使用 Arduino 测量输入方波频率的小项目,我使用定时器和硬件中断都做到了,这是我的代码:
#include <TimerOne.h>
long count1=0,freq1=0;
void setup(){
attachInterrupt(5,count,RISING);
Timer1.initialize(1000000); //increments count for 1 sec and prints out count
Timer1.attachInterrupt( freq ); // attach the service routine here
Serial.begin(9600);
}
void loop(){
while(freq1)
Serial.println(freq1);
}
void count()
{
count1++;
}
void freq(){
freq1 = count1;
count1=0;
}
问题是输出值几乎是输入频率的两倍,我不知道这是怎么回事。我在密集的谷歌搜索后了解到中断。似乎没有用。
这里有
一个工作的例子。它使用本机ISR函数(更快)而不是arduino中断处理程序。使用 Timer1 获得最佳分辨率和精度。
代码很大,但您感兴趣的部分是:
The "// Setup Timer1 for precise timing" portion of void setup() ISR(TIMER1_OVF_vect) -> for timer1 overflow counter ISR(INT0_vect) // External Interrupt pin 2 [D2] void PulseCaptureScheduler_callback() // Starts a capture and flags a timeout void PrintInfoScheduler_callback() // Processes the results
相关文章:
- 在C++中测量postscript字体宽度
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 在指针的帮助下,文本文件中单词的频率
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 函数计算用户按下按钮的频率
- 在条件变量中触发错误信号的频率是多少
- 用时钟测量程序的运行时间
- 用于测量微秒的QueryPerformanceCounter的奇怪行为
- 为什么根据测量位置测量经过的时间时会有很大的不同?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 为什么在尝试测量函数返回所需的时间时,我的运行时编号是错误的?
- 分别测量每个线程上花费的 CPU 时间(C++)
- 如何测量管道延迟?
- 如何在 c++ 中循环中使用频率变化的声音?
- 查找给定范围内最长连续 1 的频率
- 字母 字母频率
- 测量时间以在 c++ 中生成更好的随机数
- 我可以使用软件来测量发送信息的频率(以赫兹为单位)吗?
- 生成随机骰子并测量结果的频率
- 使用 Arduino 测量频率