I am having problems getting correct voltage readings on the PIC18F27J53 analog input.
If I tie my analog input to ground using a pull down resistor. I consistently get a a value of 64 (decimal) in ADRES. I would expect 0.
If I tie the analog input to VDD, I consistently get 65,472 in ADRES. With 10 bit ADC, I would expect 1023 at full scale.
I suspect it might be a problem with my aquestion setup as I find the datasheet as confusing as hell on that subject.
My reference is VDD/VSS. I am using a 10 bit ADC.
If I tie my analog input to ground using a pull down resistor. I consistently get a a value of 64 (decimal) in ADRES. I would expect 0.
If I tie the analog input to VDD, I consistently get 65,472 in ADRES. With 10 bit ADC, I would expect 1023 at full scale.
I suspect it might be a problem with my aquestion setup as I find the datasheet as confusing as hell on that subject.
My reference is VDD/VSS. I am using a 10 bit ADC.
Code:
void batteryMonitor_Init()
{
// Load control setup
ANCON1bits.PCFG12 = 1; // Load control is digital
TRISBbits.TRISB0 = 0; // Set load control pin as an output
// Battery sense setup
ANCON1bits.PCFG10 =0; // Battery sense is analog
TRISBbits.TRISB1 = 1; // Battery sense is an input
ADCON0bits.VCFG0 = 0; // Use VSS as VRef-
ADCON0bits.VCFG1 = 0; // Use VDD as Vref+
ADCON1bits.ACQT =0; // Set acquisition of 0
ADCON1bits.ADCS = 0; // Set A/D Conversion Clock of F0SC/2
ADCON0bits.ADON = 1; // Turn on the ADC
}
Code:
unsigned batteryMonitor_GetVoltage(unsigned char chkWithLoad)
{
if (chkWithLoad)
{
LATBbits.LATB0 = 1; // Turn on the load
__delay_ms(2); // Delay for a bit
}
ADCON0bits.CHS = 10; // Select channel 10
__delay_ms(100);
ADCON0bits.GO_DONE = 1; //Trigger the conversion
while(ADCON0bits.GO_DONE); //Wait for conversion
unsigned int v = ADRES;
LATBbits.LATB0 = 0; // Turn off the load
return v;
}
Last edited: