Skip to content

Analog-to-Digital Converter (ADC)

The PIC18F4550 has an integrated 10-bit ADC with 13 analog channels (AN0–AN12). It converts an analog voltage (potentiometer, LM35, resistor divider) into a digital number your program reads from ADRESH:ADRESL.

This page completes Topic 9 ADC: ADCON0/1/2 registers, conversion flow, and links to GPIO and config bits. The official lab is Practice 7 — A/D converter.

Analog channel map AN0 to AN12 on ports A, B and E
13 ADC channels. RA4 is not analog. — Topic 9 ADC — based on PIC18F4550 datasheet
Figure 9.1: basic functions in a measurement system
Sensor → conditioning → ADC → processor. The ADC digitizes the analog signal. — Topic 9 — A/D Converter · UNEXPO
Figures 9.2 and 9.3: signal conditioning and ADC transfer characteristic
Conditioner before ADC and quantization curve (successive approximation). — Topic 9 — A/D Converter · UNEXPO

The A/D converter measures voltage on an analog pin and represents it as integer 0–1023 (10 bits).

Sensor (potentiometer, LM35…)
↓ voltage 0 V – Vref (typically 5 V)
PIC ANx pin (RA0, RE0…)
↓ ADC module (successive approximation)
ADRESH:ADRESL → value 0..1023 in your program
ConceptPIC18F4550 value
Resolution10 bits → 1024 levels (0 to 1023)
1 LSBVref / 1024 ≈ 4.88 mV with Vref = 5 V
Estimated voltageV ≈ (ADRES / 1023) × Vref

Example: ADRES = 512 → V ≈ 2.5 V with Vref = 5 V.

RegisterMain function
ADCON1Which pins are analog vs digital (PCFG)
ADCON2Conversion clock (ADCS), format (ADFM), acquisition time
ADCON0Channel (CHS), on (ADON), start/done (GO/DONE)

Configure ADCON1/2 once at startup; each read touches ADCON0 (channel + GO).

Step 4 — Relation to GPIO and config bits

Section titled “Step 4 — Relation to GPIO and config bits”
  • TRISx — ANx pin must be input.
  • ADCON1 — declares pin as analog; rest of port can stay digital (LCD, LEDs).
  • PBADEN = OFF in CONFIG — avoids RA/RB starting as analog unintentionally (Config bits).
PortAnalog pinsNotes
ARA0/AN0, RA1/AN1, RA2/AN2, RA3/AN3, RA5/AN4RA4 is not analog (T0CKI digital)
BRB0/AN12, RB1/AN10, RB2/AN8, RB3/AN9, RB4/AN11Share port with GPIO and interrupts
ERE0/AN5, RE1/AN6, RE2/AN7Common when LCD uses RE

Practice 7: potentiometer on AN0 (RA0); LCD and control pins digital via ADCON1.

FieldFunction
CHS3:CHS0Selects channel AN0–AN12
ADON1 = ADC module on
GO/DONE1 = conversion running; 0 = finished

Read flow:

BSF ADCON0, GO ; start conversion
Wait GO = 0 ; hardware done
Read ADRESH:ADRESL

PCFG in ADCON1 defines ANx vs digital GPIO. Typical Practice 7AN0 only analog:

MOVLW 0x0E ; RA0 analog, rest digital
MOVWF ADCON1

GPIO-only labs (Practice 1, keyboard LCD) often use ADCON1 = 0x0F for all pins digital.

FieldFunction
ADCS2:0Conversion clock (depends on Fosc)
ADFM0 = right justified; 1 = left justified
ACQT2:0Acquisition time (sample & hold)

20 MHz crystal (UNEXPO course): ADCS = 101 in ADCON2. Reference code Practice 7:

MOVLW B'10100010' ; ADFM=1, ADCS=101
MOVWF ADCON2
MOVLW B'00000001' ; channel AN0, ADON=1
MOVWF ADCON0
BSF TRISA, 0 ; RA0 input

Full walkthrough — read AN0 (potentiometer)

Section titled “Full walkthrough — read AN0 (potentiometer)”

Subroutines in sandwich style:

config_adc:
MOVLW 0x0E
MOVWF ADCON1
MOVLW B'10100010'
MOVWF ADCON2
MOVLW B'00000001'
MOVWF ADCON0
BSF TRISA, 0
RETURN
read_an0:
BSF ADCON0, GO ; start conversion
wait_adc:
BTFSC ADCON0, GO ; skip if GO=0 (done)
GOTO wait_adc ; jump → loop until finished
MOVF ADRESL, W ; read result (per ADFM)
MOVWF adc_l
MOVF ADRESH, W
MOVWF adc_h
RETURN

From programa:: CALL config_adc once; in loop CALL read_an0 and display on LCD or convert to voltage.

You can use ADIE in PIE1 + PEIE + GIE to read the result in an ISR when ADIF sets — same chain as Timer1. Practice 7 usually uses polling on GO/DONE.

MistakeSymptom
Skip ADCON1Erratic reads or LCD pins affected
Pin as output (TRIS=0)ADC does not measure correctly
GO without ADONConversion never starts
Wrong ADCS for FoscSlow or inaccurate conversion
Treat RA4 as analogAN4 is on RA5, not RA4

Practica · Laboratory

Practice 7 — A/D converter

Read a potentiometer or sensor on AN0, convert the value and show on LCD or LEDs. 20 MHz crystal: ADCS = 101 in ADCON2. Full tutorial: Practice 7.

  • PWM / CCP — another Topic 9 module (analog-like output via duty cycle)
  • UART — send ADC value to PC (Practice 8)
  • Partial IV — ADC + serial review

How many bits does the PIC18F4550 ADC have?

10 bits → values 0–1023.

What does BSF ADCON0, GO do?

Starts a conversion; the bit stays 1 until done (GO/DONE = 0).

What is ADCON1 for?

Configures which pins are analog input vs digital GPIO.

Which PORTA pin is NOT analog?

RA4 — it is T0CKI (Timer0 digital input).

Ejercicio · Parcial

Calculate voltage

If Vref = 5 V and ADRES = 768, approximately how many volts? (768/1023 × 5 V)

Interactive exam

A/D converter

Based on: Tema 9. CAD.pdf

0 of 0 answered

The PIC18F4550 ADC resolution is...
ADCON0 mainly selects...
ADCON1 with PCFG is mainly used to...
GO/DONE in ADCON0 during conversion...
With 20 MHz crystal, ADCS in ADCON2 is usually...
Maximum digital value of a 10-bit ADC is...
RA4 on PORTA...
Typical Practice 7: potentiometer on...