1
votes

I'm using a PIC18f26k22 to simply read two potentiometers (connects to analog pin AN0 and AN1). Working with a single pot is easy but more than one pot requires a bit-shifting technique which I haven't understood clearly. I did look around the internet and found an ADC_Read() function. I made some changes to the code so that I could use it for PIC18F26K22.

The problem is that even though I use that function in main, only the ADC channel AN0 works but the AN1 channel doesn't respond (i.e. it won't toggle LEDs). unsigned int ADC_Read (unsigned char channel). In the main function int 'num' and 'den' are used to read each analog input AN0 and AN1, respectively. The only response that I get is from num (AN0).

unsigned int ADC_Read(unsigned char channel)
{
 if(channel > 7)              //Channel range is 0 ~ 7
 return 0;

 ADCON0 &= 0b11000000;              //Clearing channel selection bits
 ADCON0 |= channel<<2;        //Setting channel selection bits
 ADCON2bits.ACQT = 0b001;        // 2 Aquisition Time
 GO_nDONE = 1;                //Initializes A/D conversion
 while(GO_nDONE);             //Waiting for conversion to complete
 return ((ADRESH<<8)+ADRESL); //Return result
 }
1
Is AN1 set as analog input?GJ.

1 Answers

1
votes

enter image description here The ADON bit off the ADC is in bit 0 of the ADCON0 register so you will switch off your ADC here:

ADCON0 &= 0b11000000;              //Clearing channel selection bits AND ADON

change it to:

ADCON0 &= 0b10000011;              //Clearing channel selection bits

This will only reset the cannel bits. Know you are able to select a new channel.

ADCON0 |= channel<<2;        //Setting channel selection bits