shomikc
Member level 4
Hello all,
I can read one analog value in one channel just fine. But using two or more seems to be a problem. It seems the ADRES(ult) register does not get refreshed with the new value from the second channel. I am thinking that capacitor in the ADC is not discharged. Can you please help. The code is as listed below.
I can read one analog value in one channel just fine. But using two or more seems to be a problem. It seems the ADRES(ult) register does not get refreshed with the new value from the second channel. I am thinking that capacitor in the ADC is not discharged. Can you please help. The code is as listed below.
Code:
void ADC_Init();
unsigned int ADC_Read(unsigned int);
void main()
{
int a, b;
float voltage;
TRISBbits.TRISB3 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB1 = 0;
PORTB = 0;
OSCCON = 0x72; // set internal oscillator at 8 MHz
while(1)
{
ADC_Init();
ADCON0 = 0b00001101; // select channel1(AN3) and power on A/D Module (ADON =1)
a = ADC_Read(3);
ADCON0 = 0b00010101; // select channel1(AN5) and power on A/D Module (ADON =1)
b = ADC_Read(5); // Read another channel
if (a > b)
{
PORTBbits.RB1 = 1;
}
else
{
PORTBbits.RB1 = 0;
}
}
void ADC_Init()
{
TRISA = 0xFF; // initialise PortA as input Port
PORTA = 0;
ADCON2 = 0b10111110; // 1 Right justified; 010 == 20*T_ad; 010 = F_osc/64
ADCON1 = 0b00000101; // AN0 and AN1 set as Analog Inputs
ADRESH = 0;
ADRESL = 0;
}
unsigned int ADC_Read(unsigned int channel)
{
unsigned int d;
ADCON0bits.GO = 1; // Set the GO
while(ADCON0bits.GO == 1)
{
d = (ADRESH*256)|(ADRESL);
}
return(d);
}