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

reading odd 12bit adc value

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



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

reading odd 12bit adc value
PostPosted: Wed Aug 03, 2011 3:47 pm     Reply with quote

Hi There,

I'm using an pic18f86k22 which features 12bit adcs.
Now in my program, i right align the values like
Code:
  setup_adc_ports (AMBIENT|TEMPFB_OVR|TEMPFB_LPR|POT1|POT2);
  setup_adc (ADC_CLOCK_INTERNAL);
  ADCON2|=0x80;
which seems to work fine for me but the problem I'm having is that i read an adc value of 4095 on an analog input where i instead should be reading a lower value. I however had
Code:
#device adc=16
in my code header. I now changed this to adc=12 - is that gonna resolve the issue? Any ideas or hints? I need to into the temperature chamber before i can test my code which is connected with somewhat lots of efforts. I would like to provide a code that works this time thus any hints or suggestions would be welcome!

As for the electronics on my analog input:
I have a NTC 10k connected to GND and a 10K pullup to +Vcc (+5V), thus the adc value should never be maxed out (unless the ntc falls off)...

Thank you very much!
Ron
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 03, 2011 4:02 pm     Reply with quote

Quote:

setup_adc_ports (AMBIENT|TEMPFB_OVR|TEMPFB_LPR|POT1|POT2);

Only you know what these are.

Quote:

setup_adc (ADC_CLOCK_INTERNAL);

For years, we have said don't do this unless you are doing the A/D
conversion while in sleep mode.

Quote:

ADCON2|=0x80;

No one knows what this is.
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

PostPosted: Wed Aug 03, 2011 4:30 pm     Reply with quote

Sorry, here the defines:
Code:
#define TEMPFB_OVR sAN7
#define TEMPFB_LPR sAN14
#define AMBIENT sAN21
#define POT1 sAN22
#define POT2 sAN23


... For years? Sorry, haven't been using this compiler that long myself. But I found a good explanation here:
ADC_CLOCK_INTERNAL - answer here is 'read the data sheet'. The internal clock is an RC oscillator at 'about' 500KHz. This is asynchronous to the master clock. Problem is that if the CPU clock is running much faster than the ADC clock, you can get varying numbers of cycles of the CPU clock, in each cycle of the ADC clock. Result varying amounts of noise. So the internal RC clock, is _not_ recommended for use above 1MHz CPU clock, _unless_ you stop the CPU (use sleep) while doing the ADC conversion.

That makes sense so i'll be using ADC_CLOCK_DIV_32 - here but that doesn't explain the 4095 that i'm getting at -10C already (NTC goes till - 40C - at the least...)


Code:

#byte ADCON2 = getenv("sfr:ADCON2")

ADCON2|=0x80;


From datasheet:
ADCON2: A/D CONTROL REGISTER 2
bit 7 ADFM:A/D Result Format Select bit
1 = Right justified
0 = Left justified
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 03, 2011 5:10 pm     Reply with quote

Quote:
here but that doesn't explain the 4095 that i'm getting at -10C already (NTC goes till - 40C - at the least...)

Measure the input voltage to the PIC's ADC pin at that temperature
with a voltmeter. What does it show ?

Post the lines of code that setup, read, and display the ADC result.
Also post the declarations of any variables or #define's used in those lines.

Also post your compiler version.
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

PostPosted: Wed Aug 03, 2011 5:43 pm     Reply with quote

Yep,

I'll go down thereto measure on my engineering unit tomorrow morning. Can't do that today anymore...

However, here's some of the code:
Code:
 
#device adc=12 // this used to be adc= 16 at the time the bug appeared
#define TEMPFB_OVR sAN7
#define TEMPFB_LPR sAN14
#define AMBIENT sAN21
#define POT1 sAN22
#define POT2 sAN23
#byte ADCON2 = getenv("sfr:ADCON2")
#define ADCCH_TEMP_LPR 14
#define ADCCH_TEMP_OVR 7

//setup adc ports
  setup_adc_ports (AMBIENT|TEMPFB_OVR|TEMPFB_LPR|POT1|POT2);
  setup_adc (ADC_CLOCK_DIV_32);
  ADCON2|=0x80;

           set_adc_channel (ADCCH_TEMP_LPR) ;
            delay_us (10) ;
           fprintf(PC, "LPR temperature %Ld\r\n",read_adc());
           set_adc_channel (ADCCH_TEMP_OVR) ;
            delay_us (10) ;
           fprintf(PC, "OVR temperature %Ld\r\n",read_adc());


Thanks, for your assistance.

PS: Compiler version: 4.119
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Aug 04, 2011 3:52 am     Reply with quote

If you look at the generated code, you'll realize that #device adc=12 in fact sets right justify, while adc=16 sets left justify. But you're overriding it, so there's no difference. Getting a result of 4095 seems to indicate correct right justify but some other kind of wrong setup or wrong pin connection.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Aug 04, 2011 4:45 am     Reply with quote

You don't appear to be setting the reference voltage up at all. You need to include it in setup_adc_ports.

Code:

setup_adc_ports(AMBIENT|TEMPFB_OVR|TEMPFB_LPR|POT1|POT2|VSS_VDD);


or whatever is correct for your ADC reference wiring. Look at the .h for your PIC device to see what options are available.

RF Developer
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

PostPosted: Thu Aug 04, 2011 9:32 am     Reply with quote

RF_Developer wrote:
You don't appear to be setting the reference voltage up at all. You need to include it in setup_adc_ports.

Code:

setup_adc_ports(AMBIENT|TEMPFB_OVR|TEMPFB_LPR|POT1|POT2|VSS_VDD);


or whatever is correct for your ADC reference wiring. Look at the .h for your PIC device to see what options are available.

RF Developer

I got the following available:
Code:
// Optional Second argument:
#define __VDD           0x00     // Range 0-Vdd
#define VSS_VREF          0x10     // Range 0-VrefH
#define VSS_2V048         0x20     // Range 0-2.048V
#define VSS_4V096         0x30     // Range 0-4.096V
#define VREF_VDD          0x08     // Range VrefL-Vdd
#define VREF_VREF         0x18     // Range VrefL-VrefH
#define VREF_2V048        0x28     // Range VrefL-2.048V
#define VREF_4V096        0x38     // Range Vrefl-4.096V
and it says second argument so i assume i wouldn't OR it to the analog pins but place a comma something like
Code:
setup_adc_ports (AMBIENT|TEMPFB_OVR|TEMPFB_LPR|POT1|POT2, __VDD);
- which i also assume would be the default.., right? (scanned through the datasheet but didn't find it right-away and have to get ready for the temperature chamber with my dev board now...)
cerr



Joined: 10 Feb 2011
Posts: 241
Location: Vancouver, BC

View user's profile Send private message

[SOLVED] reading odd 12bit adc value
PostPosted: Thu Aug 04, 2011 12:23 pm     Reply with quote

Seems like the changes I applied fixed the issue. I think it probably was the wrong
Code:
#device adc=16
directive, you guys agree?

Thanks for the help
RF_Dev, FvM & PCM
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