1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-16 11:21:18 +03:00

pulseIn() now times out while measuring the pulse, not just while waiting for it to start.

This commit is contained in:
David A. Mellis
2010-11-22 23:33:59 -05:00
parent 012b4b0f0f
commit 4dad13532f

View File

@ -55,12 +55,15 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
return 0; return 0;
// wait for the pulse to stop // wait for the pulse to stop
while ((*portInputRegister(port) & bit) == stateMask) while ((*portInputRegister(port) & bit) == stateMask) {
if (numloops++ == maxloops)
return 0;
width++; width++;
}
// convert the reading to microseconds. The loop has been determined // convert the reading to microseconds. The loop has been determined
// to be 10 clock cycles long and have about 16 clocks between the edge // to be 20 clock cycles long and have about 16 clocks between the edge
// and the start of the loop. There will be some error introduced by // and the start of the loop. There will be some error introduced by
// the interrupt handlers. // the interrupt handlers.
return clockCyclesToMicroseconds(width * 10 + 16); return clockCyclesToMicroseconds(width * 21 + 16);
} }