Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: lib_ClockCounter.h
- Revision:
- 0:d3c997729db1
- Child:
- 2:cc5b11ec50ad
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib_ClockCounter.h Thu Nov 21 09:17:25 2019 +0000
@@ -0,0 +1,63 @@
+/** Mbed Library Clock Counter
+* Hardware pulse counter with TIMER2 (CAP2.0 or CAP2.1) on Mbed LPC1768
+*
+* Counts signal transitions on p30(CAP2.0) or p29(CAP2.1).
+* Can detecte rising, falling or both signal edge.
+* Return the signal edge count during a periode in seconds.
+* Shannon's theorem say with an input signal frequency up to 48 MHz with 96 MHz CCLK.
+* Only tested with frequencys up to 20 MHz and it work.
+*
+* Example :
+* @code
+* #include "mbed.h"
+* #include "lib_ClockCounter.h"
+*
+* Serial pc(USBTX, USBRX);
+* ClockCounter Frequency;
+*
+* int main()
+* {
+* while(1) pc.printf("Frequency = %d Hz\r\n", Frequency.getCount());
+* }
+* @endcode
+*/
+
+#ifndef DEF_LIB_CLOCKCOUNTER_H
+#define DEF_LIB_CLOCKCOUNTER_H
+
+#include "mbed.h"
+#ifndef TARGET_LPC1768
+#error unsupported target
+#else
+
+/** Counts signal transitions on p30(CAP2.0) or p29(CAP2.1) for LPC1768 target.
+* Can detecte rising, falling or both signal edge.
+* Return the signal edge count during a period in seconds.
+* In theory (Shannon's theorem) input signal frequency can up to 48 MHz with 96 MHz CCLK.
+* But only tested with frequencys up to 20 MHz and it work.
+*/
+
+enum edgeDetection { RISING = 1, FALLING = 2, BOTH = 3 };
+
+/** ClockCounter class
+ */
+class ClockCounter
+{
+public:
+ /** Create an ClockCounter instance.
+ *
+ * Configure LPC1768 TIMER2 with capture input @param PIN_CAP2 and detecte transition @param EDGE.
+ *
+ * @param PIN_CAP2 can be p30(CAP2.0) or p29(CAP2.1), default is p30(CAP2.0).
+ * @param EDGE can be RISING, FALLING, BOTH, default is RISING.
+ */
+ ClockCounter(PinName PIN_CAP2 = p30, edgeDetection EDGE = RISING);
+
+ /** Get the signal transition count during period
+ *
+ * @param period default is 1.0 second.
+ */
+ int getCount(float period = 1.0);
+};
+#endif
+#endif
\ No newline at end of file