Measure capacitances by counting rising edges of a 555 astable oscillator.
Dependencies: 4DGL-uLCD-SE mbed
Revision 0:292d0c987fe3, committed 2014-03-24
- Comitter:
- jford38
- Date:
- Mon Mar 24 19:47:55 2014 +0000
- Commit message:
- Capacitive Sensing using 555 astable oscillator
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4DGL-uLCD-SE.lib Mon Mar 24 19:47:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/capSense.h Mon Mar 24 19:47:55 2014 +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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Mar 24 19:47:55 2014 +0000 @@ -0,0 +1,22 @@ +#include "mbed.h" +#include "capSense.h" +#include "uLCD_4DGL.h" + + +#define RA 19770 // ohms +#define RB 9053 // ohms +#define PERIOD 2000000 //us + +uLCD_4DGL uLCD(p9, p10, p11); // lcd connection +CapSense cap(RA, RB, PERIOD, p21); + +int main() { + uLCD.locate(3,5); //setting the cursor to the middle + + while(1) { + uLCD.locate(0,6); // setting cursor + float capacitance = cap.measure(); + uLCD.printf("Capacitance:\n%f uF \n\n", capacitance); + uLCD.printf("Frequency:\n%f Hz ",(float)cap._count/PERIOD*1000000); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Mar 24 19:47:55 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06 \ No newline at end of file