I'm trying to read an analog input on a PIC18F65J11, and I'm having trouble. I don't think the pin is set up correctly because I'm getting a 0 for the value I read in, and the input is measured to be about 1V (but can get up to 3V). I want to use Here is my code:
#include "p18f65j11.h"
#include <adc.h>
/**CONFIGURATION **************************************************/
/* Device Configuration */
#pragma config XINST = OFF // Turn off extended CPU instructions
#pragma config FOSC = HSPLL // HS oscillator will PLL that multiplies oscillator frequency by 4 (system clock is running at 16 MHz)
#pragma config FOSC2 = ON // System clock selected by FOSC1:FOSC0 (used #config to set FOSC to HSPLL)
#pragma config DEBUG = ON // Background debugger enabled; RB6 and RB7 are dedicated to In-Circuit Debug.
#pragma config WDTEN = OFF // Disable watchdog timer.
#pragma config WDTPS = 8 // Watchdog timer poscaler select bits is 1:8 (not important b/c watchdog timer disabled).
#pragma config STVREN = OFF // Reset on stack overflow/underflow disabled.
#pragma config CP0 = OFF // Program memory is not code-protected
#pragma config IESO = ON // Oscillator Switchover mode disabled.
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled.
#pragma config CCP2MX = DEFAULT // ECCP2/P2A multiplexed with RC1 instead of RE7 pin (doesn't matter b/c not using ECCP, which is pwm output)
int myVoltage;
void main(void)
{
myVoltage = 0;
// Configure clock registers (FOSC is set to HSPLL)
OSCCONbits.SCS1 = 0; // SCS1:SCS0 = 00 means use primary
OSCCONbits.SCS0 = 0; // clock source (CPU divider output) if FOSC2 is on (FOSC2 is indeed on from the #pragma config above)
OSCCONbits.IRCF2 = 1; // IRCF2:IRCF0 = 110 means the internal oscillator is set to 4 MHz
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 1;
OSCCONbits.IDLEN = 0; // on SLEEP instruction, device enters sleep mode instead of idle mode
OSCTUNEbits.PLLEN = 1; // enable PLL (4x multiplier), so system clock is 4 * 4 MHz = 16 MHz
// Basically, I've just set it to use primary oscillator, told it the oscillator is
// 4 MHz, and told it to enable PLL so that my system clock is 16 MHz.
// Initialize Port A
TRISA &= 0; // set Port A as outputs
LATA &= 0; // all pins outputting 0
TRISAbits.RA0 = 1; // set pin AN0 as input to read voltage
while(1) {
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD, 0);
Delay10TCYx( 5 );
ConvertADC(); // initiate conversion of sensor1 @ AN0
while(BusyADC()); // waiting to complete conversion
myVoltage = ReadADC(); // read the result of sensor1 @ AN0
CloseADC();
printf("%i volts \n\r", myVoltage);
}
}
What am I doing wrong or missing? I suspect it's the OpenADC() part that may be wrong. I've got it using Fosc/8, result in least significant bits, 12 Tad, channel 0 (AN0 pin), interrupt off, VDD as the highest limit, and 0 so that all analog pins are analog.