View previous topic :: View next topic |
Author |
Message |
swerve029
Joined: 09 Nov 2004 Posts: 1
|
10 bit ADC on 18F458 |
Posted: Tue Nov 09, 2004 8:21 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
|
Posted: Tue Nov 09, 2004 8:41 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Nov 09, 2004 8:46 am |
|
|
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.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
|