123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- #include "wiring_private.h"
- #include "pins_arduino.h"
- 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;
-
-
-
- unsigned long numloops = 0;
- unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
-
-
- while ((*portInputRegister(port) & bit) == stateMask)
- if (numloops++ == maxloops)
- return 0;
-
-
- while ((*portInputRegister(port) & bit) != stateMask)
- if (numloops++ == maxloops)
- return 0;
-
-
- while ((*portInputRegister(port) & bit) == stateMask)
- width++;
-
-
-
-
- return clockCyclesToMicroseconds(width * 10 + 16);
- }
|