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"); }
PulseCounter.h@1:83149916f8a9, 2012-12-15 (annotated)
- Committer:
- geotec
- Date:
- Sat Dec 15 14:29:08 2012 +0000
- Revision:
- 1:83149916f8a9
- Parent:
- 0:157c2fddaa68
- Child:
- 3:9e7474866e48
MIT license added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geotec | 1:83149916f8a9 | 1 | /* Copyright (c) 2012 Christian Buntebarth, MIT License |
geotec | 1:83149916f8a9 | 2 | * |
geotec | 1:83149916f8a9 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
geotec | 1:83149916f8a9 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
geotec | 1:83149916f8a9 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
geotec | 1:83149916f8a9 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
geotec | 1:83149916f8a9 | 7 | * furnished to do so, subject to the following conditions: |
geotec | 1:83149916f8a9 | 8 | * |
geotec | 1:83149916f8a9 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
geotec | 1:83149916f8a9 | 10 | * substantial portions of the Software. |
geotec | 1:83149916f8a9 | 11 | * |
geotec | 1:83149916f8a9 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
geotec | 1:83149916f8a9 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
geotec | 1:83149916f8a9 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
geotec | 1:83149916f8a9 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
geotec | 1:83149916f8a9 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
geotec | 1:83149916f8a9 | 17 | */ |
geotec | 1:83149916f8a9 | 18 | |
geotec | 0:157c2fddaa68 | 19 | #ifndef MBED_PULSECOUNTER_H |
geotec | 0:157c2fddaa68 | 20 | #define MBED_PULSECOUNTER_H |
geotec | 0:157c2fddaa68 | 21 | |
geotec | 0:157c2fddaa68 | 22 | #include "mbed.h" |
geotec | 0:157c2fddaa68 | 23 | |
geotec | 0:157c2fddaa68 | 24 | const int _pulseCountArrayLength = 20; |
geotec | 0:157c2fddaa68 | 25 | |
geotec | 0:157c2fddaa68 | 26 | /** 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. |
geotec | 0:157c2fddaa68 | 27 | * These events can be triggered at a rate up to 9kHz. The counter results are stored in a "circular array" of 20 elements. |
geotec | 0:157c2fddaa68 | 28 | * ("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) |
geotec | 0:157c2fddaa68 | 29 | * */ |
geotec | 0:157c2fddaa68 | 30 | class PulseCounter |
geotec | 0:157c2fddaa68 | 31 | { |
geotec | 0:157c2fddaa68 | 32 | public: |
geotec | 0:157c2fddaa68 | 33 | |
geotec | 0:157c2fddaa68 | 34 | /** Initializer for PulseCounter instance |
geotec | 0:157c2fddaa68 | 35 | */ |
geotec | 0:157c2fddaa68 | 36 | void init(); |
geotec | 0:157c2fddaa68 | 37 | |
geotec | 0:157c2fddaa68 | 38 | /** Start counter |
geotec | 0:157c2fddaa68 | 39 | */ |
geotec | 0:157c2fddaa68 | 40 | void start(); |
geotec | 0:157c2fddaa68 | 41 | |
geotec | 0:157c2fddaa68 | 42 | /** Stop counter |
geotec | 0:157c2fddaa68 | 43 | */ |
geotec | 0:157c2fddaa68 | 44 | void stop(); |
geotec | 0:157c2fddaa68 | 45 | |
geotec | 0:157c2fddaa68 | 46 | /** Read pulse count results into external array. Do not call this method before you called method stop() - otherwise it will return zeros. |
geotec | 0:157c2fddaa68 | 47 | * @param counterArray = Pointer to array in which the pulse count results should be read into. |
geotec | 0:157c2fddaa68 | 48 | * @param counterArrayLength = Size of counterArray. Should be 20 or less. If more than 20, remaining elements will be filled with zeros. |
geotec | 0:157c2fddaa68 | 49 | * @code #include "mbed.h" |
geotec | 0:157c2fddaa68 | 50 | #include "PwmOscillator.h" |
geotec | 0:157c2fddaa68 | 51 | #include "PulseCounter.h" |
geotec | 0:157c2fddaa68 | 52 | |
geotec | 0:157c2fddaa68 | 53 | Serial usbPC(USBTX, USBRX); // sends log messages via USB to PC terminal |
geotec | 0:157c2fddaa68 | 54 | PwmOscillator pulseGenerator; // generates pulses to be count (48 MHz) on pin p22. Connect this pin to p29 (counter input). |
geotec | 0:157c2fddaa68 | 55 | PulseCounter pulseCounter; // counts the pulses on pin p29 between trigger events (= rising edges) on pin p30. |
geotec | 0:157c2fddaa68 | 56 | |
geotec | 0:157c2fddaa68 | 57 | int main() |
geotec | 0:157c2fddaa68 | 58 | { |
geotec | 0:157c2fddaa68 | 59 | usbPC.printf("---> start <---\n"); |
geotec | 0:157c2fddaa68 | 60 | pulseGenerator.initWithFrequency(48000000); |
geotec | 0:157c2fddaa68 | 61 | pulseCounter.init(); |
geotec | 0:157c2fddaa68 | 62 | pulseGenerator.start(); |
geotec | 0:157c2fddaa68 | 63 | |
geotec | 0:157c2fddaa68 | 64 | pulseCounter.start(); |
geotec | 0:157c2fddaa68 | 65 | wait(1); // waiting 1 second for trigger events (pin30: rising edge). Count pulses between trigger events. |
geotec | 0:157c2fddaa68 | 66 | pulseCounter.stop(); |
geotec | 0:157c2fddaa68 | 67 | |
geotec | 0:157c2fddaa68 | 68 | pulseGenerator.stop(); |
geotec | 0:157c2fddaa68 | 69 | |
geotec | 0:157c2fddaa68 | 70 | // read & print pulseCounter results |
geotec | 0:157c2fddaa68 | 71 | int pulseCounterResults[20]; |
geotec | 0:157c2fddaa68 | 72 | pulseCounter.counterArray(&pulseCounterResults[0],sizeof pulseCounterResults / sizeof (int)); |
geotec | 0:157c2fddaa68 | 73 | |
geotec | 0:157c2fddaa68 | 74 | for(int i = 0; i < (sizeof pulseCounterResults / sizeof (int)); i++) { |
geotec | 0:157c2fddaa68 | 75 | usbPC.printf("counter of trigger event %i = %i\n",i,pulseCounterResults[i]); |
geotec | 0:157c2fddaa68 | 76 | } |
geotec | 0:157c2fddaa68 | 77 | // |
geotec | 0:157c2fddaa68 | 78 | |
geotec | 0:157c2fddaa68 | 79 | usbPC.printf(" finished.\n"); |
geotec | 0:157c2fddaa68 | 80 | } |
geotec | 0:157c2fddaa68 | 81 | * @endcode */ |
geotec | 0:157c2fddaa68 | 82 | void counterArray(int* counterArray, int counterArrayLength); |
geotec | 0:157c2fddaa68 | 83 | |
geotec | 0:157c2fddaa68 | 84 | private: |
geotec | 0:157c2fddaa68 | 85 | |
geotec | 0:157c2fddaa68 | 86 | static void _triggerInterrupt(); |
geotec | 0:157c2fddaa68 | 87 | static PulseCounter *instance; |
geotec | 0:157c2fddaa68 | 88 | bool _hasFinishedCounting; |
geotec | 0:157c2fddaa68 | 89 | |
geotec | 0:157c2fddaa68 | 90 | volatile int _triggerCounter; |
geotec | 0:157c2fddaa68 | 91 | volatile uint32_t *_arrayPointer; |
geotec | 0:157c2fddaa68 | 92 | volatile uint32_t _pulseCountArray[_pulseCountArrayLength + 1]; |
geotec | 0:157c2fddaa68 | 93 | }; |
geotec | 0:157c2fddaa68 | 94 | |
geotec | 0:157c2fddaa68 | 95 | #endif |