I have tried everything I can think of to try to isolate what is causing this strange ADC error I am seeing and I narrowed down the cause, but I can’t figure out Why. I can really use some help with this.
I am trying to build a cable tester (Continuity and resistance) by using all 28 Analog pins to test for connections against each pin one at a time. To accomplish this I use two loops: The outer loop sets up one of the pins as a digital output and sets the voltage high, while setting the remaining 27 pins as Analog inputs. The Inside loop then tests each analog pin (excluding the one set to digital) using the ADCIN command, and stores the results in an array. If the digital pin being tested is wired to any of the analog pins it will record the value showing the connection. The outside loop then sets the digital pin low, and then sets up the TRISx and ANSELx registers to proceed to testing the next pin until all pins have been tested against each other. This sounds like a simple thing to accomplish, and it does work to a point, but there is an error once the outside loop reaches AN16 (RC4 set to digital) And the inside loop is >= 16. At that point the ADCIN command starts reporting connections that don’t exist. They are always the same and follow a pattern.
AN16 show short with AN0, AN17 shows shorted to AN1, AN18 Shorted to AN2,… and so on.
Now if I leave the Outside loop alone (0 to 27) setting up each pin to digital one at a time and testing against each other pin the same as before, but this time I set the inside loop from 0 to 15 instead, I can test all 28 pin (one at a time as digital output) against the first 16 Analog pins (1-15) without error and I can repeat this as many times as I like. Once I do a simple ADCIN read of any Analog pin higher than AN15 (Even if I don’t set any pin to digital/High), then try to run the same routing again it shows the shorts again, and I then need to cut power to the micro to get valid results again.
The ADCIN read of pins 16-27 seems to change something. I am using the following defines:
DEFINE ADC_BITS 8, DEFINE ADC_CLOCK 3, DEFINE ADC_SAMPLEUS 50
I am not sure if setting the ADC Bits to 8 is valid for the 18F46k22, but it seems to work correctly when reading values from a trim port. One more thing to note is all Analog pins are tied to ground with a 10k resistor. If there are any ADC experts out there that would be willing to look at my code, I would be happy to send it to you (The code might be able to explain what I am doing better).

Thanks,
Jim