Y SI / lib_ClockCounter
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lib_ClockCounter.h Source File

lib_ClockCounter.h

00001 /** Mbed Library Clock Counter
00002 * Hardware pulse counter with TIMER2 (CAP2.0 or CAP2.1) on Mbed LPC1768
00003 *
00004 * Counts signal transitions on p30(CAP2.0) or p29(CAP2.1).
00005 * Can detecte rising, falling or both signal edge.
00006 * Return the signal edge count during a periode in seconds.
00007 * Shannon's theorem say with an input signal frequency up to 48 MHz with 96 MHz CCLK.
00008 * Only tested with frequencys up to 20 MHz and it work.
00009 *
00010 * Example :
00011 * @code
00012 * #include "mbed.h"
00013 * #include "lib_ClockCounter.h"
00014 *
00015 * Serial pc(USBTX, USBRX);
00016 * ClockCounter Frequency;
00017 * 
00018 * int main()
00019 * {
00020 *     while(1) pc.printf("Frequency = %d Hz\r\n", Frequency.getCount());
00021 * }
00022 * @endcode
00023 */
00024 
00025 #ifndef DEF_LIB_CLOCKCOUNTER_H
00026 #define DEF_LIB_CLOCKCOUNTER_H
00027 
00028 #include "mbed.h"
00029 #ifndef TARGET_LPC1768
00030 #error unsupported target
00031 #else
00032 
00033 enum edgeDetection  { RISING = 1, FALLING = 2, BOTH = 3 };
00034 
00035 /** ClockCounter class
00036 *   Counts signal transitions on p30(CAP2.0) or p29(CAP2.1) for LPC1768 target.
00037 *   Can detecte rising, falling or both signal edge.
00038 *   Return the signal edge count during a period in seconds.
00039 *   In theory (Shannon's theorem) input signal frequency can up to 48 MHz with 96 MHz CCLK.
00040 *   But only tested with frequencys up to 20 MHz and it work.
00041  */
00042 class ClockCounter
00043 {
00044 public:
00045     /** Create an ClockCounter instance.
00046     *
00047     * Configure LPC1768 TIMER2 with capture input PIN_CAP2 and detecte transition EDGE.
00048     *
00049     * @param PIN_CAP2 can be p30(CAP2.0) or p29(CAP2.1), default is p30(CAP2.0).
00050     * @param EDGE can be RISING, FALLING, BOTH, default is RISING.
00051     */
00052     ClockCounter(PinName PIN_CAP2 = p30, edgeDetection EDGE = RISING);
00053 
00054     /** Select an ClockCounter instance
00055     *
00056     * Configure LPC1768 TIMER2 with capture input PIN_CAP2 and detecte transition EDGE.
00057     *
00058     * @param PIN_CAP2 can be p30(CAP2.0) or p29(CAP2.1).
00059     * @param EDGE can be RISING, FALLING, BOTH, default is RISING.
00060     */
00061     void setPin(PinName PIN_CAP2, edgeDetection EDGE = RISING);
00062 
00063     /** Start the signal transition count
00064     *
00065     */
00066     void startCount(void);
00067 
00068     /** Stop and Get the signal transition count
00069     *
00070      */
00071     int stopCount(void);
00072 
00073     /** Get the signal transition count during period
00074     *
00075     * @param period default is 1000000 microsecond.
00076      */
00077     int getCount(int period = 1000000);
00078 
00079     /** Get the selected input PIN_CAP2 on started signal transition count
00080     *
00081      */
00082     PinName getPin(void);
00083 private:
00084     int _count;
00085     PinName _selectPin;
00086 };
00087 #endif
00088 #endif