zoukankan      html  css  js  c++  java
  • 3.3.2 pulseIn(pin,state,timeout)

    pulseIn函数用于读取引脚脉冲的时间长度,脉冲可以是HIGH或LOW。如果是HIGH,函数将先等引脚变为高电平,然后开始计时,一直到变为低电平为止。返回脉冲持续的时间长短, 单位为ms。如果超时还没有读到的话, 将返回0。

    pulseIn函数返回值类型为无符号长整型(unsigned long),3个参数分别表示脉冲输入的引脚、脉冲响应的状态(高脉冲或低脉冲)和超时时间。函数原型在wiring_pulse.c中,如下:

    unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
    {     
          uint8_t bit = digitalPinToBitMask(pin);
          uint8_t port = digitalPinToPort(pin);
          uint8_t stateMask = (state ? bit : 0);
          unsigned long width = 0;
          // keep initialization out of time critical area
         
          unsigned long numloops = 0;
          unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
         
          // wait for any previous pulse to end
          while ((*portInputRegister(port) & bit) == stateMask)
                    if (numloops++ == maxloops)
                    return 0;
          // wait for the pulse to start
          while ((*portInputRegister(port) & bit) != stateMask)
                    if (numloops++ == maxloops)
                    return 0;
          // wait for the pulse to stop
          while ((*portInputRegister(port) & bit) == stateMask)
                    width++;
         
          return clockCyclesToMicroseconds(width * 10 + 16);
    }

    可以在开发环境的下列实例程序中找到pulseIn函数的应用:

    Memsic2125.pde、Ping.pde

  • 相关阅读:
    ASP.NET 实现邮件发送和接受的功能(Sockets)
    使用Sql server进行分布式查询
    Sqlserver中的一些技巧
    使用sql server中的全文索引
    水晶报表的装载和修改文本
    创建作业的通用存储过程
    MS SQL数据库备份和恢复
    数据库运用XML操作
    安装程序自动安装数据库
    ASP.NET 实现邮件发送和接受的功能(Mail)
  • 原文地址:https://www.cnblogs.com/jikexianfeng/p/6242642.html
Copyright © 2011-2022 走看看