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

PIC 12F1840 ADC problem

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



Joined: 17 Nov 2016
Posts: 7

View user's profile Send private message

PIC 12F1840 ADC problem
PostPosted: Thu Nov 17, 2016 5:03 am     Reply with quote

Hello,

I am working with PIC12F1840. I have a problem with the ADC conversion.
There are 3 ADC channels configured and are not giving expected value for the input voltages.

ADC is configured as below:
Code:
 
/*
setup_oscillator(OSC_16MHZ);
setup_adc(ADC_CLOCK_DIV_16);
setup_adc_ports(sAN3 | sAN2 | sAN0);
*/

int ADCread(int ADCchannel)
{
int ADCvalue;
set_adc_channel(ADCchannel);
READ_ADC(ADC_START_ONLY);
delay_us(20);
ADCvalue = READ_ADC(ADC_READ_ONLY );
return ADCvalue;
}


Here in configuration since i have not given any fixed voltage referance, i think the default referance value is 5V (Vdd).
But Vref Pin (Pin 4) of PIC is supplied with 5V, Is this a problem ?

I have 2.5V at one pin and it is converted to between 230 - 260(d).
and 400mv in another pin which is converted to between 185 - 200(d).
This is inconsistent.

As per my knowledge the ADC conversion is like
Digital value = (Analogue value)*1024/(+Vref--Vref)

Can someone let me know the issue here?

Thank you,
Nirmal
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 17, 2016 6:10 am     Reply with quote

While I don't use that PIC...I would think that this...

setup_oscillator(OSC_16MHZ);

uses an EXTERNAL xtal and 2 caps,though you say you put +5 on the clkout pin, if I'm reading your post correctly.

Normally you add the word 'internal' to get the compiler to select the internal oscillator section.

Check with the CCS manual ( press F11 while projects open) to see the proper syntax.


Also code a 1Hz LED program and confirm the LED blinks at the correct speed. This will confirm the PIC, PCB, etc. are working right !!

Once that does, then, try the ADC program.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19544

View user's profile Send private message

PostPosted: Thu Nov 17, 2016 7:41 am     Reply with quote

His chip has got a 16MHz internal oscillator. The oscillator setting is OK.
His problem is Tacq.

The ADC takes _time_ for the internal capacitor to charge to the voltage on the pin. When you select a new channel, you have to wait for a minimum of Tacq, before triggering an ADC reading. The internal capacitor is connected via an internal resistance (the resistance of the multiplexer, tracks etc..). Tacq, depends on the particular PIC, and the impedance of the source feeding the ADC.

Section 16.4 in the data sheet. For a source with an impedance not exceeding 10KR, there needs to be a minimum of 4.87uSec between selecting a channel and taking the reading. He is starting the reading, then waiting.
Code:

int16 ADCread(int ADCchannel)
{
    set_adc_channel(ADCchannel);
    delay_us(5); //Wait for Tacq
    return read_adc();
}


There is no point is separately starting and reading, unless you are using something like a timer interrupt. Just read the adc, which will trigger and read automatically. Also you can just directly return the value.

Also the ADC returns a 16bit value (assuming #device ADC=10 is selected). An int is only 8bits. The function needs to return an int16.
nirmal1990



Joined: 17 Nov 2016
Posts: 7

View user's profile Send private message

PostPosted: Fri Nov 18, 2016 7:59 am     Reply with quote

Thank you all for the inputs,

The problem was with return type and delay.
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