PIC16/PIC18 ADC Technical Brief: 2018 Microchip Technology Inc. 90003194a-Page 1
PIC16/PIC18 ADC Technical Brief: 2018 Microchip Technology Inc. 90003194a-Page 1
Introduction
An Analog-to-Digital Converter (ADC) converts an analog input signal into a digital number representing
the magnitude of the input voltage. Microchip’s 12-bit ADC with Computation (ADC2) outputs a 12-bit
binary representation of the original signal and adds special hardware features to provide post-processing
functions that can be performed on the conversion result.
This technical brief provides an overview of the basic ADC features and functions, and describes the
additional computation features that are not found in other ADC modules. This document will not cover
the Capacitive Voltage Divider (CVD) feature, which is covered in TB3198, but may refer to CVD in
general terms. This technical brief will cover the ADC2 module found in Microchip’s PIC16 and PIC18 8-
bit architecture.
Table of Contents
Introduction......................................................................................................................1
2. Conclusion...............................................................................................................20
Customer Support......................................................................................................... 24
Legal Notice...................................................................................................................25
Trademarks................................................................................................................... 25
analog input by setting the port pin’s ANSEL bit. It is important to note that analog voltages on any pin
that is not defined as an analog input may cause the input buffer to conduct excess current.
When converting a digital signal, care must be taken when considering the ANSEL selection. If the ADC
is to convert a digital input signal, the port pin’s ANSEL bit should be cleared. The ADC can also read and
convert a digital output signal. In this case, the ANSEL bit can be cleared.
When switching between input channels, an acquisition delay is required before starting the next
conversion. The acquisition delay is required to meet the ADC’s specified accuracy. The delay allows time
for the sampling capacitor to fully charge to the input voltage level.
External source impedance (RS), internal interconnect impedance (RIC), and sampling switch impedance
(RSS) have a direct effect on the acquisition time (see figure below). Microchip typically recommends a
maximum source impedance of no greater than 10 kΩ. The device-specific recommended maximum
source impedance can be found in the device data sheet’s electrical specifications. Higher impedance
values require a longer acquisition time, and, as impedance is decreased, acquisition time may be
decreased.
Refer to Equation 1-1 to calculate the minimum acquisition time. This equation assumes that the sample
and hold capacitor is charged to within one-half of a Least Significant bit (LSb), which is the maximum
error allowed for the ADC to meet its specified resolution.
Figure 1-1. Analog Input Model
VDD Sampling
Analog input Switch
pin VT = 0.6V
RS RIC < 1k RSS
SS
CPIN ILEAKAGE
Analog voltage
(5 pF) VT = 0.6V CHOLD = 28 pF
source
7k
LEGEND:
6k
RSS (ohms)
Where:
TAD = ADC example clock period (2 us)
0.002 us
TCOFF = Temperature Coefficient = (Temperature - 25°C) *
°C
1
TC = Hold Capacitor Charging Time = �� = − ����� ��� + ��� + �� * ln �+1
2 − 1
Where:
CHOLD = sample and hold capacitor value (typical 28 pF)
RIC = internal interconnect impedance (1 kΩ typical)
RSS = internal sampling switch impedance (varies depending on VDD, see Figure 1-1)
RS = external analog source impedance
Example:
Find TACQ(MIN) when the temperature is 30°C, VDD = 5V, ADC resolution = 12 bits, and input
impedance is 1 kΩ:
First, TC is unknown, so it must be calculated:
1 1
�� = − 28 �� 1 �Ω + 1 �Ω + 1 �Ω * ln = − 28� − 12 3 �Ω * ln = 756 ��
12 + 1
2 ‐1 8191
Next, calculate TACQ:
0.002 ��
���� ��� = 2 �� + 756 �� + 30°� − 25°� * = 2.77 ��
°�
Since the example ADC clock period is 2.0 us and the TACQ is 2.77 us, it will take two ADC clock periods
to complete the conversion. The ADC Acquisition Time Control Register pair (ADACQH:ADACQL) can
now be loaded with the register value corresponding to two ADC clock periods.
Acquisition
Time Conversion stage
ADACQ<12:0> determines TACQ ≈ 2*TA D
TA D1 TA D2 TAD3 TA D4 TA D5 TA D6 TA D7 TA D8 TA D9 TA D10 TA D11TA D12
b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0
Software/trigger
source Conversion
sets GO Sampling begins
capacitor
disconnected On the following cycle:
ADRESH:ADRESL is loaded
Hardware sets ADIF (ADIF= 1)
Hardware clears GO (GO = 0)
Where:
N = number of ADC bits
VREF = VREF+ - VREF-
VIN = Analog input voltage
When an ADC conversion is complete, the results are stored in the ADRESH:ADRESL register pair. The
results are supplied to the register pair in one of two formats, either left or right justified (see figure
below). Result formatting is controlled by the ADC Results Format/Alignment Selection (FM) bit of the
ADCON0 register. The selected format for the ADRESH:ADRESL register pair also applies to the ADC
Previous Result (ADCPREVH:ADCPREVL) register pair.
Figure 1-3. 12-Bit ADC Conversion Result Format
ADRESH ADRESL
FM = 0 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0 0 0 0 0
bit 7 bit 0 bit 7 bit 0
ADRESH ADRESL
FM = 1 0 0 0 0 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0
bit 7 bit 0 bit 7 bit 0
The ADC module is not affected by either Idle or Doze modes which are available for use with both FOSC
and ADCRC clock sources. Idle or Doze modes may be used instead of Sleep mode to reduce the effects
of system noise.
1.2 Computation
The ADC2 module features post-conversion computation. After an ADC conversion has completed, the
result can be passed through one of the computation functions. The computation mode can be selected
by the ADC Operating Mode Selection (MD<2:0>) bits of the ADCON2 control register.
The computation modes include:
• Basic
• Accumulate
• Average
• Burst Average
• Low-Pass Filter
Threshold Test
ADCNT CRS<2:0> VIN 12-BIT ADRES 18-BIT ADACC 16-BIT ADFLTR
Performed?
1 4 2.5V 2047 2047 127 Yes
2 4 2.5V 2047 4094 256 Yes
3 4 2.5V 2047 6141 383 Yes
4 4 2.5V 2047 8188 511 Yes
5 4 2.5V 2047 10, 235 639 Yes
6 4 2.5V 2047 12, 282 767 Yes
7 4 2.5V 2047 14, 329 895 Yes
8 4 2.5V 2047 16, 376 1023 Yes
ADCNT is equal to ADRPT. At that point, the ADFLTR holds the average value of the samples acquired
during the burst, a threshold test is performed on the ADFLTR value, and ADTIF may be set depending
on the threshold settings.
The table below shows a Burst Average mode example. In this case, ADRPT is loaded with a value of 8
(based on Equation 1-4). That means that for each trigger event, the number of samples taken in each
burst is equal to ADRPT, or 8. After all eight samples are accumulated, the ADACC register is right-
shifted by the value of ADCRS, and the result transferred into ADFLTR.
Table 1-4. Burst Average Mode Example
difference on the filtered output. As the ADCRS value increases, the time it takes for the ADFLTR output
to achieve a steady state increases, but the effects of any deviations from the overall average have less
of an impact on the filtered output (see figure below).
Table 1-5 shows the effects of ADCRS on the ADFLTR output. In this comparison, the ADRES values are
centered around a value of 200. At random sample points (shaded), the ADRES values are changed to
simulate an unwanted noise component that the ADCC acquired. When the ADCRS bits are set to ‘6’, the
‘noise’ does not have much of an effect on the filtered output. Conversely, when ADCRS is set to ‘1’, the
‘noise’ has much more of an impact on the filtered output.
Essentially, when ADCRS values are higher, the effects of noise on the output are reduced, but sudden
changes in the input may take longer to influence the output. When the ADCRS values are lower, the
effects of noise have a larger impact on the filtered output, but sudden changes would be detected
quickly.
Equation 1-5. ADFLTR Calculation in Low-Pass Filter Mode
������
������ = �����
2
Where:
ACCPREV
ACCNEW = (ACCPREV + ADRES) -
2ADCRS
ACCPREV = Previous accumulator result
ADRES = Current conversion result
Figure 1-4. ADCRS Effects on ADFLTR Output
ADFLTR Output
Sample# ADRES
ADCRS = 6 ADCRS = 1
0 200 166 198
1 200 167 199
2 200 167 199
3 200 168 200
7 100 168 150
8 200 169 175
17 200 173 200
18 200 173 200
19 50 171 125
20 200 172 162
36 200 178 200
37 85 177 142
38 200 177 171
43 200 179 199
44 85 177 142
45 200 178 171
55 200 181 200
56 25 179 112
82 200 186 200
83 64 184 132
128 200 188 196
The radian values listed in the table above are defined by the ADCC’s hardware. These values are used
to calculate the -3dB roll-off point in terms of frequency. The following equation can be used to determine
the -3dB point; however, there is one fundamental part of this equation that can cause confusion.
Equation 1-6.
�������@ − 3��
Frequency @ -3dB roll-off point =
2Π�
Where:
Radians @ -3dB = the value from the table above based on the CRS value
T = total sampling time
The ‘T’ term indicates the total sampling time. The total sampling time is the measured time between
samples. The total sampling time is critical since it is the actual time it takes to acquire a single filtered
conversion result.
The ADC’s sampling rate is only part of the total sampling time necessary to properly calculate the roll-off
frequency. We know that the ADC’s sampling frequency influences the ADC result. What may not be
known is that the number of instructions contained in the ADC routine also influences the total sampling
time. Once the ADC’s conversion result has been acquired, the result must still pass through the filter.
The conversion result may need to be sent to the DAC to output the filtered waveform, or sent to a
logging file via a serial port. For example, if the ADC routine transmits the filtered result to the UART
using ‘printf’ commands, the total sampling time will be longer than if the filtered value was ‘manually’
written to the UART transmit buffer. The total sampling time includes the ADC acquisition time, the
conversion time, interrupt time, and any output transmission time.
The table below shows the difference in roll-off frequencies based on the sampling time ‘T’. In this
example, the ADCC acquires each sample in the same manner; however, the methods used to transmit
data over the UART are different. One method uses ‘printf’ statements, which are easy to use, but at the
expense of additional instruction time. The other method loads the UART TX buffer with the filtered
results through software instructions, which is slightly more cumbersome, but require fewer instructions
than ‘printf’ statements.
When shorter total sampling times are desired, consider the following:
• System clock (FOSC) - when used as the ADC clock, the system clock determines the TAD period
• Number of instructions - every instruction in the ADC routine takes time to execute, which adds to
the total sampling time
• Number of instructions in the ISR - interrupt routines should typically be as short as possible
• Type of instructions - as previously mentioned, using the ‘printf’ library function may be very easy to
use, but at the expense of additional instruction cycles
• ADC acquisition time - faster acquisition times reduce total sampling time
One way to measure the total sampling time would be to use the Stopwatch function built in to the
®
MPLAB X debugger. This is accomplished by placing a breakpoint at the beginning and at the end of the
ADC routine. The debugger will calculate the amount of time it takes to execute the ADC function in its
entirety, including any interrupts. Of course, there are other ways to calculate the routine’s time, such as
toggling a pin at the beginning and end of the routine and measuring the time in between pin states, or
using a timer that is enabled at the beginning of the routine and stops at the end of the routine.
LPF EXAMPLE
This example illustrates the expected output of the ADCC using the Low-Pass Filter function with a CRS
value of ‘1’. For this example, the ‘Method Using Direct UART Writes’ (table above) is used since it has
the fastest total sampling time, and gives a Nyquest limit of approximately 1.15 kHz.
A function generator is configured such that its output is 50 Hz sinewave, with a peak-to-peak value of 2
volts. The sinewave is offset by 1500 mV so that the voltage ranges from 500 mV to 2.5V because the
ADC cannot read voltages below the negative reference voltage. The output of the function generator is
connected to an analog input of the PIC18F26K42 microcontroller.
ADC Threshold interrupts are set to always interrupt after the completion of each sample.
The filtered result is copied to the UART, which sends the results to the Data Visualizer plug-in feature of
the Atmel Studio 7 IDE. The Data Visualizer accepts serial data and, amongst other features, converts
the data back into an analog equivalent that is shown on its built-in oscilloscope.
Figure 1-5 shows a 50 Hz sinewave reconstructed by the Data Visualizer. With the CRS value at 1, and
the sample time equal to 435 μs, the 50 Hz signal is well below the expected 263 Hz roll-off point. As the
sinewave’s frequency is increased, once it reaches approximately 270 Hz, a reduction in peak-to-peak
voltage takes place as the filter actively reduces the magnitude of the signal, as observed in Figure 1-6.
As the frequency continues to increase, the peak-to-peak range will shrink, as observed in Figure 1-7.
2. Conclusion
This technical brief describes basic ADC operation as well as the computation block features found in
Microchip’s Analog-to-Digital Converter with Computation (ADC2) module. Code examples can be found
at http://www.microchip.com/mplab/mplab-xpress.
����
LSb =
2�
Where:
N = number of ADC bits
VREF = VREF+ - VREF-
Differential Nonlinearity (DNL) Error: The difference between a measured code width and the ideal
value of one LSb. In an ideal ADC, when the DNL error is zero, each analog step equals one LSb, where
one LSb is equal to the ratio of the reference voltage to the ADC resolution (see equation below). In this
case, each transition is equally spaced one LSb apart.
DNL errors are calculated for each transition point, and the largest error is reported as the ADC’s DNL.
DNL errors are measured after the transfer function has been normalized. The possible range for DNL
error values is ± 1 LSb. If the error is ≤ -1 LSb, there will be missing codes in the transfer function. If the
error is zero, each LSb is considered ideal and no missing codes are reported in the transfer function. If
the error is greater than zero but less than or equal to +1 LSb, a monotonic transfer function is
guaranteed and there are no missing codes.
Equation 3-3. Differential Nonlinearity (DNL) Error Calculation
��� + 1 − ���
������ = − 1, where 0 < OC < 2N - 2
���� − �����
Customer Support
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Field Application Engineer (FAE)
• Technical Support
Customers should contact their distributor, representative or Field Application Engineer (FAE) for support.
Local sales offices are also available to help customers. A listing of sales offices and locations is included
in the back of this document.
Technical support is available through the web site at: http://www.microchip.com/support
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their
code. Code protection does not mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the
code protection features of our products. Attempts to break Microchip’s code protection feature may be a
violation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your software
or other copyrighted work, you may have a right to sue for relief under that Act.
Legal Notice
Information contained in this publication regarding device applications and the like is provided only for
your convenience and may be superseded by updates. It is your responsibility to ensure that your
application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY
OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS
CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE.
Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life
support and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend,
indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting
from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual
property rights unless otherwise stated.
Trademarks
The Microchip name and logo, the Microchip logo, AnyRate, AVR, AVR logo, AVR Freaks, BeaconThings,
BitCloud, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, Heldo, JukeBlox, KeeLoq, KeeLoq logo,
Kleer, LANCheck, LINK MD, maXStylus, maXTouch, MediaLB, megaAVR, MOST, MOST logo, MPLAB,
OptoLyzer, PIC, picoPower, PICSTART, PIC32 logo, Prochip Designer, QTouch, RightTouch, SAM-BA,
SpyNIC, SST, SST Logo, SuperFlash, tinyAVR, UNI/O, and XMEGA are registered trademarks of
Microchip Technology Incorporated in the U.S.A. and other countries.
ClockWorks, The Embedded Control Solutions Company, EtherSynch, Hyper Speed Control, HyperLight
Load, IntelliMOS, mTouch, Precision Edge, and Quiet-Wire are registered trademarks of Microchip
Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BodyCom,
chipKIT, chipKIT logo, CodeGuard, CryptoAuthentication, CryptoCompanion, CryptoController,
dsPICDEM, dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial
Programming, ICSP, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, Mindi, MiWi,
motorBench, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient
Code Generation, PICDEM, PICDEM.net, PICkit, PICtail, PureSilicon, QMatrix, RightTouch logo, REAL
ICE, Ripple Blocker, SAM-ICE, Serial Quad I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total
Endurance, TSHARC, USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA, and ZENA are
trademarks of Microchip Technology Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of
Microchip Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2018, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-3069-8
ISO/TS 16949
Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and wafer
fabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in California
® ®
and India. The Company’s quality system processes and procedures are for its PIC MCUs and dsPIC
®
DSCs, KEELOQ code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design and manufacture of development
systems is ISO 9001:2000 certified.