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 support@ccsinfo.com

Problem reading two AD channels

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



Joined: 12 Oct 2010
Posts: 12
Location: South Africa

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

Problem reading two AD channels
PostPosted: Wed Oct 27, 2010 1:18 pm     Reply with quote

Hi I have a problem reading two AD channels.
The value of the next channel is very next to the first channel.
There is the code:
Code:

delay_us(100);
setup_port_a(RA0_ANALOG);
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel(0);
read_adc(ADC_START_ONLY);
valuedisp=(float)read_adc();
setup_adc( ADC_OFF );

delay_us(100);
setup_port_a(ALL_ANALOG);
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel(1);
read_adc(ADC_START_ONLY);
outputvolts1=(float)read_adc();
setup_adc( ADC_OFF );

_________________
Regards,
Brandon
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Oct 27, 2010 1:51 pm     Reply with quote

Internally the ADC value is read from a capacitor.
The input inside the chip, and the multiplexer in particular have resistance.
The capacitor takes _time_ to charge.
This is in the PIC data sheet. ADC section.

You need to select the channel, then _wait_ for the capacitor to charge. Then take the reading. The wait duration will depend on the PIC involved, read the data sheet.
Your 'start only' ADC lines are pointless. These are only wanted when you use the 'read only ' option once the chip has performed the conversion.
So the sequence needs to be:

1) select channel
2) wait for the acquisition time.
3) read the adc.

Also check whether ADC_CLOCK_INTERNAL is legal for your clock rate?. Normally not if you are operating above 1MHz, unless you put the PIC to sleep for the conversion.
To do this, you need to enable the ADC interrupt, disable global interrupts, and all other interrupts, set read_adc(ADC_START_ONLY), then immediately put the chip to sleep. When it wakes, use ADC_READ_ONLY to get the value and set the interrupts back to normal.

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