User a frequecy of response using LM555 in circuit to mensure the capacitor unit

Dependencies:   mbed

Committer:
jhon309
Date:
Thu Jun 25 00:26:16 2015 +0000
Revision:
0:8b2d83f469fa
.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhon309 0:8b2d83f469fa 1 #ifndef CAP_SENSE_H
jhon309 0:8b2d83f469fa 2 #define CAP_SENSE_H
jhon309 0:8b2d83f469fa 3
jhon309 0:8b2d83f469fa 4 #include "mbed.h"
jhon309 0:8b2d83f469fa 5
jhon309 0:8b2d83f469fa 6 class CapSense {
jhon309 0:8b2d83f469fa 7
jhon309 0:8b2d83f469fa 8 public:
jhon309 0:8b2d83f469fa 9 int _Ra;
jhon309 0:8b2d83f469fa 10 int _Rb;
jhon309 0:8b2d83f469fa 11 int _Period;
jhon309 0:8b2d83f469fa 12
jhon309 0:8b2d83f469fa 13 CapSense(int, int, int, PinName);
jhon309 0:8b2d83f469fa 14 ~CapSense();
jhon309 0:8b2d83f469fa 15 float measure();
jhon309 0:8b2d83f469fa 16
jhon309 0:8b2d83f469fa 17 //private:
jhon309 0:8b2d83f469fa 18 volatile int _count;
jhon309 0:8b2d83f469fa 19 volatile float _partial;
jhon309 0:8b2d83f469fa 20 float _coeff;
jhon309 0:8b2d83f469fa 21
jhon309 0:8b2d83f469fa 22 void atInterrupt();
jhon309 0:8b2d83f469fa 23
jhon309 0:8b2d83f469fa 24 Timer _t;
jhon309 0:8b2d83f469fa 25 InterruptIn* _event;
jhon309 0:8b2d83f469fa 26 };
jhon309 0:8b2d83f469fa 27
jhon309 0:8b2d83f469fa 28 //Serial pc(USBTX, USBRX);
jhon309 0:8b2d83f469fa 29
jhon309 0:8b2d83f469fa 30 CapSense::CapSense(int Ra, int Rb, int Period, PinName pIn) {
jhon309 0:8b2d83f469fa 31 _Ra = Ra;
jhon309 0:8b2d83f469fa 32 _Rb = Rb;
jhon309 0:8b2d83f469fa 33 _Period = Period;
jhon309 0:8b2d83f469fa 34 _event = new InterruptIn(pIn);
jhon309 0:8b2d83f469fa 35
jhon309 0:8b2d83f469fa 36 _coeff = (_Ra*_Rb)/(float)(_Ra+_Rb) * log( (float)(_Rb-2*_Ra)/(2*_Rb-_Ra) );
jhon309 0:8b2d83f469fa 37 }
jhon309 0:8b2d83f469fa 38
jhon309 0:8b2d83f469fa 39 CapSense::~CapSense() {
jhon309 0:8b2d83f469fa 40 delete _event;
jhon309 0:8b2d83f469fa 41 }
jhon309 0:8b2d83f469fa 42
jhon309 0:8b2d83f469fa 43 void CapSense::atInterrupt(void) {
jhon309 0:8b2d83f469fa 44 static float lastTime = 1;
jhon309 0:8b2d83f469fa 45 float time = _t.read_us();
jhon309 0:8b2d83f469fa 46 _t.stop();
jhon309 0:8b2d83f469fa 47 _t.reset();
jhon309 0:8b2d83f469fa 48 _partial = (time < lastTime) ? time/lastTime : 1;
jhon309 0:8b2d83f469fa 49 _count++;
jhon309 0:8b2d83f469fa 50 lastTime = time;
jhon309 0:8b2d83f469fa 51 _t.start();
jhon309 0:8b2d83f469fa 52 }
jhon309 0:8b2d83f469fa 53
jhon309 0:8b2d83f469fa 54 float CapSense::measure() {
jhon309 0:8b2d83f469fa 55 _event->rise(this, &CapSense::atInterrupt);
jhon309 0:8b2d83f469fa 56
jhon309 0:8b2d83f469fa 57 _count = 0;
jhon309 0:8b2d83f469fa 58 _partial = 0;
jhon309 0:8b2d83f469fa 59 wait_us(_Period);
jhon309 0:8b2d83f469fa 60 float read = _count + _partial;
jhon309 0:8b2d83f469fa 61
jhon309 0:8b2d83f469fa 62 _event->rise(NULL);
jhon309 0:8b2d83f469fa 63 _t.stop();
jhon309 0:8b2d83f469fa 64 return (float)_Period / ((.693*_Ra + _coeff) * read);
jhon309 0:8b2d83f469fa 65 }
jhon309 0:8b2d83f469fa 66
jhon309 0:8b2d83f469fa 67 #endif