arduino测方波频率(外部中断)

admin 2023-6-14 371

单片机测方波频率有两种典型的方法:

1.如果频率较块时候:利用定时器(定时一秒),外部中断下降沿触发,计算一秒钟触发的次数,算出频率

2.如果频率较慢的话,利用两个下降沿或上升沿时间间隔,测出频率

教程针对第二种

1.arduino外部中断教程

介绍:中断程序可以看作是一段独立于主程序之外的程序,当中断触发时,控制器会暂停当前正在运行的主程序,而跳转去运行中 断程序,中断程序运行完后,会再回到之前主程序暂停的位置,继续运行主程序。如此便可做到实时响应处理事件的效果。

具体程序;设置一个引脚,默认是输入

//下降沿触发 触发中断0(引脚2),调用blink函数

attachInterrupt(0, blink, FALLING);

2.时间函数

micros():毫秒计数

micros();微秒计数

一般用unsiged long(无符号长整型表示)

3.程序编写(硬件连接,一个单片机引脚输出方波信号(模拟信号源),一个单片机引脚2读取方波信号)

//arduino测方波频率
int SensorINPUT = 2;      //连到中断0,也就是数字引脚`
unsigned long time1,time2,time_;
float pl;
void setup() {
  Serial.begin(9600);  //开启串口
  pinMode(SensorINPUT, INPUT);  //设置引脚输入(默认就是输入)
  attachInterrupt(0, blink, FALLING);   //下降沿触发,触发中断0,调用blink函数
  
}
void loop() {
   //int a = digitalRead(SensorINPUT);  //串口打印一下看看数据正确吗
   //Serial.println(a);  
   if(time2 > 1000000000 ){
     time2= 0;    //避免溢出
    }
   delay(10);
}
void blink() {               //中断函数blink()
  time1 = time2;    //保留上一次数值
  time2 = millis(); //读取现在的时间数值
  time_ = time2 - time1;   //计数两个下降沿之间的所经过的时间
  pl=1000.0/time_;     //计算频率  pl = 1/(time_/1000)  时间的倒数
  Serial.println("----");
  Serial.print(pl);     //打印数值
  Serial.println("----");
}


