CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Basic ADC question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Basic ADC question
PostPosted: Tue Jul 18, 2006 6:58 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 18, 2006 7:53 pm     Reply with quote

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








PostPosted: Tue Jul 18, 2006 8:17 pm     Reply with quote

thank you
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 19, 2006 6:49 am     Reply with quote

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). Shocked Smile
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Ttelmah
Guest







PostPosted: Wed Jul 19, 2006 7:49 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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