On Thu, 18 Oct 2001, Thomas McGahee wrote: > Refer to the attached GIF. Hmm, this version is even simpler and lets you measure voltages far above the rails +-----------> PIC (not used in 1-pin version) | Vin o---/\/\/\---+-+-/\/\/\----< PIC R1 | R2 === | --- /// To measure 20V for example, you'd want R2 to be about 1/20 the size of R1. For example, 47k for R1 and 1Meg for R2. In the limits with the output at 5V and ground, the voltage at the cap is about 6V and 1V. Of course, the 6V will actually be clamped by the internal protection diode (and the 1meg severly limits the current). The algorithm is extremely simple: high_count = 0; for(i=0; i<2^N; i++) { if input is high make output low high_count++ else make output high } After the loop, high_count will be the digitized voltage on an N-bit scale. In my experiments, I could get 12 bits of dynamic range. However, I never tested this over a full temperature range, etc. Also, my voltage range was smaller than the rails. In other words, I did not make the input resistor 1Meg and the other resistor 47k; mine were both 10k. The only other issue is that you have to be careful about pre-charging the capacitor before starting the algorithm. Also, the loop must be isochronous. The poorly comment code that does this is here: http://www.dattalo.com/technical/software/pic/a2d.asm Alice has also experimented with this and determined that it doesn't work ell when the voltage source is a potentiometer. It's interesting to see why this is the case. It basically boils down to the pot having a high impedance AND changing impedance. The resistance of a pot as viewed from the wiper is Rh*Rl/Rt, i.e. the product of the high and low legs divided by the total resistance. Weird things happen to this circuit when the source impedance changes like this. Suffice it say that the PWM duty cycle is not linearly proportional to the wiper position. Scott -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu