View previous topic :: View next topic |
Author |
Message |
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
Using a comparator as an ADC |
Posted: Fri Feb 25, 2005 4:00 am |
|
|
Hi,
MCU : 16F628
Compiler : PCM Compiler ver3.1619
Xtal : 4 Mhz
Is it possible to use the comparator input as an ADC ?? I would like to read voltages upto 2.5 V ( 5 V would be preferable).
thanks
arun |
|
|
Ttelmah Guest
|
|
Posted: Fri Feb 25, 2005 4:53 am |
|
|
Yes(ish)...
Basically, you need to generate some way of ramping a voltage. If (for instance), you build an external constant current source, charging up a capacitor, then attach the junction pin of this, to an pin on the PIC, and to one of the comparator inputs, with the voltage you want to measure to the other, you can turn the pin to an output, and pull the capacitor 'low'. Wait a while for it to fully discharge, then let the output 'float', and time how long it takes for the compator to switch. The time is proportional to the voltage being measured.
You could also use a PWM output to form a DAC, and compare the voltage from this with the required input.
You can even simplify the ramping system, just using a resistor to charge the capacitor, but then you have to allow for the ramp being non-linear in the code.
Just about every ADC, contains a comparator, and this component forms the 'core' part needed to make an ADC, but there needs to be some other parts to support it.
Best Wishes |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Feb 25, 2005 8:40 am |
|
|
Look at Microchip's application notes AN512 and AN513 on how to measure analog signals with digital inputs. They are old but still just as sound as when they were written. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
wireless
Joined: 02 Nov 2003 Posts: 16 Location: London England
|
|
Posted: Sun Feb 27, 2005 7:54 am |
|
|
Hi Arun
It depends how much resolution you need from the ADC. If 4bits (16) steps is OK you can use the internal Vref generator on the 16f628.
For measuring 5 V ( with a 5V supply) you will need to pot. down the test voltage with say two equal resistors eg 10k. Then start the Vref generator at 0 (see set_vref()) and increase its value one step at a time whilst monitoring the comparator output bit. When it changes state it means that the Vref has just exceeded half the input voltage.
This works well for simple battery voltage testing and I have used it to control the brightness of a LED diplay as a function of ambient light level by connecting a LDR and resistor to the comparator input.
Regards
Terry |
|
|
|