MPLAB X red squiggles *LIE*! Try building the code. If the compiler gives any errors you have a real problem, otherwise its just MPLAB X being stupid. There was a specific problem with older versions of MPLAB X and the XC8 compiler, where the toolsuite was passing different options to the Netbeans syntax checker/hilter than those it put in the makefile so the syntax checker never saw the #defines for the delay macros in pic.h so red squiggled every occurrence even though the compiler had no problem using them.
The clock division factor is set by various ADCS bits in ADCON0 and ADCON1. Most newer devices have then grouped in a single register, but the old ones with the original 10 bit ADC are a P.I.T.A that way. Actually, arguably they are named wrong in the datasheet as ADCS2 is the LSB!
WTF were Microchip smoking at the time ?!? The actual clock divisor used is 2
(ADCS1 ADCS0 ADCS2)+1, apart from (11 x) which are the Frc clock.
I'll be expressing them with ADCS2 as the LSB henceforth. You've got them defined in two magic numbers:
ADCON0 = 0x41; //ADC Module Turned ON and Clock is selected
ADCON1 = 0xC0; //All pins as Analog Input With reference voltages VDD and VSS
If you expressed them in binary and provided a key, you'd know what was going on:
// ADCON0: ADCS1 ADCS0 CHS2 CHS1 CHS0 GO/DONE --x-- ADON
ADCON0 = 0b01000001; //ADC Module Turned ON and Clock is selected
// ADCON1: ADFM ADCS2 --x-- --x-- PCFG3 PCFG2 PCFG1 PCFG0
ADCON1 = 0x11000000; //All pins as Analog Input With reference voltages VDD and VSS
So its obvious that the ADC ADCS bits are set to
(00 1) (01 1), which TABLE 11-1 in the datasheet tells us is
16 Tosc.4 Tosc and is invalid[ for Fosc>2.5MHz! You would need 8 Tosc for a Tad of 2us (
must be >1.6us) so
will need to could change the ADCS bits accordingly (to
0 01)
as faster conversions are generally preferable if you have a lot of other processing and are busy-waiting for the ADC result. N.B. Grossly excessive Tad can cause loss of accuracy due to charge leakage. however 4ms is within the acceptable range.Here's how I'd handle it, bearing in mind my comment about their order:
PIE1bits.ADIE=0; // not interrupt driven
ADCON1bits.PCFG=0b0000; // All analog, internal refs. See DS39582B-page 128.
ADCON0bits.ADCS1=0; // ADCS=(01 0), Tad=8*Tosc (2us @Fosc=4MHz)
ADCON0bits.ADCS0=1;
ADCON1bits.ADCS2=0; // Datasheet WTF: ADCS2 is LSB !!!
ADCON1bits.ADFM=1; // Right Justified
ADCON0bits.ADON=1; // *FINALLY* enable A2D converter
Magic numbers in your code are *BAD*. Used once and properly documented they are forgiveable but without that documentation they are *EVIL*. I wouldn't be surprised to find four hex digits you didn't document have wasted nearly two weeks of your time and $16 of your money.
@StillTrying,
I make them Fosc/64, = the value for a 20M osc.
I don't think so! You obviously misread the *EVIL* *MAGIC* *NUMBERS* !
However you can be forgiven as its not your code, and I misread them myself earlier in this topic when I checked the O.P's
code attached to reply #28.
Edit: Thanks for the correction by Still trying - I misread the Tad setting yet again inspite of showing correctly how to decode it. Dyslexia and Binary is a bad combo!