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 4:0eb01612bcb2, committed 2013-01-09
- Comitter:
- geotec
- Date:
- Wed Jan 09 09:57:21 2013 +0000
- Parent:
- 3:9e7474866e48
- Commit message:
- bugfix: capture interrupt enabled before start()
Changed in this revision
PulseCounter.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 9e7474866e48 -r 0eb01612bcb2 PulseCounter.cpp --- a/PulseCounter.cpp Tue Jan 08 14:25:02 2013 +0000 +++ b/PulseCounter.cpp Wed Jan 09 09:57:21 2013 +0000 @@ -50,7 +50,6 @@ // Set custom method to be called on Timer2 interrupt NVIC_DisableIRQ(TIMER2_IRQn); NVIC_SetVector(TIMER2_IRQn, (uint32_t)&_triggerInterrupt); - NVIC_EnableIRQ(TIMER2_IRQn); // LPC_TIM2->TCR = 0x2; // Timer2 count reset