PIC 18F4550 come with ADC in PORT A which reads analog input such as:
1. voltage
2. temperature
3. light intensity
4. resistance
5. moisture
6. humidity etc
Analog signal is converted into digital form by referring to reference voltage which can be set to Vdd (same as source voltage) or any other external reference voltage source. ADC
Module has high-voltage reference (Vref+) and low-voltage reference (Vref-). The
reference voltage value can be chose to be Vdd, VSS, RA2 or RA3. ADC Module from
Channel 0-7 (AN0-AN7) will read the voltage value detected from the chosen
channel.
Table below shows how to configure PORT A to either digital or analog I/O.
For example: configure only AN0 of PORT A as analog I/O while others are digital. An analog signal is send to AN0. The code should look like below:
ADCON0 = 0B00000001 //choose channel AN0
ADCON1 = 0B00001110 // only AN0 is analog
**ADON = turn ON ADC
**GO/DONE = conversion status
For PIC18, it is difference compare to PIC16 where PIC18 need to configure ADCON2.
ADCON2 = 0B10001010 is use for 22pF ceramic capacitor pair and 20MHz-40Mhz crystal.
1. Configure the A/D module:
• Configure analog pins, voltage reference and digital I/O (ADCON1)
• Select A/D input channel (ADCON0)
• Select A/D acquisition time (ADCON2)
• Select A/D conversion clock (ADCON2)
• Turn on A/D module (ADCON0)
2. Configure A/D interrupt (if desired):
• Clear ADIF bit
• Set ADIE bit
• Set GIE bit
3. Wait the required acquisition time (if required).
4. Start conversion:
• Set GO/DONE bit (ADCON0 register)
5. Wait for A/D conversion to complete, by either:
• Polling for the GO/DONE bit to be cleared
OR
• Waiting for the A/D interrupt
6. Read A/D Result registers (ADRESH:ADRESL); clear bit ADIF, if required.
7. For next conversion, go to step 1 or step 2, as required. The A/D conversion time per bit is defined as TAD. A minimum wait of 3 TAD is required before the next acquisition starts.