sanguino/cores/arduino/wiring_pulse.c

Thu, 07 Jul 2016 12:23:34 +0200

author
mbayer
date
Thu, 07 Jul 2016 12:23:34 +0200
changeset 2
b373b0288715
permissions
-rw-r--r--

added missing sanguino files

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

mercurial