Measuring the capacity of a capacitor, dumping info to PC screen

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
GerritPathuis
Date:
Sat Jan 07 20:36:57 2012 +0000
Parent:
0:863c3f4f452f
Commit message:
Rev 2, algorithm now based on step response of a first order system.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 863c3f4f452f -r d95ae9068740 main.cpp
--- a/main.cpp	Sat Jan 07 16:35:13 2012 +0000
+++ b/main.cpp	Sat Jan 07 20:36:57 2012 +0000
@@ -4,15 +4,15 @@
 // GPa@quicknet.nl
 /////////////////////////////////////////
 //
-// An unkwown ceramic capacitor is connected to 
-// pin21 via a 82 kilo ohm resister, the other 
+// An unkwown ceramic capacitor is connected to
+// pin21 via a 82 kilo ohm resister, the other
 // capacitor terminal is connected to ground
 //
-// A ceramic capacitor is used becouse it is non polarized 
-// 
+// A ceramic capacitor is used becouse it is non polarized
+//
 //                                 ||
-//     pin21-----RRRRRRR-----------||------- pin1 (GND)        
-//                            |    || 
+//     pin21-----RRRRRRR-----------||------- pin1 (GND)
+//                            |    ||
 //                82k ohm     |    ?? Farad
 //                            |
 //     pin20-------------------
@@ -25,24 +25,22 @@
 // measure voltage on pin 20
 // If (not zero Volt) then wait
 // else pin 21 goes high 3.3 Volt
-// wait 2 micro seconds (the capacitor is now charging)
-// measure voltage on pin 20
-// the Voltage increase in 2 micro seconds is a measure
-// for the size of the capacitor
-// a small capacitor will charge fast a big one slow
+// Capacitor is charging
+// wait until 3.13 Volt being 3 Tou
+// Theory step response on a first order system
 //
-// Cap = current * time / measured voltage change
-// current = voltage over the resistor / resistor value
-// voltage max is 3.3 volt !!!
+// 1 x Tou= 63% final value
+// 2 x Tou= 86% final value
+// 3 x Tou= 95% final value
+//
+// Tou = R x C
+// ==> C = Tou / R
 //
 // The measuring results are send via Teraterm to the PC screen
 // Note 1)
 // The tolerance range of a ceramic capacitor
 // lies between 5 and 100%, not very accure at all.
 // Note 2)
-// Choose the charging time such that the capacitor 
-// reaches about 3 Volt, lower you get less accurate results
-// higher the 3.3 is not acceptable to the analogin
 //
 // Cap sizes are
 // m = 10^-3  (mili)
@@ -55,68 +53,58 @@
 AnalogIn ain(p20);
 DigitalOut red(p21);
 Serial pc(USBTX, USBRX); // tx, rx
+Timer timer;
 
 int main() {
-    int i, cnt;
-    double volt_0;       // first measurement
-    double volt_1;       // second measurement
-    double volt_delta;   // delta voltage
-    double current;      // max current trough resistor
-    double cx;           // capacitance to be calculated;
-    double mmtime =20;   // measure time in micro seconds
-
+    int i;
+    double timec;
+    double volt;       // Voltage measurement
+    double cx;         // capacitance
 
     pc.baud(9600);
     pc.format(8,Serial::None,1);
 
     for (i=0; i<2250; i +=1)  {
-        // starting at zero volt
-        red.write(0);
-        wait_ms(20);        // deterime the number of measurements
+        wait_ms(10);                  // deterime the number of measurements
 
-        // start measurement, wait for 0.0 volt on input
-        volt_0 = 1;
-        cnt=0;
-        while (volt_0 > 0.0001) {   // wait for 0.0 Volt
-            wait_us(2);  
-            cnt+=1;                 // wait
-            volt_0 = ain.read();
+        // Wait for 0.0 volt on input
+        red.write(0);
+        volt = 1;
+        while (volt > 0.0001) {     // wait for 0.0 Volt
+            wait_us(2);
+            volt = ain.read();
         }
+        // start to measure
+        timer.reset();
+        timer.start();
         red.write(1);               // output high
-        wait_us(mmtime);            // measuring time
-        volt_1 = ain.read();
+
+        while (volt < 3.13) {       // wait for 3.13 Volt
+            volt = ain.read()*3.3;
+        }
+        timer.stop();
+        timec=timer.read();
         red.write(0);               // output low
 
         // calculations
-        volt_0 *= 3.3;
-        volt_1 *= 3.3;
+        cx = (double) timec/ (3 * 82000);
 
-        volt_delta = abs(volt_1 - volt_0);
-        current = (3.3- volt_delta/2) / 82000;   // 82k resistor
-        cx = current* mmtime/(1000 * 1000 * volt_delta);
-        //pc.printf("Volt_0  %f, volt_1 %f, current %f  ", volt_0, volt_1, current);
-        //pc.printf("Delta voltage %f, time %f ", volt_delta, mmtime);
-        pc.printf("cnt= %3d, Delta= %5.4f Volt ", cnt, volt_delta);
-        if (volt_1 > 3.1)
-            pc.printf("Out of range \n\r");
-        else {
-            if (cx < 1 and cx >= 0.001) {
-                cx = cx *1000;
-                pc.printf("Cap. is %5.4f milli Farad    ", cx);
-            } else if (cx < 0.001 and cx >= 0.000001) {
-                cx = cx *1000 *1000;
-                pc.printf("Cap. is %5.4f micro Farad     ", cx);
-            } else if (cx < 0.000001 and cx >= 0.000000001) {
-                cx = cx *1000 *1000 *1000;
-                pc.printf("Cap. is %4.2f nano Farad      ", cx);
-            }
-            if (cx <  0.000000001 and cx >=  0.000000000001) {
-                cx = cx *1000 *1000 *1000 *1000;
-                pc.printf("Cap. is %4.0f pico Farad       ", cx);
-            } else if (cx < 0.000000000001)
-                pc.printf("Cx = %f Out of range too small! ", cx);
-            pc.printf("\r");
-            // pc.printf("\n");
-        }
+        pc.printf("Volt %3.2f, time %9.4f msec ", volt, timec*1000);
+        if (cx < 1 and cx >= 0.001) {
+            cx = cx *1000;
+            pc.printf("Cap. is %5.1f milli Farad    ", cx);
+        } else if (cx < 0.001 and cx >= 0.000001) {
+            cx = cx *1000 *1000;
+            pc.printf("Cap. is %5.1f micro Farad     ", cx);
+        } else if (cx < 0.000001 and cx >= 0.000000001) {
+            cx = cx *1000 *1000 *1000;
+            pc.printf("Cap. is %5.1f nano Farad      ", cx);
+        } else if (cx <  0.000000001 and cx >=  0.000000000001) {
+            cx = cx *1000 *1000 *1000 *1000;
+            pc.printf("Cap. is %5.1f pico Farad       ", cx);
+        } else if (cx < 0.000000000001)
+            pc.printf("Cx = %5.1f Out of range too small! ", cx);
+        pc.printf("\r");
+        pc.printf("\n");
     }
-}
+}
\ No newline at end of file