Library to simplify using capture pins on hardware.

Dependents:   bluesync

When I first started experimenting with the notion of capture pins, I had no idea where to begin. Rather than write to registers in every single program, I decided to create a class that'd handle that heavy lifting for me!

Import library

Public Member Functions

TimerCapture (PinName pCapturePin)
Configures registers to use the given pin as a capture pin.
uint32_t getTime ()
Get the time captured by the capture pin's register.

Static Public Member Functions

static void startTimer ()
Starts the TIMER2 timer, and configures it if it's not already configured.
static bool isRunning ()
Checks if the TIMER2 timer is running.
static void stopTimer ()
Stops the TIMER2 timer.
static void resetTimer ()
Resets the TIMER2 timer.

This code uses TIMER2 on the LPC1768. I have no idea if it will work on other hardware, but I hope it helps someone else!

Committer:
dishbreak
Date:
Thu May 21 08:11:06 2015 +0000
Revision:
1:660af7d3c416
Parent:
0:94cfc6ceec2f
Child:
2:7c4ca945bfe1
updating documentation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dishbreak 0:94cfc6ceec2f 1 #ifndef TIMERCAPTURE_H
dishbreak 0:94cfc6ceec2f 2 #define TIMERCAPTURE_H
dishbreak 0:94cfc6ceec2f 3
dishbreak 0:94cfc6ceec2f 4 #include "mbed.h"
dishbreak 0:94cfc6ceec2f 5
dishbreak 0:94cfc6ceec2f 6 /** Directive that will toggle debug output via `printf`. Set to 1 to enable, 0 to disable. */
dishbreak 0:94cfc6ceec2f 7 #define DEBUG 0
dishbreak 0:94cfc6ceec2f 8
dishbreak 0:94cfc6ceec2f 9 /** A class that will start TIMER2 on the LPC1768 and configure it to use one of the two capture pins (p29 or p30).
dishbreak 0:94cfc6ceec2f 10 * Capture pins allow the hardware to "timestamp" when a pin goes high or low, depending on the configuration. For more information, consult the
dishbreak 0:94cfc6ceec2f 11 * [LPC1768 user manual](http://www.nxp.com/documents/user_manual/UM10360.pdf).
dishbreak 1:660af7d3c416 12 * # Example
dishbreak 1:660af7d3c416 13 * @code
dishbreak 1:660af7d3c416 14 * #include "mbed.h"
dishbreak 1:660af7d3c416 15 * #include "TimerCapture.h"
dishbreak 1:660af7d3c416 16 *
dishbreak 1:660af7d3c416 17 * Serial pc(USBTX, USBRX);
dishbreak 1:660af7d3c416 18 * TimerCapture * capture;
dishbreak 1:660af7d3c416 19 *
dishbreak 1:660af7d3c416 20 * void handleInput() {
dishbreak 1:660af7d3c416 21 * pc.getc(); // clear the interrupt handler.
dishbreak 1:660af7d3c416 22 * printf("Capture register reads: %d\r\n", capture->getTime());
dishbreak 1:660af7d3c416 23 * }
dishbreak 1:660af7d3c416 24 *
dishbreak 1:660af7d3c416 25 * int main() {
dishbreak 1:660af7d3c416 26 * pc.printf("Timer Capture Program Starting\r\n");
dishbreak 1:660af7d3c416 27 * TimerCapture::startTimer();
dishbreak 1:660af7d3c416 28 * capture = new TimerCapture(p29);
dishbreak 1:660af7d3c416 29 * pc.attach(&handleInput);
dishbreak 1:660af7d3c416 30 * }
dishbreak 1:660af7d3c416 31 * @endcode
dishbreak 0:94cfc6ceec2f 32 *
dishbreak 0:94cfc6ceec2f 33 * @attention Because this code operates directly on TIMER2, it has the potential to (a) mess with existing code or (b) cause errors when you instantiate too many objects.
dishbreak 0:94cfc6ceec2f 34 * Make sure to read this documentation thoroughly, else you may find yourself staring at [lights of death](https://developer.mbed.org/handbook/Debugging#runtime-errors)!
dishbreak 0:94cfc6ceec2f 35 */
dishbreak 0:94cfc6ceec2f 36 class TimerCapture {
dishbreak 0:94cfc6ceec2f 37
dishbreak 0:94cfc6ceec2f 38 public:
dishbreak 0:94cfc6ceec2f 39
dishbreak 0:94cfc6ceec2f 40 /** Configures registers to use the given pin as a capture pin.
dishbreak 0:94cfc6ceec2f 41 * Initializes an object that will configure and read from a capture pin.
dishbreak 0:94cfc6ceec2f 42 * If the timer is not already running, it will get configured.
dishbreak 0:94cfc6ceec2f 43 *
dishbreak 0:94cfc6ceec2f 44 * @attention *There are only two pins that will act as capture pins for TIMER2.* These are P0.4 (DIP p30) and P0.5(DIP p29).
dishbreak 0:94cfc6ceec2f 45 *
dishbreak 0:94cfc6ceec2f 46 * @warning This will cause a runtime error if:
dishbreak 0:94cfc6ceec2f 47 * + The `capture_pin` is not set to an actual capture pin on the LPC1768.
dishbreak 0:94cfc6ceec2f 48 * + The specified capture pin is already in use (i.e. already configured).
dishbreak 0:94cfc6ceec2f 49 *
dishbreak 0:94cfc6ceec2f 50 * @param pCapturePin The PinName intended to be used for the capture.
dishbreak 0:94cfc6ceec2f 51 */
dishbreak 0:94cfc6ceec2f 52 TimerCapture(PinName pCapturePin);
dishbreak 0:94cfc6ceec2f 53
dishbreak 0:94cfc6ceec2f 54 /** Get the time captured by the capture pin's register.
dishbreak 0:94cfc6ceec2f 55 * @return Time in miliseconds since the timer got started.
dishbreak 0:94cfc6ceec2f 56 */
dishbreak 0:94cfc6ceec2f 57 uint32_t getTime();
dishbreak 0:94cfc6ceec2f 58
dishbreak 0:94cfc6ceec2f 59 /** Starts the TIMER2 timer, and configures it if it's not already configured. */
dishbreak 0:94cfc6ceec2f 60 static void startTimer();
dishbreak 0:94cfc6ceec2f 61
dishbreak 0:94cfc6ceec2f 62 /** Checks if the TIMER2 timer is running.
dishbreak 0:94cfc6ceec2f 63 * @return True if the timer is running, false otherwise.
dishbreak 0:94cfc6ceec2f 64 */
dishbreak 0:94cfc6ceec2f 65 static bool isRunning();
dishbreak 0:94cfc6ceec2f 66
dishbreak 0:94cfc6ceec2f 67 /** Stops the TIMER2 timer. */
dishbreak 0:94cfc6ceec2f 68 static void stopTimer();
dishbreak 0:94cfc6ceec2f 69
dishbreak 0:94cfc6ceec2f 70 /** Resets the TIMER2 timer.
dishbreak 0:94cfc6ceec2f 71 * @attention This will also clear both timer registers. You've been warned!
dishbreak 0:94cfc6ceec2f 72 */
dishbreak 0:94cfc6ceec2f 73 static void resetTimer();
dishbreak 0:94cfc6ceec2f 74
dishbreak 0:94cfc6ceec2f 75 private:
dishbreak 0:94cfc6ceec2f 76 PinName mCapturePin;
dishbreak 0:94cfc6ceec2f 77
dishbreak 0:94cfc6ceec2f 78 static bool timerStarted;
dishbreak 0:94cfc6ceec2f 79
dishbreak 0:94cfc6ceec2f 80 static void configureTimer();
dishbreak 0:94cfc6ceec2f 81
dishbreak 0:94cfc6ceec2f 82 };
dishbreak 0:94cfc6ceec2f 83 #endif