Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@1:d95ae9068740, 2012-01-07 (annotated)
- Committer:
- GerritPathuis
- Date:
- Sat Jan 07 20:36:57 2012 +0000
- Revision:
- 1:d95ae9068740
- Parent:
- 0:863c3f4f452f
Rev 2, algorithm now based on step response of a first order system.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| GerritPathuis | 0:863c3f4f452f | 1 | #include "mbed.h" |
| GerritPathuis | 0:863c3f4f452f | 2 | |
| GerritPathuis | 0:863c3f4f452f | 3 | ////////////////////////////////////////// |
| GerritPathuis | 0:863c3f4f452f | 4 | // GPa@quicknet.nl |
| GerritPathuis | 0:863c3f4f452f | 5 | ///////////////////////////////////////// |
| GerritPathuis | 0:863c3f4f452f | 6 | // |
| GerritPathuis | 1:d95ae9068740 | 7 | // An unkwown ceramic capacitor is connected to |
| GerritPathuis | 1:d95ae9068740 | 8 | // pin21 via a 82 kilo ohm resister, the other |
| GerritPathuis | 0:863c3f4f452f | 9 | // capacitor terminal is connected to ground |
| GerritPathuis | 0:863c3f4f452f | 10 | // |
| GerritPathuis | 1:d95ae9068740 | 11 | // A ceramic capacitor is used becouse it is non polarized |
| GerritPathuis | 1:d95ae9068740 | 12 | // |
| GerritPathuis | 0:863c3f4f452f | 13 | // || |
| GerritPathuis | 1:d95ae9068740 | 14 | // pin21-----RRRRRRR-----------||------- pin1 (GND) |
| GerritPathuis | 1:d95ae9068740 | 15 | // | || |
| GerritPathuis | 0:863c3f4f452f | 16 | // 82k ohm | ?? Farad |
| GerritPathuis | 0:863c3f4f452f | 17 | // | |
| GerritPathuis | 0:863c3f4f452f | 18 | // pin20------------------- |
| GerritPathuis | 0:863c3f4f452f | 19 | // |
| GerritPathuis | 0:863c3f4f452f | 20 | // Mbed pins used |
| GerritPathuis | 0:863c3f4f452f | 21 | // Pin 20...... analog input |
| GerritPathuis | 0:863c3f4f452f | 22 | // Pin 21...... digital out |
| GerritPathuis | 0:863c3f4f452f | 23 | ////////////////////////////////////////// |
| GerritPathuis | 0:863c3f4f452f | 24 | // How does it work |
| GerritPathuis | 0:863c3f4f452f | 25 | // measure voltage on pin 20 |
| GerritPathuis | 0:863c3f4f452f | 26 | // If (not zero Volt) then wait |
| GerritPathuis | 0:863c3f4f452f | 27 | // else pin 21 goes high 3.3 Volt |
| GerritPathuis | 1:d95ae9068740 | 28 | // Capacitor is charging |
| GerritPathuis | 1:d95ae9068740 | 29 | // wait until 3.13 Volt being 3 Tou |
| GerritPathuis | 1:d95ae9068740 | 30 | // Theory step response on a first order system |
| GerritPathuis | 0:863c3f4f452f | 31 | // |
| GerritPathuis | 1:d95ae9068740 | 32 | // 1 x Tou= 63% final value |
| GerritPathuis | 1:d95ae9068740 | 33 | // 2 x Tou= 86% final value |
| GerritPathuis | 1:d95ae9068740 | 34 | // 3 x Tou= 95% final value |
| GerritPathuis | 1:d95ae9068740 | 35 | // |
| GerritPathuis | 1:d95ae9068740 | 36 | // Tou = R x C |
| GerritPathuis | 1:d95ae9068740 | 37 | // ==> C = Tou / R |
| GerritPathuis | 0:863c3f4f452f | 38 | // |
| GerritPathuis | 0:863c3f4f452f | 39 | // The measuring results are send via Teraterm to the PC screen |
| GerritPathuis | 0:863c3f4f452f | 40 | // Note 1) |
| GerritPathuis | 0:863c3f4f452f | 41 | // The tolerance range of a ceramic capacitor |
| GerritPathuis | 0:863c3f4f452f | 42 | // lies between 5 and 100%, not very accure at all. |
| GerritPathuis | 0:863c3f4f452f | 43 | // Note 2) |
| GerritPathuis | 0:863c3f4f452f | 44 | // |
| GerritPathuis | 0:863c3f4f452f | 45 | // Cap sizes are |
| GerritPathuis | 0:863c3f4f452f | 46 | // m = 10^-3 (mili) |
| GerritPathuis | 0:863c3f4f452f | 47 | // u = 10^-6 (micro) |
| GerritPathuis | 0:863c3f4f452f | 48 | // n = 10^-9 (nano) |
| GerritPathuis | 0:863c3f4f452f | 49 | // p = 10^-12 (pico) |
| GerritPathuis | 0:863c3f4f452f | 50 | // f = 10^-15 (femto) |
| GerritPathuis | 0:863c3f4f452f | 51 | ///////////////////////////////////////// |
| GerritPathuis | 0:863c3f4f452f | 52 | |
| GerritPathuis | 0:863c3f4f452f | 53 | AnalogIn ain(p20); |
| GerritPathuis | 0:863c3f4f452f | 54 | DigitalOut red(p21); |
| GerritPathuis | 0:863c3f4f452f | 55 | Serial pc(USBTX, USBRX); // tx, rx |
| GerritPathuis | 1:d95ae9068740 | 56 | Timer timer; |
| GerritPathuis | 0:863c3f4f452f | 57 | |
| GerritPathuis | 0:863c3f4f452f | 58 | int main() { |
| GerritPathuis | 1:d95ae9068740 | 59 | int i; |
| GerritPathuis | 1:d95ae9068740 | 60 | double timec; |
| GerritPathuis | 1:d95ae9068740 | 61 | double volt; // Voltage measurement |
| GerritPathuis | 1:d95ae9068740 | 62 | double cx; // capacitance |
| GerritPathuis | 0:863c3f4f452f | 63 | |
| GerritPathuis | 0:863c3f4f452f | 64 | pc.baud(9600); |
| GerritPathuis | 0:863c3f4f452f | 65 | pc.format(8,Serial::None,1); |
| GerritPathuis | 0:863c3f4f452f | 66 | |
| GerritPathuis | 0:863c3f4f452f | 67 | for (i=0; i<2250; i +=1) { |
| GerritPathuis | 1:d95ae9068740 | 68 | wait_ms(10); // deterime the number of measurements |
| GerritPathuis | 0:863c3f4f452f | 69 | |
| GerritPathuis | 1:d95ae9068740 | 70 | // Wait for 0.0 volt on input |
| GerritPathuis | 1:d95ae9068740 | 71 | red.write(0); |
| GerritPathuis | 1:d95ae9068740 | 72 | volt = 1; |
| GerritPathuis | 1:d95ae9068740 | 73 | while (volt > 0.0001) { // wait for 0.0 Volt |
| GerritPathuis | 1:d95ae9068740 | 74 | wait_us(2); |
| GerritPathuis | 1:d95ae9068740 | 75 | volt = ain.read(); |
| GerritPathuis | 0:863c3f4f452f | 76 | } |
| GerritPathuis | 1:d95ae9068740 | 77 | // start to measure |
| GerritPathuis | 1:d95ae9068740 | 78 | timer.reset(); |
| GerritPathuis | 1:d95ae9068740 | 79 | timer.start(); |
| GerritPathuis | 0:863c3f4f452f | 80 | red.write(1); // output high |
| GerritPathuis | 1:d95ae9068740 | 81 | |
| GerritPathuis | 1:d95ae9068740 | 82 | while (volt < 3.13) { // wait for 3.13 Volt |
| GerritPathuis | 1:d95ae9068740 | 83 | volt = ain.read()*3.3; |
| GerritPathuis | 1:d95ae9068740 | 84 | } |
| GerritPathuis | 1:d95ae9068740 | 85 | timer.stop(); |
| GerritPathuis | 1:d95ae9068740 | 86 | timec=timer.read(); |
| GerritPathuis | 0:863c3f4f452f | 87 | red.write(0); // output low |
| GerritPathuis | 0:863c3f4f452f | 88 | |
| GerritPathuis | 0:863c3f4f452f | 89 | // calculations |
| GerritPathuis | 1:d95ae9068740 | 90 | cx = (double) timec/ (3 * 82000); |
| GerritPathuis | 0:863c3f4f452f | 91 | |
| GerritPathuis | 1:d95ae9068740 | 92 | pc.printf("Volt %3.2f, time %9.4f msec ", volt, timec*1000); |
| GerritPathuis | 1:d95ae9068740 | 93 | if (cx < 1 and cx >= 0.001) { |
| GerritPathuis | 1:d95ae9068740 | 94 | cx = cx *1000; |
| GerritPathuis | 1:d95ae9068740 | 95 | pc.printf("Cap. is %5.1f milli Farad ", cx); |
| GerritPathuis | 1:d95ae9068740 | 96 | } else if (cx < 0.001 and cx >= 0.000001) { |
| GerritPathuis | 1:d95ae9068740 | 97 | cx = cx *1000 *1000; |
| GerritPathuis | 1:d95ae9068740 | 98 | pc.printf("Cap. is %5.1f micro Farad ", cx); |
| GerritPathuis | 1:d95ae9068740 | 99 | } else if (cx < 0.000001 and cx >= 0.000000001) { |
| GerritPathuis | 1:d95ae9068740 | 100 | cx = cx *1000 *1000 *1000; |
| GerritPathuis | 1:d95ae9068740 | 101 | pc.printf("Cap. is %5.1f nano Farad ", cx); |
| GerritPathuis | 1:d95ae9068740 | 102 | } else if (cx < 0.000000001 and cx >= 0.000000000001) { |
| GerritPathuis | 1:d95ae9068740 | 103 | cx = cx *1000 *1000 *1000 *1000; |
| GerritPathuis | 1:d95ae9068740 | 104 | pc.printf("Cap. is %5.1f pico Farad ", cx); |
| GerritPathuis | 1:d95ae9068740 | 105 | } else if (cx < 0.000000000001) |
| GerritPathuis | 1:d95ae9068740 | 106 | pc.printf("Cx = %5.1f Out of range too small! ", cx); |
| GerritPathuis | 1:d95ae9068740 | 107 | pc.printf("\r"); |
| GerritPathuis | 1:d95ae9068740 | 108 | pc.printf("\n"); |
| GerritPathuis | 0:863c3f4f452f | 109 | } |
| GerritPathuis | 1:d95ae9068740 | 110 | } |