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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz

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

查看数: 7196 | 评论数: 3 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2014-11-25 15:25

正文摘要:

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

回复

二叔科技 发表于 2014-11-25 15:56:31
参考了一个卖传感器的商家的代码,当年二叔买这个传感器花了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. }
复制代码



QQ|Archiver|手机版|狗趴(GodPub) Arduino&Raspberry Pi开源硬件学习与实践[QQ群:20085629]  

GMT+8, 2024-5-19 18:33 , Processed in 0.036285 second(s), 32 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表