I have a PIC18F1330 running at 8MHz on the internal oscillator, and I'm trying to read analog values on the RA4/T0CKI/AN2/Vref+ pin. Vdd is 3.7 volts, and the pin is connected to 3.3 volts, yet the readings I get are consistently '0'!
Relevant code:
in main:
TRISAbits.TRISA4 = 1; //RA4 = input T0CON = 0x00; /* Shut off timer 0, since it could use T0CKI */
the ADC capture code:
ADCON0bits.ADON = 0; // Turn off ADC ADCON1 = 0b00000100; // Vref=Vdd, AN2 enabled ADCON2 = 0b10111001; // Right-justify, 20 TAD, TOSC=FOSC/8 ADCON0 = 0b00001000; // SEVTEN=0, CH = 2, still off ADCON0bits.ADON = 1; // Turn ADC on ADCON0bits.GO = 1;
while(ADCON0bits.NOT_DONE); //wait for result adc = ReadADC(); //get ADC result itoa(adc, val); putsUSART (val);
Notes:
* ReadADC() is just the normal library function. At first I was strictly using library code to save time, but I've decomposed everything trying to track this problem down. I have also directly verified that ADRESH and ADRESL are both coming up 0. * I'm using the extended instruction set. * I've also tried configuring to read AN0,AN1, and they come up zero as well.
Help! If anyone has ideas, I will try them immediately and post the results.
Thanks in advance!
Updates: Just to be sure this was a problem with my code, I tried another 18F1330, same behavior. I'm using version 3.02 of the C18 compiler, and I noticed that there were some small fixes for this PIC in newer versions. However, I verified that the ADC-related registers all point to the right places. I'm not sure what else to try.
< Message edited by jrpowers -- Jan. 30, 2007 8:06:49 AM >
Easy solution to this one. The data sheet is correct for early silicon, but in later silicon the ADCON1 register was changed. You now need to enable the A/D channels with a 0 or select port functions with a 1.
Thanks, I will give that a try. I hadn't noticed the response here until just now.
This should also solve my latest nagging problem, which is that my software I2C implementation is constantly reading '0'.
I'll post results, but I suspect this will solve my problems. Thank you. By the way, how did you figure this out? Trial-and-error, or are there new data sheets?