![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Measure capacitances by counting rising edges of a 555 astable oscillator.
Dependencies: 4DGL-uLCD-SE mbed
capSense.h@0:292d0c987fe3, 2014-03-24 (annotated)
- Committer:
- jford38
- Date:
- Mon Mar 24 19:47:55 2014 +0000
- Revision:
- 0:292d0c987fe3
Capacitive Sensing using 555 astable oscillator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jford38 | 0:292d0c987fe3 | 1 | #ifndef CAP_SENSE_H |
jford38 | 0:292d0c987fe3 | 2 | #define CAP_SENSE_H |
jford38 | 0:292d0c987fe3 | 3 | |
jford38 | 0:292d0c987fe3 | 4 | #include "mbed.h" |
jford38 | 0:292d0c987fe3 | 5 | |
jford38 | 0:292d0c987fe3 | 6 | class CapSense { |
jford38 | 0:292d0c987fe3 | 7 | |
jford38 | 0:292d0c987fe3 | 8 | public: |
jford38 | 0:292d0c987fe3 | 9 | int _Ra; |
jford38 | 0:292d0c987fe3 | 10 | int _Rb; |
jford38 | 0:292d0c987fe3 | 11 | int _Period; |
jford38 | 0:292d0c987fe3 | 12 | |
jford38 | 0:292d0c987fe3 | 13 | CapSense(int, int, int, PinName); |
jford38 | 0:292d0c987fe3 | 14 | ~CapSense(); |
jford38 | 0:292d0c987fe3 | 15 | float measure(); |
jford38 | 0:292d0c987fe3 | 16 | |
jford38 | 0:292d0c987fe3 | 17 | //private: |
jford38 | 0:292d0c987fe3 | 18 | volatile int _count; |
jford38 | 0:292d0c987fe3 | 19 | volatile float _partial; |
jford38 | 0:292d0c987fe3 | 20 | float _coeff; |
jford38 | 0:292d0c987fe3 | 21 | |
jford38 | 0:292d0c987fe3 | 22 | void atInterrupt(); |
jford38 | 0:292d0c987fe3 | 23 | |
jford38 | 0:292d0c987fe3 | 24 | Timer _t; |
jford38 | 0:292d0c987fe3 | 25 | InterruptIn* _event; |
jford38 | 0:292d0c987fe3 | 26 | }; |
jford38 | 0:292d0c987fe3 | 27 | |
jford38 | 0:292d0c987fe3 | 28 | Serial pc(USBTX, USBRX); |
jford38 | 0:292d0c987fe3 | 29 | |
jford38 | 0:292d0c987fe3 | 30 | CapSense::CapSense(int Ra, int Rb, int Period, PinName pIn) { |
jford38 | 0:292d0c987fe3 | 31 | _Ra = Ra; |
jford38 | 0:292d0c987fe3 | 32 | _Rb = Rb; |
jford38 | 0:292d0c987fe3 | 33 | _Period = Period; |
jford38 | 0:292d0c987fe3 | 34 | _event = new InterruptIn(pIn); |
jford38 | 0:292d0c987fe3 | 35 | |
jford38 | 0:292d0c987fe3 | 36 | _coeff = (_Ra*_Rb)/(float)(_Ra+_Rb) * log( (float)(_Rb-2*_Ra)/(2*_Rb-_Ra) ); |
jford38 | 0:292d0c987fe3 | 37 | } |
jford38 | 0:292d0c987fe3 | 38 | |
jford38 | 0:292d0c987fe3 | 39 | CapSense::~CapSense() { |
jford38 | 0:292d0c987fe3 | 40 | delete _event; |
jford38 | 0:292d0c987fe3 | 41 | } |
jford38 | 0:292d0c987fe3 | 42 | |
jford38 | 0:292d0c987fe3 | 43 | void CapSense::atInterrupt(void) { |
jford38 | 0:292d0c987fe3 | 44 | static float lastTime = 1; |
jford38 | 0:292d0c987fe3 | 45 | float time = _t.read_us(); |
jford38 | 0:292d0c987fe3 | 46 | _t.stop(); |
jford38 | 0:292d0c987fe3 | 47 | _t.reset(); |
jford38 | 0:292d0c987fe3 | 48 | _partial = (time < lastTime) ? time/lastTime : 1; |
jford38 | 0:292d0c987fe3 | 49 | _count++; |
jford38 | 0:292d0c987fe3 | 50 | lastTime = time; |
jford38 | 0:292d0c987fe3 | 51 | _t.start(); |
jford38 | 0:292d0c987fe3 | 52 | } |
jford38 | 0:292d0c987fe3 | 53 | |
jford38 | 0:292d0c987fe3 | 54 | float CapSense::measure() { |
jford38 | 0:292d0c987fe3 | 55 | _event->rise(this, &CapSense::atInterrupt); |
jford38 | 0:292d0c987fe3 | 56 | |
jford38 | 0:292d0c987fe3 | 57 | _count = 0; |
jford38 | 0:292d0c987fe3 | 58 | _partial = 0; |
jford38 | 0:292d0c987fe3 | 59 | wait_us(_Period); |
jford38 | 0:292d0c987fe3 | 60 | float read = _count + _partial; |
jford38 | 0:292d0c987fe3 | 61 | |
jford38 | 0:292d0c987fe3 | 62 | _event->rise(NULL); |
jford38 | 0:292d0c987fe3 | 63 | _t.stop(); |
jford38 | 0:292d0c987fe3 | 64 | return (float)_Period / ((.693*_Ra + _coeff) * read); |
jford38 | 0:292d0c987fe3 | 65 | } |
jford38 | 0:292d0c987fe3 | 66 | |
jford38 | 0:292d0c987fe3 | 67 | #endif |