Skip to content

Commit c4c8c95

Browse files
committed
Commented the pulseIn function.
1 parent 4f80fe1 commit c4c8c95

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

Diff for: targets/arduino/wiring.c

+20-3
Original file line numberDiff line numberDiff line change
@@ -381,21 +381,38 @@ unsigned long pulseIn(int pin, int state)
381381
}
382382
*/
383383

384+
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
385+
* or LOW, the type of pulse to measure. Works on pulses from 10 microseconds
386+
* to 3 minutes in length, but must be called at least N microseconds before
387+
* the start of the pulse. */
384388
unsigned long pulseIn(int pin, int state)
385389
{
386-
unsigned long width = 0;
390+
// cache the port and bit of the pin in order to speed up the
391+
// pulse width measuring loop and achieve finer resolution. calling
392+
// digitalWrite() instead yields much coarser resolution.
387393
int r = port_to_input[digitalPinToPort(pin)];
388394
int bit = digitalPinToBit(pin);
389395
int mask = 1 << bit;
396+
unsigned long width = 0;
390397

398+
// compute the desired bit pattern for the port reading (e.g. set or
399+
// clear the bit corresponding to the pin being read). the !!state
400+
// ensures that the function treats any non-zero value of state as HIGH.
391401
state = (!!state) << bit;
392402

403+
// wait for the pulse to start
393404
while ((_SFR_IO8(r) & mask) != state)
394405
;
395-
406+
407+
// wait for the pulse to stop
396408
while ((_SFR_IO8(r) & mask) == state)
397409
width++;
398-
410+
411+
// convert the reading to microseconds. the slower the CPU speed, the
412+
// proportionally fewer iterations of the loop will occur (e.g. a
413+
// 4 MHz clock will yield a width that is one-fourth of that read with
414+
// a 16 MHz clock). each loop was empirically determined to take
415+
// approximately 23/20 of a microsecond with a 16 MHz clock.
399416
return width * (16000000UL / F_CPU) * 20 / 23;
400417
}
401418

0 commit comments

Comments
 (0)