Lessons
2012 Microchip Technology Inc. DS41628B-page 55
3.5 LESSON 4: ANALOG-TO-DIGITAL CONVERSION
3.5.1 Introduction
This lesson shows how to configure the ADC, run a conversion, read the analog voltage
controlled by the potentiometer (RP1) on the board, and display the high order four bits
on the display.
3.5.2 Hardware Effects
The top four MSbs of the ADC are mirrored onto the LEDs. Rotate the potentiometer
to change the display.
3.5.3 Summary
Both PIC devices have an on-board Analog-to-Digital Converter (ADC) with 10 bits of
resolution on any of 11 channels. The converter can be referenced to the device’s V
DD
or an external voltage reference. The lesson references it to V
DD. The result from the
ADC is represented by a ratio of the voltage to the reference.
EQUATION 3-2:
Here’s the checklist for this lesson:
1. Configure the ADC pin as an analog input.
2. Select clock scaling.
3. Select channel, result justification, and V
REF source.
3.5.4 New Registers
3.5.4.1 BOTH
3.5.4.1.1 ANSEL:
The ANSEL register determines whether the pin is a digital (1 or 0) or analog (varying
voltage) I/O. I/O pins configured as analog input have their digital input detectors dis-
abled and, therefore always read ‘0’ and allow analog functions on the pin to operate
correctly. The state of the ANSELX bits have no effect on digital output functions. When
setting a pin to an analog input, the corresponding TRIS bit must be set to Input mode
in order to allow external control of the voltage on the pin.
This lesson sets RA4 as an analog input, since the POT will vary the voltage.
TABLE 3-14: NEW REGISTERS FOR BOTH DEVICES
Register Purpose
ANSELX
Determines if the pin is digital or analog.
ADCON0
Selects ADC channel – Enables module – Contains the ‘I’m done with conversion” bit
ADC = (V/VREF) * 1023
Converting the answer from the ADC back to voltage requires solving for V.
V = (ADC/1023) * VREF