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

10 bit ADC on 18F458

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



Joined: 09 Nov 2004
Posts: 1

View user's profile Send private message

10 bit ADC on 18F458
PostPosted: Tue Nov 09, 2004 8:21 am     Reply with quote

I have been pulling my hair out as of late in attempt to get 10 bit precision from the ADC. I have put the #DEVICE ADC=10 in the 18f458.h file, but I still only get 8 bits of precision.

Code:
//////// Standard Header file for the PIC18F458 device ////////////////
#device PIC18F458 ADC=10


Code:



void main()
{

     setup_adc(ADC_CLOCK_INTERNAL);      // set port A to analog
     setup_adc_ports(ALL_ANALOG);


     while(true){
             output_high(PIN_B1);
          getTempK(0);
            output_low(PIN_B1);
            delay_ms(500);
     }
}

float getTempK(int ch) {
   long value;
   float tempK, voltage;   

   set_adc_channel(ch);            // select the required channel
   delay_us(50);

   value = read_adc();
   printf("Voltage increment: %u \r\n", value);
   //convert to voltage in V
   voltage = (float)value*(5.0/256.0); //Vref/resolution

   // Calculate the temperature in K
   tempK = voltage*100.0;

   return tempK;
}


If anyone could offer some advise to help gain the 10 bits of precision that I require, I would be very thankful.

Thanks
valemike
Guest







PostPosted: Tue Nov 09, 2004 8:41 am     Reply with quote

I didn't try out your code, but it looks like you should change:

5.0/256.0 to

5.0/1024.0

Remember: 0xFF is 255 (8-bits), while 0x03FF (10 bits) is 1023.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Nov 09, 2004 8:46 am     Reply with quote

Remember that the result registers (there are two of them, combined) ADRESH and ADRESL are left justified by default. The lower six bits, 0 - 5, will be filled with zeros and the result will be in the upper 10 bits. If you want to have the value stored in the lower section then you will need to set ADFM to a one(1). This will affect the value that you read from the registers.

I hope most of your hair grows back. Wink

Ronald
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