@@ -49,4 +49,46 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
4949
5050 width = countPulseASM (portInputRegister (port ), bit , stateMask , maxloops );
5151 return clockCyclesToMicroseconds (width * 16 + 16 );
52- }
52+ }
53+
54+ /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
55+ * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
56+ * to 3 minutes in length, but must be called at least a few dozen microseconds
57+ * before the start of the pulse.
58+ *
59+ * ATTENTION:
60+ * this function relies on micros() so cannot be used in noInterrupt() context
61+ */
62+ unsigned long pulseInLong (uint8_t pin , uint8_t state , unsigned long timeout )
63+ {
64+ // cache the port and bit of the pin in order to speed up the
65+ // pulse width measuring loop and achieve finer resolution. calling
66+ // digitalRead() instead yields much coarser resolution.
67+ uint8_t bit = digitalPinToBitMask (pin );
68+ uint8_t port = digitalPinToPort (pin );
69+ uint8_t stateMask = (state ? bit : 0 );
70+ unsigned long width = 0 ; // keep initialization out of time critical area
71+
72+ // convert the timeout from microseconds to a number of times through
73+ // the initial loop; it takes 16 clock cycles per iteration.
74+ unsigned long numloops = 0 ;
75+ unsigned long maxloops = microsecondsToClockCycles (timeout );
76+
77+ // wait for any previous pulse to end
78+ while ((* portInputRegister (port ) & bit ) == stateMask )
79+ if (numloops ++ == maxloops )
80+ return 0 ;
81+
82+ // wait for the pulse to start
83+ while ((* portInputRegister (port ) & bit ) != stateMask )
84+ if (numloops ++ == maxloops )
85+ return 0 ;
86+
87+ unsigned long start = micros ();
88+ // wait for the pulse to stop
89+ while ((* portInputRegister (port ) & bit ) == stateMask ) {
90+ if (numloops ++ == maxloops )
91+ return 0 ;
92+ }
93+ return micros () - start ;
94+ }
0 commit comments