On Sun, Oct 28, 2001 at 11:54:53AM +0200, wrote: > BlankHi, > > I need help with doing a 8-bit A2D conversion on a 0-5V analog input, by using the PIC's compare mode and 2 counters... > > If any1 has done something like this before, please help... A gentle piece of advise: Please turn off HTML mode when posting to the list. You describe two of the four elements you need. The third is the PWM module. And the fourth is a hold module. It's called a sample and hold A/D converter. Here is the basic elements: 1) Use a hold circuit to charge a cap to the target voltage. 2) Connect the output of the hold and the output of the PWM (use a low pass filter on its output of course) to the comparitor. 2) Sweep the PWM module across its range. At the point the comparitor indicates that the comparison between the PWM and the hold circuit changes, the current value of the A/D is the value of the unknown input voltage. Now it takes time. Time to sample and hold. Time to adjust the PWM voltage to settle. Time to do the comparison. But you can get an accurate measurement given enough time. You can make it a bit faster by doing a binary search instead of a linear search. Say for example the unknown voltage is 4.377V (A/D value of 224). In the linear search you'd have to update the PWM 224 times. Bit a binary search can limit the number of searches. 1) Set bit 7 of PWM . Compare (2.5 < 4.377). Target is higher. Set bit 7 of result to 1. 2) Set bit 6 of PWM. Compare (3.75 < 4.377). Target is higher. Set bit 6 of result to 1. 3) Set bit 5 of PWM. Compare (4.375 < 4.377). Target is higher. Set bit 5 of result to 1. 4) Set bit 4 of PWM. Compare (4.687 > 4.377). Target is lower. Set bit 4 of result to 0. Continue with lower bits. PWM will be higher for all of them. So set all of the lower bits to 0. Result is 11100000 -> 224. Note that it only took 8 PWM samples to compute the target. Hope this helps. BAJ -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu