Issues with analog input on PIC18F27J53

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
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.

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;
  
}
upload_2017-11-19_20-37-57.png
 
Last edited:

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Thanks. That was it. I swear I tried that earlier. Bet I didn't have that delay between channel select and setting GO_DONE. I also had a larger cap before. Reduced it to 27pf.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Why do I get a value of 2 when the analog input is tied to ground? That only equates .006 Volts at my reference point but why not zero? Do I need to calibrate?
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
That was it.

Code:
  ADCON1bits.ADCAL = 1;           // Set calibration
    ADCON0bits.CHS = 10;            // Select channel 10     
    ADCON0bits.GO_DONE = 1;         //Trigger pseudo  conversion   
    while(ADCON0bits.GO_DONE == 1); //Wait for conversion
    ADCON1bits.ADCAL = 0;           // Clear calibration
 

AlbertHall

Joined Jun 4, 2014
12,347
Why do I get a value of 2 when the analog input is tied to ground? That only equates .006 Volts at my reference point but why not zero? Do I need to calibrate?
What is the voltage difference between the point where you connected the ADC input and the chip VSS pin?
Currents flowing in the ground can easily cause that kind of voltage difference.
 

OBW0549

Joined Mar 2, 2015
3,566
Why do I get a value of 2 when the analog input is tied to ground? That only equates .006 Volts at my reference point but why not zero?
The PIC18F27J53 data sheet, in Electrical Characteristics, Table 31-30 on page 539, says (param A06) that the A/D converter can have an offset error as much as ±5 LSBs; this is probably where you're getting that error from.

Be aware that you can also get additional errors if you use the chip's internal Vss/Vdd connections as the source of the A/D's voltage reference, as those voltages can differ significantly from Vss and Vdd outside the chip due to voltage drops in the chip's bond wires and metallization. When I need the best accuracy out of a PIC's A/D converter, I configure the chip to use an external Vref, even if it's only Vss and Vdd. On the PIC18F27J53 this is controlled by bits VCFG1 and VCFG0 in the ADCON0 register (see p. 360 in the data sheet). You lose two pins doing this, but sometimes it's worthwhile.

Do I need to calibrate?
I never count on getting absolute accuracy from A/D converters on microcontrollers, and always use some sort of calibration if accuracy is important.
 
Top