My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiring_pulse.c 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. wiring_pulse.c - pulseIn() function
  3. Part of Arduino - http://www.arduino.cc/
  4. Copyright (c) 2005-2006 David A. Mellis
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General
  14. Public License along with this library; if not, write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
  18. */
  19. #include "wiring_private.h"
  20. #include "pins_arduino.h"
  21. /* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
  22. * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
  23. * to 3 minutes in length, but must be called at least a few dozen microseconds
  24. * before the start of the pulse. */
  25. unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
  26. {
  27. // cache the port and bit of the pin in order to speed up the
  28. // pulse width measuring loop and achieve finer resolution. calling
  29. // digitalRead() instead yields much coarser resolution.
  30. uint8_t bit = digitalPinToBitMask(pin);
  31. uint8_t port = digitalPinToPort(pin);
  32. uint8_t stateMask = (state ? bit : 0);
  33. unsigned long width = 0; // keep initialization out of time critical area
  34. // convert the timeout from microseconds to a number of times through
  35. // the initial loop; it takes 16 clock cycles per iteration.
  36. unsigned long numloops = 0;
  37. unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
  38. // wait for any previous pulse to end
  39. while ((*portInputRegister(port) & bit) == stateMask)
  40. if (numloops++ == maxloops)
  41. return 0;
  42. // wait for the pulse to start
  43. while ((*portInputRegister(port) & bit) != stateMask)
  44. if (numloops++ == maxloops)
  45. return 0;
  46. // wait for the pulse to stop
  47. while ((*portInputRegister(port) & bit) == stateMask)
  48. width++;
  49. // convert the reading to microseconds. The loop has been determined
  50. // to be 10 clock cycles long and have about 16 clocks between the edge
  51. // and the start of the loop. There will be some error introduced by
  52. // the interrupt handlers.
  53. return clockCyclesToMicroseconds(width * 10 + 16);
  54. }