|
-
-
- #include "wiring_private.h"
- #include "pins_arduino.h"
- #include "core_pins.h"
-
-
- #define PULSEIN_CYCLES_PER_LOOP 21
- #define PULSEIN_CYCLES_LATENCY 11
-
-
- unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
- {
-
-
-
- uint8_t bit = digitalPinToBitMask(pin);
- volatile uint8_t *reg = portInputRegister(digitalPinToPort(pin));
- uint8_t stateMask = (state ? bit : 0);
- unsigned long width = 0;
-
-
-
- unsigned long numloops = 0;
-
- unsigned long maxloops = timeout * clockCyclesPerMicrosecond() / PULSEIN_CYCLES_PER_LOOP;
-
-
- while ((*reg & bit) == stateMask)
- if (numloops++ == maxloops)
- return 0;
-
-
- while ((*reg & bit) != stateMask)
- if (numloops++ == maxloops)
- return 0;
-
-
- while ((*reg & bit) == stateMask) {
- width++;
- if (numloops++ == maxloops)
- return 0;
- }
-
-
-
-
-
-
-
- return (width * PULSEIN_CYCLES_PER_LOOP + PULSEIN_CYCLES_LATENCY + (clockCyclesPerMicrosecond() / 2)) / clockCyclesPerMicrosecond();
- }
-
|