Datasheet

PICkit™ 3 Debug Express Lessons
© 2009 Microchip Technology Inc. DS41370C-page 45
3.7 LESSON 7: ANALOG-TO-DIGITAL CONVERTER (ADC)
Lesson 7 builds on the previous lesson by using the on-chip ADC to read the demo
board potentiometer voltage. The result is used to vary the LED rotation time delay so
that the potentiometer controls the LED rotation speed.
3.7.1 PIC18F45K20 ADC Basics
Simply put, an ADC takes the ratio of an input voltage to a reference voltage and
represents it as a number. This number is dependent on the bits of resolution of the
ADC. For example, the 10-bit resolution of the PIC18F45K20 ADC means that 1024
numbers from 0-1023 are available to represent the voltage ratio. In mathematical
terms,
ADC Value = (V
IN/VREF) * 1023
If V
IN = 2.5Volts, and VREF = 5.0Volts, then the ADC Value is (2.5/5)*1023 = 511. This
makes sense in that V
IN is half of VREF, so the ADC value is half of 1023.
Knowing the reference voltage and solving the equation for V
IN allows the ADC Value
to be converted back into a voltage:
V
IN = (ADC Value/1023) * VREF
The PIC18F45K20 ADC may be referenced to the device VDD voltage or an external
voltage reference. In this lesson, the ADC is referenced to the PIC18F45K20 Starter Kit
Demo Board V
DD, which is supplied by PICkit 3. This voltage is typically around 3.3V
for this device.
The ADC can convert the voltage from any one of 13 channels on the PIC18F45K20.
These analog input channels, numbered AN0 up to AN12, are shared with digital
microcontroller pins and must be configured as analog inputs to be used with the ADC.
The ADC is configured and controlled by 5 Special Function Registers: ANSEL,
ANSELH, ADCON0, ADCON1 and ADCON2. These are covered in detail in the next
section.
3.7.2 ADC Configuration and Operation
Looking at the schematic of the 44-Pin Demo Board in the Appendix, the potentiometer
(RP1) output is connected to the RA0/AN0 pin of the PIC18F45K20.
The basic steps needed to convert the ADC voltage on this pin are:
1. Configure the RA0/AN0 pin as an analog input in ANSEL.
2. Set the ADC voltage references in ADCON1.
3. Set the result justification, ADC clock source, and acquisition time in ADCON2.
4. Select the channel and turn on the ADC in ADCON0.
5. Start the conversion in ADCON0.
#1: To use a pin as an analog input, it must not be used by other peripheral functions
multiplexed on the same pin. The pin TRIS bit must be set to ‘1’ (input) and the ANSEL
bit associated with RA0 should be set to ‘1’ (analog input). However, we still want
RB0/AN12 configured as a Digital input to for the switch. Therefore, we will clear0’ the
AN12 bit in ANSELH.
Key Concepts
- An Analog-to-Digital Converter is used to convert an analog voltage level
into a digital number representing the voltage.
- The ANSEL, ANSELH, ADCON0, ADCON1, and ADCON2 SRFs configure
and control the on-chip ADC.
- A timer register can be written to a value that will cause a timer overflow at a
specific time interval required by the application.