void select_channel(int channel)
{
if ( channel == 1 )
{
ADCON0.CHS0 = 0x1;
ADCON0.CHS2 = 0x0;
ADCON0.CHS1 = 0x0;
ADCON0.CHS3 = 0x0;
}
else if (channel == 8)
{
ADCON0.CHS0 = 0x0;
ADCON0.CHS2 = 0x0;
ADCON0.CHS1 = 0x0;
ADCON0.CHS3 = 0x1;
}
else
{
ADCON0.CHS0 = 0x1;
ADCON0.CHS2 = 0x0;
ADCON0.CHS1 = 0x0;
ADCON0.CHS3 = 0x1;
}
}
unsigned int ADC_read_implementation(int channel){
unsigned int adval = 0;
ADCON2.B7 = 1; //results right justified
ADCON2.B0 = 0;
ADCON2.B1 = 1;
ADCON2.B2 = 1;
ADCON2.B3 = 1;
ADCON2.B4 = 0;
ADCON2.B5 = 1;
ADCON1 = 0x05;
ADCON0.B0 = 1; //turn on ADC
ADCON0.GO_DONE = 1;
while(ADCON0.GO_DONE == 1);
adval = (ADRESH << 8) + ADRESL;
return adval;
}
select_channel(1)
Delay_ms(5);
usb[0] = (char)ADC_read_implementation();
select_channel(8)
Delay_ms(5);
usb[1] = (char)ADC_read_implementation();
select_channel(9)
Delay_ms(5);
usb[2] = (char)ADC_read_implementation();