Y SI / lib_ClockCounter
Committer:
YSI
Date:
Thu Nov 21 09:17:25 2019 +0000
Revision:
0:d3c997729db1
Child:
2:cc5b11ec50ad
lib_ClockCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:d3c997729db1 1 /** Mbed Library Clock Counter
YSI 0:d3c997729db1 2 * Hardware pulse counter with TIMER2 (CAP2.0 or CAP2.1) on Mbed LPC1768
YSI 0:d3c997729db1 3 *
YSI 0:d3c997729db1 4 * Counts signal transitions on p30(CAP2.0) or p29(CAP2.1).
YSI 0:d3c997729db1 5 * Can detecte rising, falling or both signal edge.
YSI 0:d3c997729db1 6 * Return the signal edge count during a periode in seconds.
YSI 0:d3c997729db1 7 * Shannon's theorem say with an input signal frequency up to 48 MHz with 96 MHz CCLK.
YSI 0:d3c997729db1 8 * Only tested with frequencys up to 20 MHz and it work.
YSI 0:d3c997729db1 9 *
YSI 0:d3c997729db1 10 * Example :
YSI 0:d3c997729db1 11 * @code
YSI 0:d3c997729db1 12 * #include "mbed.h"
YSI 0:d3c997729db1 13 * #include "lib_ClockCounter.h"
YSI 0:d3c997729db1 14 *
YSI 0:d3c997729db1 15 * Serial pc(USBTX, USBRX);
YSI 0:d3c997729db1 16 * ClockCounter Frequency;
YSI 0:d3c997729db1 17 *
YSI 0:d3c997729db1 18 * int main()
YSI 0:d3c997729db1 19 * {
YSI 0:d3c997729db1 20 * while(1) pc.printf("Frequency = %d Hz\r\n", Frequency.getCount());
YSI 0:d3c997729db1 21 * }
YSI 0:d3c997729db1 22 * @endcode
YSI 0:d3c997729db1 23 */
YSI 0:d3c997729db1 24
YSI 0:d3c997729db1 25 #ifndef DEF_LIB_CLOCKCOUNTER_H
YSI 0:d3c997729db1 26 #define DEF_LIB_CLOCKCOUNTER_H
YSI 0:d3c997729db1 27
YSI 0:d3c997729db1 28 #include "mbed.h"
YSI 0:d3c997729db1 29 #ifndef TARGET_LPC1768
YSI 0:d3c997729db1 30 #error unsupported target
YSI 0:d3c997729db1 31 #else
YSI 0:d3c997729db1 32
YSI 0:d3c997729db1 33 /** Counts signal transitions on p30(CAP2.0) or p29(CAP2.1) for LPC1768 target.
YSI 0:d3c997729db1 34 * Can detecte rising, falling or both signal edge.
YSI 0:d3c997729db1 35 * Return the signal edge count during a period in seconds.
YSI 0:d3c997729db1 36 * In theory (Shannon's theorem) input signal frequency can up to 48 MHz with 96 MHz CCLK.
YSI 0:d3c997729db1 37 * But only tested with frequencys up to 20 MHz and it work.
YSI 0:d3c997729db1 38 */
YSI 0:d3c997729db1 39
YSI 0:d3c997729db1 40 enum edgeDetection { RISING = 1, FALLING = 2, BOTH = 3 };
YSI 0:d3c997729db1 41
YSI 0:d3c997729db1 42 /** ClockCounter class
YSI 0:d3c997729db1 43 */
YSI 0:d3c997729db1 44 class ClockCounter
YSI 0:d3c997729db1 45 {
YSI 0:d3c997729db1 46 public:
YSI 0:d3c997729db1 47 /** Create an ClockCounter instance.
YSI 0:d3c997729db1 48 *
YSI 0:d3c997729db1 49 * Configure LPC1768 TIMER2 with capture input @param PIN_CAP2 and detecte transition @param EDGE.
YSI 0:d3c997729db1 50 *
YSI 0:d3c997729db1 51 * @param PIN_CAP2 can be p30(CAP2.0) or p29(CAP2.1), default is p30(CAP2.0).
YSI 0:d3c997729db1 52 * @param EDGE can be RISING, FALLING, BOTH, default is RISING.
YSI 0:d3c997729db1 53 */
YSI 0:d3c997729db1 54 ClockCounter(PinName PIN_CAP2 = p30, edgeDetection EDGE = RISING);
YSI 0:d3c997729db1 55
YSI 0:d3c997729db1 56 /** Get the signal transition count during period
YSI 0:d3c997729db1 57 *
YSI 0:d3c997729db1 58 * @param period default is 1.0 second.
YSI 0:d3c997729db1 59 */
YSI 0:d3c997729db1 60 int getCount(float period = 1.0);
YSI 0:d3c997729db1 61 };
YSI 0:d3c997729db1 62 #endif
YSI 0:d3c997729db1 63 #endif