unsignedlongpulseIn(uint8_tpin,uint8_tstate,unsignedlongtimeout){// 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_tbit=digitalPinToBitMask(pin);uint8_tport=digitalPinToPort(pin);uint8_tstateMask=(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
unsignedlongmaxloops=microsecondsToClockCycles(timeout)/16;unsignedlongwidth=countPulseASM(portInputRegister(port),bit,stateMask,maxloops);// prevent clockCyclesToMicroseconds to return bogus values if countPulseASM timed out
if(width)returnclockCyclesToMicroseconds(width*16+16);elsereturn0;}
// 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_tbit=digitalPinToBitMask(pin);uint8_tport=digitalPinToPort(pin);
stateMaskとmaxloopsを設定します。
8
9
10
11
12
uint8_tstateMask=(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
unsignedlongmaxloops=microsecondsToClockCycles(timeout)/16;
// prevent clockCyclesToMicroseconds to return bogus values if countPulseASM timed out
if(width)returnclockCyclesToMicroseconds(width*16+16);elsereturn0;}
/*
Copyright (c) 2016 garretlab.
Added source code explanation by garretlab.
*//*
wiring_pulse.c - pulseIn() function
Part of Arduino - http://www.arduino.cc/
Copyright (c) 2005-2006 David A. Mellis
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/