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:04:03 2015 +0000
Revision:
0:94cfc6ceec2f
Child:
1:660af7d3c416
Initial commit of TimerCapture code.

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 0:94cfc6ceec2f 12 *
dishbreak 0:94cfc6ceec2f 13 * @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 14 * 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 15 */
dishbreak 0:94cfc6ceec2f 16 class TimerCapture {
dishbreak 0:94cfc6ceec2f 17
dishbreak 0:94cfc6ceec2f 18 public:
dishbreak 0:94cfc6ceec2f 19
dishbreak 0:94cfc6ceec2f 20 /** Configures registers to use the given pin as a capture pin.
dishbreak 0:94cfc6ceec2f 21 * Initializes an object that will configure and read from a capture pin.
dishbreak 0:94cfc6ceec2f 22 * If the timer is not already running, it will get configured.
dishbreak 0:94cfc6ceec2f 23 *
dishbreak 0:94cfc6ceec2f 24 * @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 25 *
dishbreak 0:94cfc6ceec2f 26 * @warning This will cause a runtime error if:
dishbreak 0:94cfc6ceec2f 27 * + The `capture_pin` is not set to an actual capture pin on the LPC1768.
dishbreak 0:94cfc6ceec2f 28 * + The specified capture pin is already in use (i.e. already configured).
dishbreak 0:94cfc6ceec2f 29 *
dishbreak 0:94cfc6ceec2f 30 * @param pCapturePin The PinName intended to be used for the capture.
dishbreak 0:94cfc6ceec2f 31 */
dishbreak 0:94cfc6ceec2f 32 TimerCapture(PinName pCapturePin);
dishbreak 0:94cfc6ceec2f 33
dishbreak 0:94cfc6ceec2f 34 /** Get the time captured by the capture pin's register.
dishbreak 0:94cfc6ceec2f 35 * @return Time in miliseconds since the timer got started.
dishbreak 0:94cfc6ceec2f 36 */
dishbreak 0:94cfc6ceec2f 37 uint32_t getTime();
dishbreak 0:94cfc6ceec2f 38
dishbreak 0:94cfc6ceec2f 39 /** Starts the TIMER2 timer, and configures it if it's not already configured. */
dishbreak 0:94cfc6ceec2f 40 static void startTimer();
dishbreak 0:94cfc6ceec2f 41
dishbreak 0:94cfc6ceec2f 42 /** Checks if the TIMER2 timer is running.
dishbreak 0:94cfc6ceec2f 43 * @return True if the timer is running, false otherwise.
dishbreak 0:94cfc6ceec2f 44 */
dishbreak 0:94cfc6ceec2f 45 static bool isRunning();
dishbreak 0:94cfc6ceec2f 46
dishbreak 0:94cfc6ceec2f 47 /** Stops the TIMER2 timer. */
dishbreak 0:94cfc6ceec2f 48 static void stopTimer();
dishbreak 0:94cfc6ceec2f 49
dishbreak 0:94cfc6ceec2f 50 /** Resets the TIMER2 timer.
dishbreak 0:94cfc6ceec2f 51 * @attention This will also clear both timer registers. You've been warned!
dishbreak 0:94cfc6ceec2f 52 */
dishbreak 0:94cfc6ceec2f 53 static void resetTimer();
dishbreak 0:94cfc6ceec2f 54
dishbreak 0:94cfc6ceec2f 55 private:
dishbreak 0:94cfc6ceec2f 56 PinName mCapturePin;
dishbreak 0:94cfc6ceec2f 57
dishbreak 0:94cfc6ceec2f 58 static bool timerStarted;
dishbreak 0:94cfc6ceec2f 59
dishbreak 0:94cfc6ceec2f 60 static void configureTimer();
dishbreak 0:94cfc6ceec2f 61
dishbreak 0:94cfc6ceec2f 62 };
dishbreak 0:94cfc6ceec2f 63 #endif