Question

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. i.e. below, AN1, AN2 work, but AN3 doesn't. Am I doing something wrong with TRISA, ANSEL, ADCON1 or ADCON0?

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);
Était-ce utile?

La solution

What is your clock mode ?

Looking at the datasheet, section Clock Mode or p176, configuration bits, at the CONFIG register, you may choose between different clock mode.

http://ww1.microchip.com/downloads/en/DeviceDoc/41262A.pdf

You may be using the INTOSC mode (CONFIG.FOSC2-0=101).

 "CLKOUT function on RA4/AN3/T1G/OSC2/CLKOUT pin, I/O function on RA5/T1CKI/OSC1/CLKIN". 

You could use the INTOSCIO instead (CONFIG.FOSC2-0=100)

   "I/O function on RA4/AN3/T1G/OSC2/CLKOUT pin, I/O function on RA5/T1CKI/OSC1/CLKIN"

Same thing for RC -> RCIO mode (CONFIG.FOSC2-0=111 -> CONFIG.FOSC2-0=110).

It is likely that the CLKOUT mode has the right of way over the A/D mode. Try to change the clock mode !

Bye,

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top