狗趴(GodPub),开源硬件学习与实践

标题: 【二叔科技】二叔测频率的测试 [打印本页]

作者: 二叔科技    时间: 2014-11-25 15:25
标题: 【二叔科技】二叔测频率的测试
要测频率先得有一个频率源吧,不太准凑合用,用blink改造一下,大概20Hz的样子吧
  1. /*
  2.   Blink
  3.   Turns on an LED on for one second, then off for one second, repeatedly.

  4.   This example code is in the public domain.
  5. */

  6. // Pin 13 has an LED connected on most Arduino boards.
  7. // give it a name:
  8. int led = 13;

  9. // the setup routine runs once when you press reset:
  10. void setup() {               
  11.   // initialize the digital pin as an output.
  12.   pinMode(led, OUTPUT);     
  13. }

  14. // the loop routine runs over and over again forever:
  15. void loop() {
  16.   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  17.   delay(25);               // wait for a second
  18.   digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  19.   delay(25);               // wait for a second
  20. }
复制代码



作者: 二叔科技    时间: 2014-11-25 15:56
参考了一个卖传感器的商家的代码,当年二叔买这个传感器花了40+还有运费,一直扔着没用呢
http://detail.etao.com/18934469888.htm
  1. int pin = 8;
  2. unsigned long duration;
  3. unsigned long starttime;
  4. unsigned long sampletime_ms = 5000;
  5. unsigned long lowpulseoccupancy = 0;
  6. float ratio = 0;

  7. void setup() {
  8.   Serial.begin(9600);
  9.   pinMode(pin,INPUT);
  10.   starttime = millis();
  11. }

  12. void loop() {
  13.   duration = pulseIn(pin, LOW);
  14.   lowpulseoccupancy++;
  15.   if ((millis()-starttime) > sampletime_ms)
  16.   {
  17.     ratio = lowpulseoccupancy*1000/sampletime_ms;
  18.     Serial.print(ratio);
  19.     Serial.println("Hz");
  20.     lowpulseoccupancy = 0;
  21.     starttime = millis();
  22.   }
  23. }
复制代码




作者: 二叔科技    时间: 2014-11-25 15:58
测试结果20Hz蛮准确的


改到250Hz(不一定准),测得结果249Hz,这个结果可以接受啦


作者: 二叔科技    时间: 2014-11-25 16:04
更专业的频率测试,不过二叔没看懂,来自饥渴工坊
http://www.geek-workshop.com/thread-3085-1-1.html





欢迎光临 狗趴(GodPub),开源硬件学习与实践 (http://forum.godpub.com/) Powered by Discuz! X3.2