Skip to content

Commit 7012587

Browse files
author
Martino Facchin
committed
pulseIn: add pulseInLong function using millis()
1 parent 6fd3a31 commit 7012587

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

hardware/arduino/avr/cores/arduino/Arduino.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ unsigned long micros(void);
134134
void delay(unsigned long);
135135
void delayMicroseconds(unsigned int us);
136136
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
137+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
137138

138139
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
139140
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
@@ -232,6 +233,7 @@ uint16_t makeWord(byte h, byte l);
232233
#define word(...) makeWord(__VA_ARGS__)
233234

234235
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
236+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
235237

236238
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
237239
void noTone(uint8_t _pin);

hardware/arduino/avr/cores/arduino/wiring_pulse.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,45 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
7979
width = countPulseASM(*portInputRegister(port), bit, maxloops, stateMask);
8080
return clockCyclesToMicroseconds(width * 11 + 16);
8181
}
82+
83+
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
84+
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
85+
* to 3 minutes in length, but must be called at least a few dozen microseconds
86+
* before the start of the pulse.
87+
*
88+
* ATTENTION:
89+
* this function relies on micros() so cannot be used in noInterrupt() context
90+
*/
91+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
92+
{
93+
// cache the port and bit of the pin in order to speed up the
94+
// pulse width measuring loop and achieve finer resolution. calling
95+
// digitalRead() instead yields much coarser resolution.
96+
uint8_t bit = digitalPinToBitMask(pin);
97+
uint8_t port = digitalPinToPort(pin);
98+
uint8_t stateMask = (state ? bit : 0);
99+
unsigned long width = 0; // keep initialization out of time critical area
100+
101+
// convert the timeout from microseconds to a number of times through
102+
// the initial loop; it takes 16 clock cycles per iteration.
103+
unsigned long numloops = 0;
104+
unsigned long maxloops = microsecondsToClockCycles(timeout);
105+
106+
// wait for any previous pulse to end
107+
while ((*portInputRegister(port) & bit) == stateMask)
108+
if (numloops++ == maxloops)
109+
return 0;
110+
111+
// wait for the pulse to start
112+
while ((*portInputRegister(port) & bit) != stateMask)
113+
if (numloops++ == maxloops)
114+
return 0;
115+
116+
unsigned long start = micros();
117+
// wait for the pulse to stop
118+
while ((*portInputRegister(port) & bit) == stateMask) {
119+
if (numloops++ == maxloops)
120+
return 0;
121+
}
122+
return micros() - start;
123+
}

0 commit comments

Comments
 (0)