Library to simplify using capture pins on hardware.
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!
Diff: TimerCapture.h
- Revision:
- 0:94cfc6ceec2f
- Child:
- 1:660af7d3c416
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TimerCapture.h Thu May 21 08:04:03 2015 +0000 @@ -0,0 +1,63 @@ +#ifndef TIMERCAPTURE_H +#define TIMERCAPTURE_H + +#include "mbed.h" + +/** Directive that will toggle debug output via `printf`. Set to 1 to enable, 0 to disable. */ +#define DEBUG 0 + +/** A class that will start TIMER2 on the LPC1768 and configure it to use one of the two capture pins (p29 or p30). +* Capture pins allow the hardware to "timestamp" when a pin goes high or low, depending on the configuration. For more information, consult the +* [LPC1768 user manual](http://www.nxp.com/documents/user_manual/UM10360.pdf). +* +* @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. +* 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)! +*/ +class TimerCapture { + +public: + + /** Configures registers to use the given pin as a capture pin. + * Initializes an object that will configure and read from a capture pin. + * If the timer is not already running, it will get configured. + * + * @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). + * + * @warning This will cause a runtime error if: + * + The `capture_pin` is not set to an actual capture pin on the LPC1768. + * + The specified capture pin is already in use (i.e. already configured). + * + * @param pCapturePin The PinName intended to be used for the capture. + */ + TimerCapture(PinName pCapturePin); + + /** Get the time captured by the capture pin's register. + * @return Time in miliseconds since the timer got started. + */ + uint32_t getTime(); + + /** Starts the TIMER2 timer, and configures it if it's not already configured. */ + static void startTimer(); + + /** Checks if the TIMER2 timer is running. + * @return True if the timer is running, false otherwise. + */ + static bool isRunning(); + + /** Stops the TIMER2 timer. */ + static void stopTimer(); + + /** Resets the TIMER2 timer. + * @attention This will also clear both timer registers. You've been warned! + */ + static void resetTimer(); + +private: + PinName mCapturePin; + + static bool timerStarted; + + static void configureTimer(); + +}; +#endif \ No newline at end of file