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.

Committer:
YSI
Date:
Wed Dec 02 15:48:45 2020 +0000
Revision:
5:b5e68cd91f09
Parent:
3:20dd01b1a1fd
add split start and stop count

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 2:cc5b11ec50ad 33 enum edgeDetection { RISING = 1, FALLING = 2, BOTH = 3 };
YSI 2:cc5b11ec50ad 34
YSI 2:cc5b11ec50ad 35 /** ClockCounter class
YSI 2:cc5b11ec50ad 36 * Counts signal transitions on p30(CAP2.0) or p29(CAP2.1) for LPC1768 target.
YSI 0:d3c997729db1 37 * Can detecte rising, falling or both signal edge.
YSI 0:d3c997729db1 38 * Return the signal edge count during a period in seconds.
YSI 0:d3c997729db1 39 * In theory (Shannon's theorem) input signal frequency can up to 48 MHz with 96 MHz CCLK.
YSI 0:d3c997729db1 40 * But only tested with frequencys up to 20 MHz and it work.
YSI 0:d3c997729db1 41 */
YSI 0:d3c997729db1 42 class ClockCounter
YSI 0:d3c997729db1 43 {
YSI 0:d3c997729db1 44 public:
YSI 0:d3c997729db1 45 /** Create an ClockCounter instance.
YSI 0:d3c997729db1 46 *
YSI 2:cc5b11ec50ad 47 * Configure LPC1768 TIMER2 with capture input PIN_CAP2 and detecte transition EDGE.
YSI 0:d3c997729db1 48 *
YSI 0:d3c997729db1 49 * @param PIN_CAP2 can be p30(CAP2.0) or p29(CAP2.1), default is p30(CAP2.0).
YSI 0:d3c997729db1 50 * @param EDGE can be RISING, FALLING, BOTH, default is RISING.
YSI 0:d3c997729db1 51 */
YSI 0:d3c997729db1 52 ClockCounter(PinName PIN_CAP2 = p30, edgeDetection EDGE = RISING);
YSI 0:d3c997729db1 53
YSI 5:b5e68cd91f09 54 /** Select an ClockCounter instance
YSI 5:b5e68cd91f09 55 *
YSI 5:b5e68cd91f09 56 * Configure LPC1768 TIMER2 with capture input PIN_CAP2 and detecte transition EDGE.
YSI 5:b5e68cd91f09 57 *
YSI 5:b5e68cd91f09 58 * @param PIN_CAP2 can be p30(CAP2.0) or p29(CAP2.1).
YSI 5:b5e68cd91f09 59 * @param EDGE can be RISING, FALLING, BOTH, default is RISING.
YSI 5:b5e68cd91f09 60 */
YSI 5:b5e68cd91f09 61 void setPin(PinName PIN_CAP2, edgeDetection EDGE = RISING);
YSI 5:b5e68cd91f09 62
YSI 5:b5e68cd91f09 63 /** Start the signal transition count
YSI 5:b5e68cd91f09 64 *
YSI 5:b5e68cd91f09 65 */
YSI 5:b5e68cd91f09 66 void startCount(void);
YSI 5:b5e68cd91f09 67
YSI 5:b5e68cd91f09 68 /** Stop and Get the signal transition count
YSI 5:b5e68cd91f09 69 *
YSI 5:b5e68cd91f09 70 */
YSI 5:b5e68cd91f09 71 int stopCount(void);
YSI 5:b5e68cd91f09 72
YSI 0:d3c997729db1 73 /** Get the signal transition count during period
YSI 0:d3c997729db1 74 *
YSI 3:20dd01b1a1fd 75 * @param period default is 1000000 microsecond.
YSI 0:d3c997729db1 76 */
YSI 3:20dd01b1a1fd 77 int getCount(int period = 1000000);
YSI 5:b5e68cd91f09 78
YSI 5:b5e68cd91f09 79 /** Get the selected input PIN_CAP2 on started signal transition count
YSI 5:b5e68cd91f09 80 *
YSI 5:b5e68cd91f09 81 */
YSI 5:b5e68cd91f09 82 PinName getPin(void);
YSI 5:b5e68cd91f09 83 private:
YSI 5:b5e68cd91f09 84 int _count;
YSI 5:b5e68cd91f09 85 PinName _selectPin;
YSI 0:d3c997729db1 86 };
YSI 0:d3c997729db1 87 #endif
YSI 0:d3c997729db1 88 #endif