I have a dilemma.
I need to read ADC values from PIC18F4520. My configuration, and code, is as follows:
#define OSC INTIO67
int adc_result = 0;
void main(void)
{
OSSCON = 0x70; //Set it as 8Mhz.
OSCTUNEbits.PLLEN = 1; //Enable PLL 4x multiplier, thus we have 32 Mhz internal clock.
//Set PORTA direction ports as input
TRISA = 0xFF;
OpenADC( ADC_FOSC_32 &
ADC_RIGHT_JUST &
ADC_4_TAD,
ADC_CH0 &
ADC_REF_VDD_VSS &
ADC_INT_OFF, ADC_5ANA);
while (1) {
SetChanADC(ADC_CH0);
ConvertADC();
while (BusyADC());
adc_result = ReadADC();
}
CloseADC();
}
The issue is this:
- When I run it in MPLAB v8.x, my reading is slightly off, and terminates my debugging.
- I can't debug the PIC again since I get errors like "Cannot enter debug mode."
How do I fix those issues? Also, is my ADC configuration correct (seeing that ADC requires a clock to do ADC conversion) with my internal clock? Is this correct to set the clock signal to 32 MHz.
I am programming using PicKit2 (with the black button).