ep.hobbyiest
Full Member level 4
Hi,
i have configured ADC of pic16f887's PIC but it is not working properly.
I m using XC8 compiler and 4MHz cystal.
i have configured ADC of pic16f887's PIC but it is not working properly.
I m using XC8 compiler and 4MHz cystal.
Code:
void adc_init(void)
{
ANSEL = 0x03;
TRISA = 0x03;// 4 channels selected
ADCON1 = 0x80;// right justified
ADCON0 = 0x01;//FOSC/2 and ADC Enabled
}
uint16_t read_adc(uint8_t por)
{
uint16_t dat;
ADCON0bits.CHS = 0;
ADCON0bits.CHS = por;
ADCON0bits.GO = 1;
__delay_ms(1);
while(ADCON0bits.GO_DONE == 1);
ADCON0bits.GO = 0;
ADCON0bits.CHS =
dat = ADRESL;
dat |= (ADRESH<<8);
return dat;
}