欢迎联系本站长QQ:3216572
最新回复 (7)
  • admin 2023-6-14
    2
    /*
    【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
    实验九十三:0.96寸I2C IIC通信128*64显示器 OLED液晶屏模块
    安装驱动库:工具—管理库—搜索“U8glib”-下载-安装
    实验接线:A4---SDA, A5---SCL
    项目二:测试之1-100循环显示数字
    */
    #include "U8glib.h"  //加载显示库文件
    U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI 实例化
    void setup() {
    }
    void loop() {
      for(int i=1;i<101;i++){
        u8g.firstPage();  //一下是显示实现部分
      do {
      u8g.setFont(u8g_font_fub30);
      //设置字体和自号,目前测试字号有fub14,17,20,30
      u8g.setPrintPos(0, 50); //显示的位置
      u8g.print(i);//显示变量i的值
      u8g.setFont(u8g_font_fub14);//设置字体和自号
      u8g.setPrintPos(95, 50); //显示的位置
      u8g.print("cm");//显示cm字样
      }
      while( u8g.nextPage() );
      delay(100);//显示的时间间隔。
      }
    }


  • admin 2023-6-14
    3
    #include <U8g2lib.h>                                       //u8g2库
    U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,SCL,SDA); //配置构造函数      
    int a=66;                                                  //定义变量a=66
    void setup() 
    {
        u8g2.begin();                                          //启动u8g2驱动程序
        u8g2.clearBuffer();                                    //清空显示屏缓存
    }
    void loop() 
    {
        face();                                                //调用函数,显示图案
        delay(1000);                                           //持续一秒钟
        heart();                                               //调用函数,显示英文字母和图案
        delay(1000);                                           //持续一秒钟
        letter(a);                                             //调用函数,显示中文和变量
        delay(1000);                                           //持续一秒钟
    }
    void face()
    {
        u8g2.clearBuffer();                                    //清空显示屏缓存
        
        u8g2.drawCircle(56,40,8,U8G2_DRAW_LOWER_LEFT);         //画四分之一圆,圆心坐标(56,44),半径8
        u8g2.drawCircle(56,40,8,U8G2_DRAW_LOWER_RIGHT);        //画四分之一圆
        u8g2.drawCircle(72,40,8,U8G2_DRAW_LOWER_LEFT);         //画四分之一圆
        u8g2.drawCircle(72,40,8,U8G2_DRAW_LOWER_RIGHT);        //画四分之一圆
        u8g2.drawCircle(56,41,8,U8G2_DRAW_LOWER_LEFT);         //加粗画四分之一圆
        u8g2.drawCircle(56,41,8,U8G2_DRAW_LOWER_RIGHT);        //加粗画四分之一圆
        u8g2.drawCircle(72,41,8,U8G2_DRAW_LOWER_LEFT);         //加粗画四分之一圆
        u8g2.drawCircle(72,41,8,U8G2_DRAW_LOWER_RIGHT);        //加粗画四分之一圆
        u8g2.drawLine(40,18,20,30);                            //画斜线,两端点坐标分别是(40,18)(20,30)
        u8g2.drawLine(88,18,108,30);                           //画斜线
        u8g2.drawLine(40,17,20,29);                            //加粗画斜线
        u8g2.drawLine(88,17,108,29);                           //加粗画斜线
        
        u8g2.sendBuffer();                                     //加载以上内容
    }
      void heart()
    {
        u8g2.clearBuffer();                                    //清空显示屏缓存
        
        u8g2.setFont(u8g2_font_open_iconic_human_2x_t);        // 设置字体
        u8g2.drawGlyph(58,30,66);                              // 画心,符号左下角坐标为(58,36),符号编号为66
        u8g2.setFont(u8g2_font_unifont_t_chinese2);            //设置字体
        u8g2.drawUTF8(58+20,30,"ZFY");                         //显示英文,左下角位置坐标为(78,30)
        u8g2.drawUTF8(58-14-16,30,"JHR");                      //显示英文,左下角位置坐标为(28,30)    
        
        u8g2.sendBuffer();                                     // 加载以上内容
    }
      void letter(int a)
    {   
        u8g2.clearBuffer();                                    //清空显示屏缓存
        
        //int8_t a=u8g2.getMaxCharHeight();                      //获取最大高度
        //int8_t b=u8g2.getMaxCharWidth();                       //获取最大宽度
        
        u8g2.setFont(u8g2_font_unifont_t_chinese2);            //设置字体
        u8g2.drawUTF8(20,17,"智能检测系统");                    //显示文字,左下角位置坐标为(20,17)
        u8g2.drawUTF8(50,34,":");                             //显示:,左下角坐标为(50,34)
        u8g2.drawUTF8(0,34,"位移为:");
        u8g2.setCursor(64,34);                                 //设置将要打印变量的左下角坐标
        u8g2.drawUTF8(0,34,"位移为:");
        u8g2.print(a);                                         //打印变量a
         
        u8g2.sendBuffer();                                     // 加载以上内容
      }


  • admin 2023-6-14
    4
    #include <Wire.h>
    
    #include <Adafruit_GFX.h>
    
    #include <Adafruit_SSD1306.h>
    
     
    
    #define OLED_RESET 4
    
    Adafruit_SSD1306 display(OLED_RESET);
    
     
    
    #define NUMFLAKES 10
    
    #define XPOS 0
    
    #define YPOS 1
    
    #define DELTAY 2
    
     
    
    #define LOGO16_GLCD_HEIGHT 16
    
    #define LOGO16_GLCD_WIDTH  16
    
     
    
    #if (SSD1306_LCDHEIGHT != 64)
    
    #error("Height incorrect, please fix Adafruit_SSD1306.h!");
    
    #endif
    
    /*-----显示文字一,把代码放入数组中---*/
    
    static const uint8_t PROGMEM photo_128x64[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x9f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xfd,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdd,0xfe,0xeb,0xbb,0xd3,0xc3,0x87,0x6d,0xfc,0x63,0xc2,0x3f,0xff,0xff,0xff,0xff,0xd9,0xb6,0x6b,0xbb,0xcd,0x9d,0xbb,0x6d,0xf3,0xcd,0xd9,0xbf,0xff,0xff,0xff,0xff,0xdb,0xb7,0x6d,0x57,0xde,0xdd,0xbb,0x9d,0xf7,0xde,0xdd,0xbf,0xff,0xff,0xff,0xff,0xea,0xb5,0x5d,0x57,0xde,0xdd,0xbb,0x9d,0xf7,0xde,0xd9,0x9f,0xff,0xff,0xff,0xff,0xe6,0xf5,0xdd,0x57,0xde,0xdd,0xbb,0x1d,0xf7,0xde,0xdd,0x9f,0xff,0xff,0xff,0xff,0xe6,0x79,0x9c,0xce,0xcd,0xdd,0xbb,0x6d,0xbb,0xcd,0xdd,0x9f,0xff,0xff,0xff,0xff,0xf7,0x7b,0xbe,0xee,0xd3,0xfd,0x87,0xed,0xbc,0xe3,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xbf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xdf,0xff,0xbf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xbf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3e,0x67,0xfe,0xe7,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x3e,0x71,0xf0,0x06,0x00,0x3f,0xff,0xff,0xf8,0x03,0xfc,0xff,0x8f,0x80,0x1f,0xfe,0x7e,0x78,0xfe,0x3f,0x8e,0x3f,0xff,0xff,0xf8,0x00,0xfc,0xff,0xcf,0x80,0x07,0xfc,0xfe,0x7c,0x7e,0x7f,0xff,0x3f,0xff,0xff,0xf9,0xf8,0x7c,0xff,0x8f,0x8f,0xc3,0xfc,0xfe,0x7e,0xfe,0x7f,0x3f,0x3f,0xff,0xff,0xf9,0xfe,0x7c,0xff,0x8f,0x8f,0xe3,0xf9,0xfe,0x7f,0xfe,0x7f,0x3e,0x7f,0xff,0xff,0xf9,0xfe,0x3c,0xff,0xcf,0x8f,0xf3,0xf1,0xfe,0x7e,0x7e,0xff,0x3e,0x7f,0xff,0xff,0xf9,0xfe,0x3c,0xff,0xcf,0x8f,0xf3,0xe1,0xfe,0x00,0x7c,0xff,0x3e,0x7f,0xff,0xff,0xf9,0xfe,0x3c,0xff,0x8f,0x8f,0xf3,0xc1,0x80,0x03,0xfc,0xff,0x3e,0x7f,0xff,0xff,0xf9,0xfe,0x7c,0xff,0xcf,0x8f,0xf3,0x89,0x80,0x7f,0xf8,0x07,0x7e,0x7f,0xff,0xff,0xf9,0xfc,0x7c,0x00,0x0f,0x8f,0xe3,0x99,0xfe,0x7f,0xf8,0xc6,0x7e,0x7f,0xff,0xff,0xf8,0xf0,0x7c,0x00,0x0f,0x8f,0x87,0xb9,0xff,0x3f,0xf8,0xe6,0x00,0x0f,0xff,0xff,0xf8,0x00,0xfc,0xff,0x8f,0x80,0x0f,0xf9,0xff,0x3f,0xf0,0xe7,0xb9,0x8f,0xff,0xff,0xf8,0x07,0xfc,0xff,0x8f,0x80,0x3f,0xf9,0xff,0x3f,0xf0,0xe7,0xff,0xcf,0xff,0xff,0xf9,0xff,0xfc,0xff,0xcf,0x8f,0xff,0xf9,0xff,0x3f,0xf4,0xe7,0xff,0xcf,0xff,0xff,0xf9,0xff,0xfc,0xff,0xcf,0x8f,0xff,0xf9,0xff,0x1f,0xfc,0xe7,0xff,0xcf,0xff,0xff,0xf9,0xff,0xfc,0xff,0x8f,0x8f,0xff,0xf9,0xff,0x9f,0xfc,0xe4,0x00,0xcf,0xff,0xff,0xf9,0xff,0xfc,0xff,0xcf,0x8f,0xff,0xf9,0xff,0x9f,0x3c,0xe4,0x00,0x9f,0xff,0xff,0xf9,0xff,0xfc,0xff,0xcf,0x8f,0xff,0xf9,0xff,0x8f,0x3c,0x47,0xff,0x9f,0xff,0xff,0xf9,0xff,0xfc,0xff,0xcf,0x8f,0xff,0xf9,0xff,0xcf,0x3c,0x07,0xff,0x9f,0xff,0xff,0xf9,0xff,0xfc,0xff,0xcf,0x8f,0xff,0xf9,0xff,0xc7,0x7c,0xff,0xff,0x9f,0xff,0xff,0xf9,0xff,0xfc,0xff,0xcf,0xcf,0xff,0xf9,0xff,0xe0,0x7c,0xff,0xff,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf9,0xff,0xf0,0x7d,0xff,0xf8,0x3f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xf9,0xff,0xfc,0xff,0xff,0xfd,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff };
    
    0void setup()   {                
    
      Serial.begin(9600);
    
      delay(500);
    
      // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
    
      display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
    
    }
    
    void loop() {
    
      test_SSD1306();
    
    }
    
    void test_SSD1306(void){
    
      display.clearDisplay();   // clears the screen and buffer
    
      display.drawBitmap(0,0,photo_128x64,128,62,WHITE);
    
      display.display();
    
      delay(2000);
    
    }


  • admin 2023-6-16
    5
    /*
    Arduino Frequency meter
    Ansifa
    2013/1/5
    */
    int divider[6] = {0, 1, 8, 64, 256, 1024};
    int prescaler = 5;
    double count = 0;
    double middle = 0;
    char x = 0;
    ISR(TIMER1_OVF_vect)
    {
        if (prescaler < 4)
        {
            prescaler++;
        }
    }
    void interrupt()
    {
        if (!x)
        {
            count = TCNT1;
            TCNT1 = 0x000;
            TCCR1B = prescaler;
            attachInterrupt(0, interrupt, FALLING);
        }
        else
        {
            middle = TCNT1;
            attachInterrupt(0, interrupt, RISING);
        }
        x = ~x;
    }
    void setup()
    {
        Serial.begin(57600);
        TIMSK1 = 0x01;
        TCCR1A = 0x00;
        attachInterrupt(0, interrupt, RISING);
    }
    void loop()
    {
        Serial.print("Freq: ");
        Serial.print(16000000.0 / divider[prescaler] / count);
        Serial.print(" Hz\t\tDuty: ");
        Serial.print(middle / count * 100);
        Serial.print(" %\t\tPeriod: ");
        Serial.print(0.0000625 * divider[prescaler]*count);
        Serial.print(" ms\t\tH-time: ");
        Serial.print(0.0000625 * divider[prescaler]*middle);
        Serial.print(" ms\t\tL-time: ");
        Serial.print(0.0000625 * divider[prescaler]*(count - middle));
        Serial.println(" ms");
        if (prescaler > 1)
        {
            prescaler--;
            delay(200);
        }
        delay(100);
    }


  • admin 2023-6-24
    6
    int microphonePin = 2; // 麦克风数字输入引脚
    volatile unsigned long pulseStartTime; // 脉冲起始时间
    volatile unsigned long pulseEndTime; // 脉冲结束时间
    volatile unsigned long pulseDuration; // 脉冲持续时间
    void setup() {
      Serial.begin(9600);
      pinMode(microphonePin, INPUT);
      attachInterrupt(digitalPinToInterrupt(microphonePin), handleInterrupt, RISING);
    }
    void loop() {
      if (pulseDuration > 0) {
        double frequency = 1000000 / pulseDuration; // 计算声音频率
        Serial.print("Frequency: ");
        Serial.print(frequency);
        Serial.println(" Hz");
        pulseDuration = 0; // 重置脉冲持续时间
      }
    }
    void handleInterrupt() {
      if (digitalRead(microphonePin) == HIGH) {
        pulseStartTime = micros(); // 记录脉冲开始时间
      } else {
        pulseEndTime = micros(); // 记录脉冲结束时间
        pulseDuration = pulseEndTime - pulseStartTime; // 计算脉冲持续时间
      }
    }

    在该示例代码中,我们使用attachInterrupt函数将中断服务程序handleInterrupt绑定到麦克风数字输入引脚上。每次中断发生时,handleInterrupt函数会记录下当前的时间(即脉冲开始或结束时间),并计算出两次脉冲之间的时间差(即脉冲持续时间)。在loop函数中,我们可以通过计算声音频率来实现声音频率的识别。需要注意的是,由于中断服务程序处理速度较快,因此该方法可以实现更高精度的声音频率识别。

  • admin 2023-6-24
    7
    #define SAMPLE_SIZE 10 // 移动平均滤波器采样点数
    int analogPin = A0; // 输入引脚
    int samples[SAMPLE_SIZE]; // 存储采样值的数组
    int sampleIndex = 0; // 当前采样值的索引
    float total = 0; // 所有采样值的和
    void setup() {
      Serial.begin(9600);
    }
    void loop() {
      int sensorValue = analogRead(analogPin); // 读取输入信号
      total -= samples[sampleIndex]; // 减去最旧的采样值
      samples[sampleIndex] = sensorValue; // 将新的采样值存储到数组中
      total += sensorValue; // 加上新的采样值
      sampleIndex++; // 增加索引
      if (sampleIndex >= SAMPLE_SIZE) {
        sampleIndex = 0; // 索引循环
      }
      float average = total / float(SAMPLE_SIZE); // 计算平均值
      Serial.println(average); // 输出平均值
      delay(10); // 降低采样率,提高计算精度
    }

    10次平均处理。

  • admin 2023-6-26
    8
    #include <SPI.h> // 加载SPI库
    #include <Wire.h> // 加载Wire库
    #include <Adafruit_GFX.h> // 加载Adafruit_GFX库
    #include <Adafruit_SSD1306.h> // 加载Adafruit_SSD1306库
     
    int SensorINPUT = 2;      //连到中断0,也就是数字引脚`
    unsigned long time1,time2,time_;
    float pl;
     
    // 定义 OLED屏幕的分辨率
    Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
     
    void setup() {
      Serial.begin(9600); // 设置串口波特率
      Serial.println("OLED FeatherWing test"); // 串口输出
      display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // 设置OLED的I2C地址
      pinMode(SensorINPUT, INPUT);  //设置引脚输入(默认就是输入)
      attachInterrupt(0, blink, FALLING);   //下降沿触发,触发中断0,调用blink函数
    }
     
    void loop() {
     
      if(pl>50 && pl<2000){
     
       
      display.clearDisplay(); // 清空屏幕
      display.setTextSize(2); // 设置字体大小
      display.setTextColor(SSD1306_WHITE); // 设置字体颜色
      display.setCursor(0,0); // 设置开始显示文字的坐标
      display.println("frequency:"); // 输出的字符
      display.setTextSize(3); // 设置字体大小
      display.setCursor(0,16); // 设置开始显示文字的坐标
      display.println(pl); // 输出的字符
      display.setCursor(80,40); // 设置开始显示文字的坐标
      display.println("Hz"); // 输出的字符
      display.display(); // 使更改的显示生效
      delay(200);
       }
    }
    void blink() {               //中断函数blink()
      time1 = time2;    //保留上一次数值
      time2 = millis(); //读取现在的时间数值
      time_ = time2 - time1;   //计数两个下降沿之间的所经过的时间
      pl=1000.0/time_;     //计算频率  pl = 1/(time_/1000)  时间的倒数
      Serial.println("----");
      Serial.print(pl);     //打印数值
      Serial.println("----");
    }


返回