极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11377|回复: 2

pulseIn函数工作,PC指针是不是一直在函数内部

[复制链接]
发表于 2014-7-26 08:44:15 | 显示全部楼层 |阅读模式
本帖最后由 zhixiang 于 2014-7-26 11:41 编辑

pulseIn函数工作时,PC指针是不是一直在函数内部,直到电平变化为止,然后跳出函数,运行一个指针命令。,比如高电平持续了3s,那这3s指针一直停留在这里计数,其他的事都坐不了?Arduino不好单步调试,请知道的朋友指点迷津,谢谢
回复

使用道具 举报

发表于 2014-7-26 10:07:13 | 显示全部楼层
最好的解释就是直接看代码。
  1. /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
  2. * or LOW, the type of pulse to measure.  Works on pulses from 2-3 microseconds
  3. * to 3 minutes in length, but must be called at least a few dozen microseconds
  4. * before the start of the pulse. */
  5. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
  6. {
  7.         // cache the port and bit of the pin in order to speed up the
  8.         // pulse width measuring loop and achieve finer resolution.  calling
  9.         // digitalRead() instead yields much coarser resolution.
  10.         uint8_t bit = digitalPinToBitMask(pin);
  11.         uint8_t port = digitalPinToPort(pin);
  12.         uint8_t stateMask = (state ? bit : 0);
  13.         unsigned long width = 0; // keep initialization out of time critical area
  14.        
  15.         // convert the timeout from microseconds to a number of times through
  16.         // the initial loop; it takes 16 clock cycles per iteration.
  17.         unsigned long numloops = 0;
  18.         unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
  19.        
  20.         // wait for any previous pulse to end
  21.         while ((*portInputRegister(port) & bit) == stateMask)
  22.                 if (numloops++ == maxloops)
  23.                         return 0;
  24.        
  25.         // wait for the pulse to start
  26.         while ((*portInputRegister(port) & bit) != stateMask)
  27.                 if (numloops++ == maxloops)
  28.                         return 0;
  29.        
  30.         // wait for the pulse to stop
  31.         while ((*portInputRegister(port) & bit) == stateMask) {
  32.                 if (numloops++ == maxloops)
  33.                         return 0;
  34.                 width++;
  35.         }

  36.         // convert the reading to microseconds. The loop has been determined
  37.         // to be 20 clock cycles long and have about 16 clocks between the edge
  38.         // and the start of the loop. There will be some error introduced by
  39.         // the interrupt handlers.
  40.         return clockCyclesToMicroseconds(width * 21 + 16);
  41. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-7-26 11:47:10 | 显示全部楼层
pathletboy 发表于 2014-7-26 10:07
最好的解释就是直接看代码。/* Measures the length (in microseconds) of a pulse on the pin; state is H ...

谢谢你的回复。但是光看这段代码,看不出pulseIn函数的工作模式,在板子上测试,得出运行时,PC指针没有在pulseIn内部停留,包括Mode相符时,计算出的脉冲时间也有一段延时。总体来着,用起来不是很方便。不知道大家用起来怎么样,有什么心得
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-18 12:01 , Processed in 0.034398 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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