Measurement of low frequencys based on timing between pulses

Dependents:   Energy_Meter_S0_Example

Revision:
2:fc21262db17a
Parent:
1:6eb686d7d16a
Child:
3:36dd0d59fdc8
--- a/Pulses.h	Thu Nov 08 07:25:41 2012 +0000
+++ b/Pulses.h	Thu Nov 08 12:01:02 2012 +0000
@@ -24,9 +24,20 @@
 * mbed Pulses Library, for measurement of low frequencys based on timing between pulses
 *
 * Use cases:
+* - Frequency counter for frequ. about or below sample rate (Hz)
 * - Motor rotations (rpm)
 * - Energy meter with SO interface
 *
+ *
+*/
+
+#ifndef MBED_PULSES_H
+#define MBED_PULSES_H
+
+#include "mbed.h"
+
+/** A class to calculate frequencys based on timing between pulses. Pulse-frequency can be about or slower than loop/aquisition time
+*
 * Example:
 *
 * @code
@@ -44,53 +55,15 @@
     
     while(1) {
         pc.printf ( "Pulses: counter=%d act=%.3f average=%.3f\r\n", 
-            pulses.getCounter(), 
+            , 
             pulses.getAct(), 
-            pulses.getAverage() );
+            pulses.getAverage(),
+            pulses.getCounter() );
         
         wait(3.14);
     }
 }
  * @endcode
- *
-*/
-
-#ifndef MBED_PULSES_H
-#define MBED_PULSES_H
-
-#include "mbed.h"
-
-class TimerDiff : public Timer {
-public:
-    TimerDiff(const char *name = NULL) : Timer(name) {
-        reset();
-        start();
-        _last = 0;
-    };
-    
-    unsigned int readDiff_us() {
-        unsigned int act = read_us();
-        unsigned int diff;
-        
-        if ( act < _last )   // overflow
-            diff = act - _last;
-        else
-            diff = act - _last;
-        _last = act;
-        return diff;
-    }
-    
-    float readDiff() {
-        return (float)readDiff_us() * 0.000001;
-    }
-
-protected:
-    unsigned int _last;
-};
-
-
-/** A class to calculate frequencys based on timing between pulses. Pulse-frequency can be about or slower than loop/aquisition time
- *
  */
 class Pulses {
 public:
@@ -104,13 +77,14 @@
 
     /** constructor of Pulses object
      *
-     * @param inPin    Pin number of input pin. All port pins except p19 and p20
+     * @param inPin    Pin number of input pin. All port pins are possible except p19 and p20
      * @param type     Type of edge detection.
      * @param timeout  Timeout in seconds to handle an offline pulse-generator (standing motor). Max 15 minutes.
+     * @param counter  Start value of the internal pulses counter to offset pSum (in get()) e.g. after reboot
      */
     explicit Pulses(PinName inPin, PulseType type = RISE, unsigned int timeout=600, unsigned int counter=0);
 
-    /** Gets the frequency based on the last 2 pulses
+    /** Gets the frequency based on the last 2 pulses. It's only a snapshot and its not representative.
      *
      * @return        Actual frequencey
      */
@@ -131,7 +105,7 @@
      */
     void get(float *pAverage, float *pMin, float *pMax, float *pSum=NULL);
 
-    /** Gets the number of pulses since start
+    /** Gets the number of pulses from the input pin since start
      *
      * @return        Number of pulses
      */
@@ -145,16 +119,17 @@
     
 
 protected:
-
+    // ISR
     void callback_in();
     void callback_timeout();
 
     InterruptIn _in;
-    TimerDiff _timer;
+    Timer _timer;
     Ticker _timeout;
     
     PulseType _type;
     
+    unsigned int _lastTimer;
     unsigned int _ActTime;
     unsigned int _MinTime;
     unsigned int _MaxTime;