ADC in PIC18F__K80 is quite similar to ADC in many other 8-bit PIC microcontrollers,
( except _K83, _K42, K40 and _Q__ devices with ADCC peripheral).
There have been numerous tutorials, example codes, and threads about ADC use.
Note that control register layout may be different between different families of devices,
so pay attention to datasheet details of register and field names.
There is also MCC Example code generator, to help making you confused.
Basic operation is:
1: Set up control registers.
2: Set ADCON0bits.ADON = 1
3: Wait a suitable time
4: Change channel selection in CHS field, and CHSN field if needed.
5: Wait a suitable time for sampling capacitor to charge.
6: Set ADCON0bits.GO = 1; // Go/Done bit, Do not change any other bits.
7: Wait until GO/DONE bit go back to zero by itself
8: Read ADRESH and ADRESL registers.
9: Go back to 4:
There are many other tricks.
Yes, Pins that may be used for Analog inputs, are configured for analog use when microcontroller is Reset.
This is controlled by ANCON0 and ANCON1 registers. There is one bit for each analog Channel.
You may use:
ANCON0bits.ANSEL0 = 1; /* ADC channel 0 Analog mode */
ANCON0bits.ANSEL1 = 1; /* ADC channel 1 Analog mode */
ANCON0bits.ANSEL2 = 0; /* ADC channel 2 is used for Digital Input */
ANCON0bits.ANSEL3 = 0;
ANCON0bits.ANSEL4 = 0; /* This is PORT pin RA5 on PIC18F__K80 */
Note that if there is something like: ANCON0 = 0b0000000; somewhere else in the code,
then it may destroy the setup shown above.
There is no simple relation between analog Channel numbers, and Port Pin numbers,
so read the datasheet carefully.
post edited by Mysil - 2020/10/23 23:46:33