二叔科技 发表于 2014-11-25 15:25:12

【二叔科技】二叔测频率的测试

要测频率先得有一个频率源吧,不太准凑合用,用blink改造一下,大概20Hz的样子吧
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/

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

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

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
delay(25);               // wait for a second
digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
delay(25);               // wait for a second
}

二叔科技 发表于 2014-11-25 15:56:31

参考了一个卖传感器的商家的代码,当年二叔买这个传感器花了40+还有运费,一直扔着没用呢
http://detail.etao.com/18934469888.htm
int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 5000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;

void setup() {
Serial.begin(9600);
pinMode(pin,INPUT);
starttime = millis();
}

void loop() {
duration = pulseIn(pin, LOW);
lowpulseoccupancy++;
if ((millis()-starttime) > sampletime_ms)
{
    ratio = lowpulseoccupancy*1000/sampletime_ms;
    Serial.print(ratio);
    Serial.println("Hz");
    lowpulseoccupancy = 0;
    starttime = millis();
}
}


二叔科技 发表于 2014-11-25 15:58:22

测试结果20Hz蛮准确的


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

二叔科技 发表于 2014-11-25 16:04:11

更专业的频率测试,不过二叔没看懂,来自饥渴工坊
http://www.geek-workshop.com/thread-3085-1-1.html
页: [1]
查看完整版本: 【二叔科技】二叔测频率的测试