Counts pulses on pin p29. Pulse frequency can be up to 48 MHz.

Example, counting 48MHz-pulses on pin p29 generated by pin p22 with PwmOscillator (https://mbed.org/users/geotec/code/PwmOscillator/). Pins p22 & p29 must be connected. Count is triggered when pin p30 = high (I connected oscillator with 9kHz that triggered periodically).

#include "mbed.h"
#include "PwmOscillator.h"
#include "PulseCounter.h"

Serial usbPC(USBTX, USBRX);   // sends log messages via USB to PC terminal
PwmOscillator pulseGenerator; // generates pulses to be count (48 MHz) on pin p22. Connect this pin to p29 (counter input).
PulseCounter pulseCounter;    // counts the pulses on pin p29 between trigger events (= rising edges) on pin p30.

int main() 
{
    usbPC.printf("---> start <---\n");
    pulseGenerator.initWithFrequency(48000000);
    pulseCounter.init();
    pulseGenerator.start();
        
    pulseCounter.start();   
    wait(1);        // waiting 1 second for trigger events (pin30: rising edge). Count pulses between trigger events.
    pulseCounter.stop();
                        
    pulseGenerator.stop();
        
    // read & print pulseCounter results
    uint32_t pulseCounterResults[20];
    pulseCounter.counterArray(&pulseCounterResults[0],sizeof pulseCounterResults / sizeof (uint32_t));
        
    for(int i = 0; i < (sizeof pulseCounterResults / sizeof (uint32_t)); i++) {
        usbPC.printf("counter of trigger event %i = %u\n",i,pulseCounterResults[i]);
    }
    //
        
    usbPC.printf("   finished.\n");
}
Revision:
0:157c2fddaa68
Child:
1:83149916f8a9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PulseCounter.h	Sat Dec 15 14:26:47 2012 +0000
@@ -0,0 +1,77 @@
+#ifndef MBED_PULSECOUNTER_H
+#define MBED_PULSECOUNTER_H
+
+#include "mbed.h"
+
+const int _pulseCountArrayLength = 20;
+
+/** Counts pulses on pin29 coming in with a frequency up to 48 MHz. The counter is read (and reset to 0) by rising edge (trigger event) on pin30.
+ *  These events can be triggered at a rate up to 9kHz. The counter results are stored in a "circular array" of 20 elements.
+ *  ("circular array" = array pointer increments each time a new value is stored into the array; if pointer reaches last element, it is redirected to first element)
+ *   */
+class PulseCounter
+{
+    public:
+
+    /** Initializer for PulseCounter instance
+     */
+    void init();
+ 
+    /** Start counter
+     */
+    void start();
+
+    /** Stop counter
+     */
+    void stop();
+    
+    /** Read pulse count results into external array. Do not call this method before you called method stop() - otherwise it will return zeros.
+     * @param counterArray          = Pointer to array in which the pulse count results should be read into. 
+     * @param counterArrayLength    = Size of counterArray. Should be 20 or less. If more than 20, remaining elements will be filled with zeros. 
+     * @code    #include "mbed.h"
+                #include "PwmOscillator.h"
+                #include "PulseCounter.h"
+
+                Serial usbPC(USBTX, USBRX);                 // sends log messages via USB to PC terminal
+                PwmOscillator pulseGenerator;               // generates pulses to be count (48 MHz) on pin p22. Connect this pin to p29 (counter input).
+                PulseCounter pulseCounter;                  // counts the pulses on pin p29 between trigger events (= rising edges) on pin p30.
+
+                int main() 
+                {
+                    usbPC.printf("---> start <---\n");
+                    pulseGenerator.initWithFrequency(48000000);
+                    pulseCounter.init();
+                    pulseGenerator.start();
+    
+                    pulseCounter.start();   
+                    wait(1);                                // waiting 1 second for trigger events (pin30: rising edge). Count pulses between trigger events.
+                    pulseCounter.stop();
+                    
+                    pulseGenerator.stop();
+    
+                    // read & print pulseCounter results
+                    int pulseCounterResults[20];
+                    pulseCounter.counterArray(&pulseCounterResults[0],sizeof pulseCounterResults / sizeof (int));
+    
+                    for(int i = 0; i < (sizeof pulseCounterResults / sizeof (int)); i++) {
+                        usbPC.printf("counter of trigger event %i = %i\n",i,pulseCounterResults[i]);
+                    }
+                    //
+    
+                    usbPC.printf("   finished.\n");
+                }
+       * @endcode  */
+    void counterArray(int* counterArray, int counterArrayLength);
+            
+private:
+    
+    static void _triggerInterrupt();
+    static PulseCounter *instance;
+    bool _hasFinishedCounting;
+    
+    volatile int _triggerCounter;
+    volatile uint32_t *_arrayPointer;
+    volatile uint32_t _pulseCountArray[_pulseCountArrayLength + 1];
+};
+
+#endif