|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Basic ADC question |
Posted: Tue Jul 18, 2006 6:58 pm |
|
|
I'm using a 18F452 reading a battery volt from channel 0 (PIN2)
I want to use just one pin, I have no free pins for external reference.
what is the default reference used ? or how can I set an internal reference?
Thank you
Code: |
void InitADC_INTERNAL()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
}
int16 ReadADC_INTERNAL(int channel) {
set_adc_channel(channel);
return read_adc(ADC_START_AND_READ);
}
|
|
|
|
epideath
Joined: 07 Jun 2006 Posts: 47
|
|
Posted: Tue Jul 18, 2006 7:53 pm |
|
|
You have the setting already to use the internal reference. it is the
setup_adc_ports(AN0);
the range is the same as the power supply to the device. So if it is powered by +5v to ground then it will range from 0-5v
regards |
|
|
Guest
|
|
Posted: Tue Jul 18, 2006 8:17 pm |
|
|
thank you |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Wed Jul 19, 2006 6:49 am |
|
|
OK, the simple answer was given.
But, keep in mind that the PIC's ADC inputs are limited (functionally) to VrefLow and VrefHigh (0 to Vcc in the first answer given). And their tolerance is only about 0.3V above and below the ground and Vcc of the chip. So if your battery voltage is outside this range, you need to attenuate (or shift or invert or some combination) it before sampling.
If you do attenutate the voltage, the next thing to keep in mind is the input impedance of the ADC and mux. Read the data sheet, read it again then read it again. Also browse the forum archives.
Next, think for a moment about what happens if you are powering Vcc directly from the battery and using Vcc as your reference.
Post some more specifics about your battery and method of powering the PIC if you have more questions (and I bet you do). _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Ttelmah Guest
|
|
Posted: Wed Jul 19, 2006 7:49 am |
|
|
As a seperate comment, on the 'example' given, you must remember that the ADC, has _two_ processes that take time. The first is that the internal capacitor used to read the sample, _must_ be allowed time to charge to within the required 'error' margin of the real voltage. Then the second is the time taken to perform the ADC read operation. The 'read_adc' function automatically waits for the ADC read operation, but in the example, no time at all is being allowed for the capacitor to charge. This is why you will see code like:
[code]
set_adc_channel(0);
delay_us(10);
x=read_adc();
[code]
The delay, is the time needed to let the capacitor charge after selecting the channel.
Now in your situation of wanting to read just one channel, it is much safer/easier, to include the channel selection, in the ADC initialisation, which then allows time for the capacitor to charge.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|