; Measure capacitance of a large capacitor ; the concept is to charge the capacitor to near supply voltage ; which is 1023 on the ADC scale ; then discharge it through a known resistance, in this case, a 10k 1% resistor ; measuring the time to decay from 1000 to 1000(1/e) = 368 will allow us to calculate ; the time constant #picaxe 08m2 #no_data symbol reading1 = w0 symbol mstimer = w1 ; ports for 08M2 symbol probeport=C.1 symbol chargeport=C.2 symbol startsw=pinC.3 symbol resetsw=pinC.4 ; 32 MHz setfreq m32 pullup %011000 ; pullups only on input switches dirsC = %000100 ; 1=output, 0=input ; charge the capacitor to Vcc (5 volts) high chargeport chargemore: readadc10 probeport,reading1 sertxd(#reading1, 13, 10) pause 8000 if reading1 < 1020 then goto chargemore sertxd("Ready",13,10) ; wait for start signal to start discharge mstimer = 0 startwait: if startsw != 0 then goto startwait low chargeport ; start timing when we cross the 1000 threshold ; the 08M2 does not support a hardware timer ; through settimer, so must do my own with a delay loop sertxd("timer = ", #mstimer, 13, 10) waitupper: readadc10 probeport,reading1 if reading1 > 1000 then goto waitupper waitlower: low chargeport ; generate waveform for calibration of loop time readadc10 probeport,reading1 ; high chargeport ; use this for calibrating loop time with external scope low chargeport ; use this in production - must keep this no-op to preserve timing pauseus 424 ; whole loop time should be 1.00 mS ideally mstimer = mstimer + 1 if reading1 > 368 then goto waitlower sertxd("Done, timer = ", #mstimer, 13, 10) waitreset: if resetsw = 1 then goto waitreset reset