Jhon Plantikow
/
MensureCapacitor
User a frequecy of response using LM555 in circuit to mensure the capacitor unit
Revision 0:8b2d83f469fa, committed 2015-06-25
- Comitter:
- jhon309
- Date:
- Thu Jun 25 00:26:16 2015 +0000
- Commit message:
- .
Changed in this revision
diff -r 000000000000 -r 8b2d83f469fa capSense.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/capSense.h Thu Jun 25 00:26:16 2015 +0000 @@ -0,0 +1,67 @@ +#ifndef CAP_SENSE_H +#define CAP_SENSE_H + +#include "mbed.h" + +class CapSense { + + public: + int _Ra; + int _Rb; + int _Period; + + CapSense(int, int, int, PinName); + ~CapSense(); + float measure(); + + //private: + volatile int _count; + volatile float _partial; + float _coeff; + + void atInterrupt(); + + Timer _t; + InterruptIn* _event; +}; + +//Serial pc(USBTX, USBRX); + +CapSense::CapSense(int Ra, int Rb, int Period, PinName pIn) { + _Ra = Ra; + _Rb = Rb; + _Period = Period; + _event = new InterruptIn(pIn); + + _coeff = (_Ra*_Rb)/(float)(_Ra+_Rb) * log( (float)(_Rb-2*_Ra)/(2*_Rb-_Ra) ); +} + +CapSense::~CapSense() { + delete _event; +} + +void CapSense::atInterrupt(void) { + static float lastTime = 1; + float time = _t.read_us(); + _t.stop(); + _t.reset(); + _partial = (time < lastTime) ? time/lastTime : 1; + _count++; + lastTime = time; + _t.start(); +} + +float CapSense::measure() { + _event->rise(this, &CapSense::atInterrupt); + + _count = 0; + _partial = 0; + wait_us(_Period); + float read = _count + _partial; + + _event->rise(NULL); + _t.stop(); + return (float)_Period / ((.693*_Ra + _coeff) * read); +} + +#endif \ No newline at end of file
diff -r 000000000000 -r 8b2d83f469fa main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Jun 25 00:26:16 2015 +0000 @@ -0,0 +1,21 @@ +#include "mbed.h" +#include "capSense.h" +#include <ctype.h> + + +#define RA 51000 // ohms +#define RB 22000 // ohms +#define PERIOD 2000000 //us + + +Serial pc(SERIAL_TX, SERIAL_RX); +CapSense cap(RA, RB, PERIOD, PB_9); + +int main() +{ + while(1) + { + float capacitance = cap.measure(); + pc.printf("%.4f#%.2f\n", capacitance,(float)cap._count/1.999); + } +} \ No newline at end of file
diff -r 000000000000 -r 8b2d83f469fa mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jun 25 00:26:16 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file