Il 20/09/2015 02:29, igendel ha scritto:
> Here's an example from the code:
>
> |
> unsignedlongnumloops =0;
> unsignedlongmaxloops =microsecondsToClockCycles(timeout);
>
> // wait for any previous pulse to end
> while((*portInputRegister(port)&bit)==stateMask)
> if(numloops++==maxloops)
> return0;
> |
you're browsing a very old version, please look at the latest here:
https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring_pulse.c#L33
unsigned long pulseIn(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.
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
uint8_t stateMask = (state ? bit : 0);
// convert the timeout from microseconds to a number of times through
// the initial loop; it takes approximately 16 clock cycles per iteration
unsigned long maxloops = microsecondsToClockCycles(timeout)/16;
unsigned long width = countPulseASM(portInputRegister(port), bit,
stateMask, maxloops);
// prevent clockCyclesToMicroseconds to return bogus values if
countPulseASM timed out
if (width)
return clockCyclesToMicroseconds(width * 16 + 16);
else
return 0;
}
as you can see maxloops is now divided by 16 and the busy-loop is
written in asm to keep the loops duration constant and independent from
compiler optimizations.
C