This is how I connected the circuit on a breadboard :
What I had in mind to display the results of the ADC is to have 5 LEDs and the higher the input voltage, the more LEDs will light up. I have a 10k potentiometer to vary the input voltage going into PORTA,0 which is also Analogue Channel 0.
For eagled eyed viewers, you might notice that I have another resistor (550 ohms) before the 10k pot and I’ve done that to prevent excessive current from going into PORTA,0 when the pot has 0 resistance. This will indeed affect the max voltage going into PORTA,0 and it’s no longer 5V so I should set a different +Vref value. But I couldn’t be bothered because the voltage drop on the 550 ohms resistor is just 0.26V. I don’t need accuracy here anyway.
So let’s get to the initialization.
TRISA,0 is set to allow input for Analogue Channel 0
TRISD is cleared to allow output to LEDs
ADCON1 is cleared. Result is right justified because I’ll just take the higher 8 bits for simpler calculation. This will not affect the accuracy much. I wanted clock conversion to be Fosc/8 to get > 1.6us of minimum conversion time (I’m using a 4MHz crystal here). AN0 to AN7 is set to Analogue mode. +Vref is 5V and -Vref is 0V.
ADCON0 is set to ‘0100 0000’ to select Fosc/8 and Analogue Channel 0.
That’s about it. And the rest of the code can be seen below.
The video below will show how the code works.