Counts interrupt driven pulses (UP or DOWN) with min and max width

Dependents:   PulseSensor test_hw_biniou

Committer:
Wimpie
Date:
Sun Apr 17 17:41:12 2011 +0000
Revision:
0:d534558752b8
Pulse counter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wimpie 0:d534558752b8 1 /*
Wimpie 0:d534558752b8 2 PulseCounter
Wimpie 0:d534558752b8 3 interrrupt driven counter of edges (UP or DOWN)
Wimpie 0:d534558752b8 4 with min en max width
Wimpie 0:d534558752b8 5
Wimpie 0:d534558752b8 6 Copyright (c) 2010 Wim De Roeve
Wimpie 0:d534558752b8 7
Wimpie 0:d534558752b8 8 Permission is hereby granted, free of charge, to any person obtaining a copy
Wimpie 0:d534558752b8 9 of this software and associated documentation files (the "Software"), to deal
Wimpie 0:d534558752b8 10 in the Software without restriction, including without limitation the rights
Wimpie 0:d534558752b8 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Wimpie 0:d534558752b8 12 copies of the Software, and to permit persons to whom the Software is
Wimpie 0:d534558752b8 13 furnished to do so, subject to the following conditions:
Wimpie 0:d534558752b8 14
Wimpie 0:d534558752b8 15 The above copyright notice and this permission notice shall be included in
Wimpie 0:d534558752b8 16 all copies or substantial portions of the Software.
Wimpie 0:d534558752b8 17
Wimpie 0:d534558752b8 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Wimpie 0:d534558752b8 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Wimpie 0:d534558752b8 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Wimpie 0:d534558752b8 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Wimpie 0:d534558752b8 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Wimpie 0:d534558752b8 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Wimpie 0:d534558752b8 24 THE SOFTWARE.
Wimpie 0:d534558752b8 25 */
Wimpie 0:d534558752b8 26
Wimpie 0:d534558752b8 27 #ifndef PULSECOUNTER_H
Wimpie 0:d534558752b8 28 #define PULSECOUNTER_H
Wimpie 0:d534558752b8 29
Wimpie 0:d534558752b8 30 #include "mbed.h"
Wimpie 0:d534558752b8 31
Wimpie 0:d534558752b8 32 extern "C" DigitalOut led3;
Wimpie 0:d534558752b8 33 extern "C" DigitalOut led4;
Wimpie 0:d534558752b8 34
Wimpie 0:d534558752b8 35 enum PULSMODE { PULSE_DOWN=false, PULSE_UP = true};
Wimpie 0:d534558752b8 36
Wimpie 0:d534558752b8 37 class PulseCounter {
Wimpie 0:d534558752b8 38 public:
Wimpie 0:d534558752b8 39 PulseCounter(PinName pin, PinMode pull, int step, uint32_t minwidth, uint32_t maxwidth,
Wimpie 0:d534558752b8 40 bool up, char c):
Wimpie 0:d534558752b8 41 _interrupt(pin)
Wimpie 0:d534558752b8 42 {
Wimpie 0:d534558752b8 43
Wimpie 0:d534558752b8 44 if (up) { // pulse up
Wimpie 0:d534558752b8 45 _interrupt.rise(this, &PulseCounter::startpulse); // first rise then fall
Wimpie 0:d534558752b8 46 _interrupt.fall(this, &PulseCounter::endpulse);
Wimpie 0:d534558752b8 47
Wimpie 0:d534558752b8 48 } else { // pulse down
Wimpie 0:d534558752b8 49 _interrupt.fall(this, &PulseCounter::startpulse); // first fall then rise
Wimpie 0:d534558752b8 50 _interrupt.rise(this, &PulseCounter::endpulse);
Wimpie 0:d534558752b8 51 }
Wimpie 0:d534558752b8 52
Wimpie 0:d534558752b8 53 _interrupt.mode(pull);
Wimpie 0:d534558752b8 54
Wimpie 0:d534558752b8 55 _step=step;
Wimpie 0:d534558752b8 56 _count=0;
Wimpie 0:d534558752b8 57 _countall=0;
Wimpie 0:d534558752b8 58 _minwidth =minwidth;
Wimpie 0:d534558752b8 59 _maxwidth =maxwidth;
Wimpie 0:d534558752b8 60
Wimpie 0:d534558752b8 61 _useled3= (c=='E');
Wimpie 0:d534558752b8 62 _f=false;
Wimpie 0:d534558752b8 63 _t.stop();
Wimpie 0:d534558752b8 64 _t.reset();
Wimpie 0:d534558752b8 65 _up=up;
Wimpie 0:d534558752b8 66
Wimpie 0:d534558752b8 67 }
Wimpie 0:d534558752b8 68
Wimpie 0:d534558752b8 69 void startpulse() {
Wimpie 0:d534558752b8 70
Wimpie 0:d534558752b8 71 _diff_start=_t.read_ms();
Wimpie 0:d534558752b8 72
Wimpie 0:d534558752b8 73 _t.reset();
Wimpie 0:d534558752b8 74 _t.start();
Wimpie 0:d534558752b8 75
Wimpie 0:d534558752b8 76 if (_useled3)
Wimpie 0:d534558752b8 77 led3=1;
Wimpie 0:d534558752b8 78 else
Wimpie 0:d534558752b8 79 led4=1;
Wimpie 0:d534558752b8 80
Wimpie 0:d534558752b8 81 _f=true; // pulse is on
Wimpie 0:d534558752b8 82 }
Wimpie 0:d534558752b8 83
Wimpie 0:d534558752b8 84 void endpulse() {
Wimpie 0:d534558752b8 85 _diff_stop=_t.read_ms();
Wimpie 0:d534558752b8 86 _t.stop();
Wimpie 0:d534558752b8 87
Wimpie 0:d534558752b8 88 if (_useled3)
Wimpie 0:d534558752b8 89 led3=0;
Wimpie 0:d534558752b8 90 else
Wimpie 0:d534558752b8 91 led4=0;
Wimpie 0:d534558752b8 92
Wimpie 0:d534558752b8 93
Wimpie 0:d534558752b8 94 if ((_diff_stop >= _minwidth) and (_diff_stop <= _maxwidth) and (_f)) {
Wimpie 0:d534558752b8 95
Wimpie 0:d534558752b8 96 _count=_count+_step;
Wimpie 0:d534558752b8 97 }
Wimpie 0:d534558752b8 98 _countall=_countall+_step;
Wimpie 0:d534558752b8 99 _f=false; // pulse is off
Wimpie 0:d534558752b8 100 }
Wimpie 0:d534558752b8 101
Wimpie 0:d534558752b8 102
Wimpie 0:d534558752b8 103 int read() {
Wimpie 0:d534558752b8 104 return _count;
Wimpie 0:d534558752b8 105 }
Wimpie 0:d534558752b8 106
Wimpie 0:d534558752b8 107 int readall() {
Wimpie 0:d534558752b8 108 return _countall;
Wimpie 0:d534558752b8 109 }
Wimpie 0:d534558752b8 110
Wimpie 0:d534558752b8 111
Wimpie 0:d534558752b8 112 void set(int value) {
Wimpie 0:d534558752b8 113 _count=value;
Wimpie 0:d534558752b8 114 }
Wimpie 0:d534558752b8 115
Wimpie 0:d534558752b8 116 void setall(int value) {
Wimpie 0:d534558752b8 117 _countall=value;
Wimpie 0:d534558752b8 118 }
Wimpie 0:d534558752b8 119
Wimpie 0:d534558752b8 120 private:
Wimpie 0:d534558752b8 121 InterruptIn _interrupt;
Wimpie 0:d534558752b8 122
Wimpie 0:d534558752b8 123 volatile int _count;
Wimpie 0:d534558752b8 124 volatile int _countall;
Wimpie 0:d534558752b8 125 volatile int _step;
Wimpie 0:d534558752b8 126
Wimpie 0:d534558752b8 127 Timer _t;
Wimpie 0:d534558752b8 128
Wimpie 0:d534558752b8 129 float _minwidth;
Wimpie 0:d534558752b8 130 float _maxwidth;
Wimpie 0:d534558752b8 131 bool _f;
Wimpie 0:d534558752b8 132 bool _up;
Wimpie 0:d534558752b8 133 bool _useled3;
Wimpie 0:d534558752b8 134 uint32_t _diff_start;
Wimpie 0:d534558752b8 135 uint32_t _diff_stop;
Wimpie 0:d534558752b8 136
Wimpie 0:d534558752b8 137 };
Wimpie 0:d534558752b8 138 #endif