pulseIn: add alternative implementation based on micros()

This commit is contained in:
Martino Facchin 2015-05-29 14:50:42 +02:00
parent bb3963c0e9
commit a7d81d0b1c
2 changed files with 46 additions and 2 deletions

View File

@ -22,7 +22,11 @@
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
* to 3 minutes in length, but must be called at least a few dozen microseconds
* before the start of the pulse. */
* before the start of the pulse.
*
* ATTENTION:
* This function performs better with short pulses in noInterrupt() context
*/
uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
{
// cache the port and bit of the pin in order to speed up the
@ -47,3 +51,43 @@ uint32_t pulseIn( uint32_t pin, uint32_t state, uint32_t timeout )
else
return 0;
}
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
* or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
* to 3 minutes in length, but must be called at least a few dozen microseconds
* before the start of the pulse.
*
* ATTENTION:
* this function relies on micros() so cannot be used in noInterrupt() context
*/
uint32_t pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
{
// cache the port and bit of the pin in order to speed up the
// pulse width measuring loop and achieve finer resolution. calling
// digitalRead() instead yields much coarser resolution.
PinDescription p = g_APinDescription[pin];
uint32_t bit = p.ulPin;
uint32_t stateMask = state ? bit : 0;
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes 18 clock cycles per iteration.
unsigned long maxloops = microsecondsToClockCycles(timeout) / 10;
// wait for any previous pulse to end
while ((p.pPort->PIO_PDSR & bit) == stateMask)
if (--maxloops == 0)
return 0;
// wait for the pulse to start
while ((p.pPort->PIO_PDSR & bit) != stateMask)
if (--maxloops == 0)
return 0;
unsigned long start = micros();
// wait for the pulse to stop
while ((p.pPort->PIO_PDSR & bit) == stateMask) {
if (--maxloops == 0)
return 0;
}
return micros() - start;
}

View File

@ -31,7 +31,7 @@ unsigned long countPulseASM(const volatile uint32_t *port, uint32_t bit, uint32_
* before the start of the pulse.
*/
extern uint32_t pulseIn( uint32_t ulPin, uint32_t ulState, uint32_t ulTimeout = 1000000L ) ;
extern uint32_t pulseInLong( uint8_t pin, uint8_t state, unsigned long timeout = 1000000L ) ;
#ifdef __cplusplus
}