Hello everyone,
I'm having a great deal of difficulties correctly configuring the ADC with this microcontroller. The PIC18 is embedded into a prototype board which limits my ANx pin usage to AN14, AN12, and a few others. I have tried supplying varying voltages to pin AN12 (the one I chose to use) and my circuit resistance is roughly 4.6K Ohms.
After scanning many forums as well as thoroughly reading the document sheet, I have been left with the code attached below
The output that I receive is a random numbers in the range of 0x10 - 0x20 with or without a voltage supplied. At one point, I was reading all zeros instead of random numbers.
I feel as though I am either missing a key step in this configuration, or have a problem hidden within the code I did not include. The board is also configured with a radio, however I did check the configurations and none of them overlapped with my configurations.
I would greatly appreciate any advice as to where to look for an issue, or what I may be forgetting to do!
I'm having a great deal of difficulties correctly configuring the ADC with this microcontroller. The PIC18 is embedded into a prototype board which limits my ANx pin usage to AN14, AN12, and a few others. I have tried supplying varying voltages to pin AN12 (the one I chose to use) and my circuit resistance is roughly 4.6K Ohms.
After scanning many forums as well as thoroughly reading the document sheet, I have been left with the code attached below
The output that I receive is a random numbers in the range of 0x10 - 0x20 with or without a voltage supplied. At one point, I was reading all zeros instead of random numbers.
I feel as though I am either missing a key step in this configuration, or have a problem hidden within the code I did not include. The board is also configured with a radio, however I did check the configurations and none of them overlapped with my configurations.
I would greatly appreciate any advice as to where to look for an issue, or what I may be forgetting to do!
Code:
#pragma config FOSC = INTIO67, PLLCFG = ON, PRICLKEN = ON, FCMEN = OFF
#pragma config PBADEN = ON, IESO = OFF, PWRTEN = ON, MCLRE = EXTMCLR
#pragma config BOREN = OFF, HFOFST = OFF, T3CMX = PORTC0, STVREN = ON, WRTC = ON, WRTD = OFF
#pragma config WDTEN = SWON, WDTPS = 8192, LVP = OFF, DEBUG = OFF
....
OSCCON = 0x70; //Configure oscillator
OSCCON2 = 0x84; //set to Fcy = 64 MHz, Fosc = 16 MHz
OSCTUNE = 0x40; //??
PORTBbits.AN12 = 1; //Enable AN12??
TRISBbits.RB0 = 1; // Enable RB0 as input
ANSELBbits.ANSB0 = 1; // Enable pin as analog
ADCON0bits.CHS = 0b01100; //Setting the Channel as AN12
ADCON1bits.NVCFG = 0; // Using Vdd
ADCON1bits.PVCFG = 0; // Using Vss
ADCON2bits.ADCS = 0b101; // Setting clock speed
ADCON2bits.ACQT = 0b101; // Setting aquisition time
ADCON2bits.ADFM = 1; // Right Justify
ADCON0bits.ADON = 1; /* A/D Enabled */
....
ADCON0bits.GO=1; // Start Conversion
while (ADCON0bits.GO==1); //Polling until done
value = ADRESL; // Store the value into uint8 Data Type
....