Library to simplify using capture pins on hardware.

Dependents:   bluesync

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TimerCapture.h Source File

TimerCapture.h

00001 #ifndef TIMERCAPTURE_H
00002 #define TIMERCAPTURE_H
00003 
00004 #include "mbed.h"
00005 
00006 /** Directive that will toggle debug output via `printf`. Set to 1 to enable, 0 to disable. */
00007 #define DEBUG 0
00008 
00009 /** A class that will start TIMER2 on the LPC1768 and configure it to use one of the two capture pins (p29 or p30). 
00010 * Capture pins allow the hardware to "timestamp" when a pin goes high or low, depending on the configuration. For more information, consult the 
00011 * <a href="http://www.nxp.com/documents/user_manual/UM10360.pdf">LPC1768 user manual</a>.
00012 * <h3>Example</h3>
00013 * The following example will set up a capture pin, and will print out the timestamp from the capture register whenever a keypress is detected on the USB serial interface.
00014 * @code
00015 * #include "mbed.h"
00016 * #include "TimerCapture.h"
00017 * 
00018 * Serial pc(USBTX, USBRX);
00019 * TimerCapture * capture;
00020 * 
00021 * void handleInput() {
00022 *     pc.getc(); // clear the interrupt handler.
00023 *     printf("Capture register reads: %d\r\n", capture->getTime());
00024 * }
00025 * 
00026 * int main() {
00027 *     pc.printf("Timer Capture Program Starting\r\n");
00028 *     TimerCapture::startTimer();
00029 *     capture = new TimerCapture(p29);
00030 *     pc.attach(&handleInput);
00031 * }
00032 * @endcode
00033 *
00034 * @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. 
00035 * Make sure to read this documentation thoroughly, else you may find yourself staring at <a href="https://developer.mbed.org/handbook/Debugging#runtime-errors">lights of death</a>!
00036 */
00037 class TimerCapture {
00038 
00039 public:
00040 
00041     /** Configures registers to use the given pin as a capture pin.
00042     * Initializes an object that will configure and read from a capture pin. 
00043     * If the timer is not already running, it will get configured. 
00044     *
00045     * @attention <b>There are only two pins that will act as capture pins for TIMER2.</b> These are P0.4 (DIP p30) and P0.5(DIP p29). 
00046     *
00047     * @warning This will cause a runtime error if: 
00048     *<ul>
00049     * <li>pCapturePin is not set to an actual capture pin on the LPC1768.
00050     * <li>The specified capture pin is already in use (i.e. already configured). 
00051     * <li>The timer hasn't yet been started (i.e. peripheral is off)
00052     *</ul>
00053     * @param pCapturePin The PinName intended to be used for the capture.
00054     */
00055     TimerCapture(PinName pCapturePin); 
00056     
00057     /** Get the time captured by the capture pin's register. 
00058     * @return Time in miliseconds since the timer got started.
00059     */
00060     uint32_t getTime();
00061 
00062     /** Starts the TIMER2 timer, and configures it if it's not already configured. */
00063     static void startTimer();
00064     
00065     /** Checks if the TIMER2 timer is running. 
00066     * @return True if the timer is running, false otherwise.
00067     */
00068     static bool isRunning();
00069     
00070     /** Stops the TIMER2 timer. */
00071     static void stopTimer();
00072     
00073     /** Resets the TIMER2 timer. 
00074     * @attention This will also clear both timer registers. You've been warned!
00075     */
00076     static void resetTimer();
00077     
00078 private:
00079     PinName mCapturePin;
00080 
00081     static bool timerStarted;
00082     
00083     static void configureTimer();
00084     
00085 };
00086 #endif