Electronic – reading analogue PIC port

pic

I'm trying to read a number of analogue ports on a PIC16F690. I can read 2 but I can't get it to read the 3rd. ie below, AN1, AN2 work, but AN3 doesn't. Am I doing something wrong with TRISA, ANSEL, ADCON1 or ADCON0? Thanks

TRISA   = 0b00010111;         // all port A:0,1,2,4 as inputs
ANSEL   = 0b11111111;         // RA0->RA1 are Analog
ADCON1  = 0b01010000;         // select ADC clock (500 Khz)

ADCON0  = 0b10000101;         //peripheral 1 - PORT A:1 - AN1
__delay_us(250);
unsigned short nRet;
ADCON0 |= 0x02;             // Start conversion
while(ADCON0 & 0x02)        // wait for conversion
 {
 }
 nRet = ADRESH;
 nRet <<=8;
 nRet += ADRESL;


ADCON0  = 0b10001001;         //peripheral 2 - PORT A:2 - AN2
__delay_us(250);
unsigned short nRet;
ADCON0 |= 0x02;             // Start conversion
while(ADCON0 & 0x02)        // wait for conversion
{
 }
 nRet = ADRESH;
 nRet <<=8;
 nRet += ADRESL;

    ADCON0  = 0b10001101;         //peripheral 2 - PORT A:4 - AN3
    __delay_us(250);
    unsigned short nRet;
    ADCON0 |= 0x02;             // Start conversion

    while(ADCON0 & 0x02)        // wait for conversion
    {
    }
    nRet = ADRESH;
    nRet <<=8;
    nRet += ADRESL;

   return (nRet & 0x3FF);

Best Answer

I assume you're using the internal oscillator.

If you do not have CLKOUT disabled in the configuration (INTOSCIO selected), it won't work, as AN3 is on RA4.

enter image description here