Zhenek
February 28, 2016, 1:39pm
1
Hello everyone. Even after reading a description of pulseIn , I still have a question. Let us have a code like that:
unsigned long pulseLength = 1000;
//
pulseTime = pulseIn (signalPin, HIGH, pulseLength);
//
What happens, if after pulseIn() is called, it detects a pulse starting within 1000 us (1 ms), but the duration of the pulse (when the port goes LOW again) is beyond 1 ms from starting point? What will function return, 1000 or 0? My guess is 0, because description reads:
the length of the pulse (in microseconds) or 0 if no pulse is completed before the timeout
But I'd like to be sure.
system
February 28, 2016, 2:53pm
2
The pulseIn() function will return 0 if the pulse does not start AND stop within the timeout period.
MarkT
February 28, 2016, 3:14pm
3
You can look the code for pulseIn if you want - this is open-source, you have the code on your machine
sitting there...
Zhenek
February 28, 2016, 8:40pm
4
Ok, I went to see the contents of wiring_pulse.c, and it shows
unsigned long width = countPulseASM(portInputRegister(port), bit, stateMask, maxloops);
// prevent clockCyclesToMicroseconds to return bogus values if countPulseASM timed out
if (width)
return clockCyclesToMicroseconds(width * 16 + 16);
else
return 0;
So then I've started to look to the code of countPulseASM, and found it to contain
while ((*port & bit) == stateMask) {
if (++width == maxloops)
return 0;
So it seems my first guess was right: if the pulse starts, but stays longer then timeout, the function returns 0.