ADC With Threshold Detection
ADC With Threshold Detection
HIGHLIGHTS
This section of the manual contains the following major topics:
51.1
51.2
51.3
51.4
51.5
51.6
51.7
51.8
51.9
51.10
51.11
51.12
51.13
51.14
51.15
51.16
51.17
51.18
51.19
51.20
51.21
DS39739B-page 51-1
INTRODUCTION
The PIC24F 12-bit A/D Converter has the following key features:
The 12-bit A/D Converter module is an enhanced version of the 10-bit module offered in some
PIC24 devices. Both modules are Successive Approximation Register (SAR) Converters at their
cores, surrounded by a range of hardware features for flexible configuration. This version of the
module extends functionality by providing 12-bit resolution, a wider range of automatic sampling
options, tighter integration with other analog modules, such as the CTMU, and a configurable
results buffer. This module also includes a unique Threshold Detect feature that allows the
module itself to make simple decisions based on the conversion results.
As before, an internal Sample-and-Hold (S/H) amplifier acquires a sample of an input signal, then
holds that value constant during the conversion process. A combination of input multiplexers
selects the signal to be converted from up to 32 analog inputs, both external (analog input pins)
and internal (e.g., on-chip voltage references and other analog modules). The whole multiplexer
path includes provisions for differential analog input, although, with a limited number of negative
input pins. The sampled voltage is held and converted to a digital value, which strictly speaking,
represents the ratio of that input voltage to a reference voltage. Configuration choices allow
connection of an external reference or use of the device power and ground (AVDD and AVSS).
Reference and input signal pins are assigned differently depending on the particular device.
An array of timing and control selections allow the user to create flexible scanning sequences. Conversions can be started individually by program control, continuously free-running or triggered by
selected hardware events. A single channel may be repeatedly converted. Alternate conversions
may be performed on two channels, or any or all of the channels may be sequentially scanned and
converted according to a user-defined bit map. The resulting conversion output is a 12-bit digital
number, which can be signed or unsigned, left or right justified. (In some devices, a user-selectable
resolution of 10 bits is available; in other devices, 12-bit resolution is the only option available.)
Conversions are automatically stored in a dedicated buffer, allowing for multiple successive
readings to be taken before software service is needed. The buffer can be configured to function
as a FIFO buffer or as a channel indexed buffer. In FIFO mode, the buffer can be split into two
equal sections for simultaneous conversion and read operations. In Indexed mode, the buffer can
use the Threshold Scan feature to determine if a conversion meets specific, user-defined criteria,
storing or discarding the converted value as appropriate, and then set semaphore flags to
indicate the event. This allows conversions to occur in low-power modes when the CPU is
inactive, waking the device only when specific conditions have occurred.
The module sets its interrupt flag after a selectable number of conversions, when the buffer can be
read, or after a successful Threshold Detect comparison. After the interrupt, the sequence restarts
at the beginning of the buffer. When the interrupt flag is set, according to the earlier selection, scan
selections and the Output Buffer Pointer return to their starting positions.
A simplified block diagram for the module is shown in Figure 51-1.
DS39739B-page 51-2
51
12-Bit A/D Converter Block Diagram
AVSS
VREF+
VREF-
VR+
16
VR-
VBG
Comparator
VINH
VINL
AN0
VRS/H
VR+
DAC
AN1
12-Bit SAR
AN2
Conversion Logic
AN3
Data Formatting
AN4
VINH
AN5
MUX A
AN6
AN7
ADC1BUF0:
ADC1BUFn(3)
AN8
AD1CON1
VINL
AN9
AD1CON2
AD1CON3
AD1CON4
AD1CON5
AD1CHS
AD1CHITL
AD1CHITH
MUX B
AN(n-1)
ANn(1)
VBG(2)
VINH
AD1CSSL
AD1CSSH
AD1CTMENL
AD1CTMENH
AD1DMBUF
VINL
VBG/2(2)
VBG/6(2)
VDDCORE(2)
Sample Control
AVDD(2)
AVSS(2)
CTMU
Control Logic
Conversion Control
(2)
Note 1:
Up to 32 analog channels may be implemented, depending on the particular device family and individual device pin
count. Some or all of these may be external channels, depending on the device. Refer to the specific device data
sheet for details.
2:
Internal analog channels are implemented with different options in different device families. Refer to the specific
device data sheet for details.
3:
The conversion buffer will always be at least the same size (in words) as the number of external channels, rounded
up to the next greater even number. Refer to the specific device data sheet for the exact number.
DS39739B-page 51-3
Figure 51-1:
Figure 51-2:
Note 1:
In Automatic Sampling modes, Extended Sampling Time is added to the sequence when the value for the
Auto-Sampling Time is greater than 0. Otherwise, sampling ends and conversion starts whenever the SAMP bit is
cleared.
DS39739B-page 51-4
51
Operation as a State Machine
The A/D conversion process can be thought of in terms of a finite state machine (Figure 51-3).
The sample state represents the time that the input channel is connected to the S/H amplifier and
the signal is passed to the converter input. The convert state is transitory. The module enters this
state as soon as it exits the sample state and transitions to a different state when that is done.
The inactive state is the default state prior to module initialization and following a
software-controlled conversion; it can be avoided in operation by using Auto-Sample mode.
Machine states are identified by the state of several control and status bits in AD1CON1
(Register 51-1).
If the module is configured for Auto-Sample mode, the operation ping-pongs continuously
between the sample and convert states. The module automatically selects the input channels to
be sampled (if channel scanning is enabled), while the selected conversion trigger source paces
the entire operation. Any time that Auto-Sample mode is not used for conversion, it is available
for the sample state. The user needs to make certain that acquisition time is sufficient, in addition
to accounting for the normal concerns about system throughput.
Whenever the issue of sampling time is important, the significant event is the transition from
sample to convert state. This is the point where the Sample-and-Hold aperture closes, and it is
essentially the signal value at this instant, which is applied to the A/D for conversion to digital.
Figure 51-3:
Device Reset
INACTIVE
SAMP = 0
DONE = 1
1 or
SW
SAMP 01
ASAM = 0 and
HW
1
DONE 0
ASAM 0
SAMP = 1
DONE = x
SAMPLE
HW
1
ASAM = 1 and DONE 0
CONVERT
Legend:
Note:
See Register 51-1 for definitions of the ASAM, SAMP, DONE and SSRC<3:0> bits.
SAMP = 0
DONE = 0
DS39739B-page 51-5
51.2.1
REGISTERS
The 12-bit A/D Converter module uses up to 43 registers for its operation. All registers are
mapped in the data memory space.
51.3.1
Control Registers
Depending on the specific device, the module has up to twelve control and status registers:
The AD1CON1, AD1CON2 and AD1CON3 registers (Register 51-1, Register 51-2 and
Register 51-3) control the overall operation of the A/D module. This includes enabling the
module, configuring the conversion clock and voltage reference sources, selecting the sampling
and conversion triggers, and manually controlling the sample/convert sequences. The
AD1CON4 register (Register 51-4) sets the DMA buffer size in Scatter/Gather mode. The
AD1CON5 register (Register 51-5) specifically controls features of Threshold Detect operation,
including its functioning in power-saving modes.
The AD1CHS register (Register 51-6) selects the input channels to be connected to the S/H
amplifier. It also allows the choice of input multiplexers and the selection of a reference source
for differential sampling.
The AD1CHITH and AD1CHITL registers (Register 51-7 and Register 51-8) are semaphore
registers used with Threshold Detect operations. The status of individual bits, or bit pairs in
some cases, indicate if a match condition has occurred. Their use is described in more detail
in Section 51.10 Threshold Detect Operation. AD1CHITL is always implemented,
whereas AD1CHITH may not be implemented in devices with 16 channels or less.
The AD1CSSH/L registers (Register 51-9 and Register 51-10) select the channels to be included
for sequential scanning.
The AD1CTMENH/L registers (Register 51-11 and Register 51-12) select the channel(s) to be
used by the CTMU during conversions. Selecting a particular channel allows the A/D Converter
to control the CTMU (particularly, its current source) and read its data through that channel.
AD1CTMENL is always implemented, whereas AD1CTMENH may not be implemented in
devices with 16 channels or less.
51.3.2
The module incorporates a multi-word, dual port RAM, called ADC1BUF. The buffer is composed
of at least the same number of word locations as there are external analog channels for a particular device, with a maximum number of 32. The number of buffer addresses is always even. Each
of the locations is mapped into the data memory space and is separately addressable.The buffer
locations are referred to as ADC1BUF0 through ADC1BUFn (up to 31).
The A/D result buffers are both readable and writable. When the module is active
(AD1CON1<15> = 1), the buffers are read-only, and store the results of A/D conversions. When
the module is inactive (AD1CON1<15> = 0), the buffers are both readable and writable. In this
state, writing to a buffer location programs a conversion threshold for Threshold Detect
operations, as described in Section 51.10.2 Setting Comparison Thresholds.
When Extended DMA operation is enabled (AD1CON1<11> = 1), conversion results are written
to AD1DMBUF, a 16-bit conversion buffer. Buffer contents are not cleared when the module is
deactivated with the ADON bit (AD1CON1<15>). Conversion results and any programmed
threshold values in the ADC1BUFn registers are maintained when ADON is set or cleared.
DS39739B-page 51-6
51
AD1CON1: A/D Control Register 1
R/W-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0/U
R/W-0
R/W-0
ADON
ADSIDL
DMABM(3,4)
DMAEN(3)
MODE12(1)
FORM1
FORM0
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
U-0
R/W-0
R/W-0 HSC
R/C-0 HSC
SSRC3(2)
SSRC2(2)
SSRC1(2)
SSRC0(2)
ASAM
SAMP
DONE
bit 7
bit 0
Legend:
C = Clearable bit
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 15
bit 14
Unimplemented: Read as 0
bit 13
bit 12
bit 11
bit 10
bit 9-8
Note 1:
2:
3:
4:
This bit is implemented only in modules with user-selectable resolution. For A/D modules with fixed, 10-bit
or 12-bit resolution, this bit is unimplemented and should be maintained as 1 or 0. Refer to the device
data sheet for the specific setting of this bit.
Sample clock trigger sources are defined differently for each device family; not all trigger sources may be
defined. Refer to the specific device data sheet for details.
DMAEN and DMABM are implemented only in devices with DMA. For devices without DMA, the bits are
unimplemented and read as 0. For more information, refer to the specific device data sheet.
This bit is only available when Extended DMA/Buffer features are available (DMAEN = 1).
DS39739B-page 51-7
Register 51-1:
bit 7-4
Unimplemented: Read as 0
bit 2
bit 1
bit 0
Note 1:
2:
3:
4:
This bit is implemented only in modules with user-selectable resolution. For A/D modules with fixed, 10-bit
or 12-bit resolution, this bit is unimplemented and should be maintained as 1 or 0. Refer to the device
data sheet for the specific setting of this bit.
Sample clock trigger sources are defined differently for each device family; not all trigger sources may be
defined. Refer to the specific device data sheet for details.
DMAEN and DMABM are implemented only in devices with DMA. For devices without DMA, the bits are
unimplemented and read as 0. For more information, refer to the specific device data sheet.
This bit is only available when Extended DMA/Buffer features are available (DMAEN = 1).
DS39739B-page 51-8
51
AD1CON2: A/D Control Register 2
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
U-0
U-0
PVCFG1
PVCFG0
NVCFG0
OFFCAL
BUFREGEN
CSCNA
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
BUFS(1)
SMPI4(2)
SMPI3(2)
SMPI2(2)
SMPI1(2)
SMPI0(2)
BUFM(1)
ALTS
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 15-14
bit 13
bit 12
bit 11
bit 10
bit 9-8
Unimplemented: Read as 0
bit 7
bit 6-2
00001 = Increment the DMA address after completion of the 2nd sample/conversion operation
00000 = Increment the DMA address after completion of each sample/conversion operation
When DMAEN = 0:
11111 = Interrupts at the completion of conversion for each 32nd sample
11110 = Interrupts at the completion of conversion for each 31st sample
Applicable only when the buffer is used in FIFO mode (BUFREGEN = 0). In addition, BUFS is only used
when BUFM = 1 and DMAEN = 0 (AD1CON1<11>), if implemented in the specific device family.
In devices where DMAEN (AD1CON1<11>) is unimplemented, the SMPIx bits always select the number
of sample/conversions per each interrupt.
DS39739B-page 51-9
Register 51-2:
bit 1
bit 0
Note 1:
2:
Applicable only when the buffer is used in FIFO mode (BUFREGEN = 0). In addition, BUFS is only used
when BUFM = 1 and DMAEN = 0 (AD1CON1<11>), if implemented in the specific device family.
In devices where DMAEN (AD1CON1<11>) is unimplemented, the SMPIx bits always select the number
of sample/conversions per each interrupt.
DS39739B-page 51-10
51
AD1CON3: A/D Control Register 3
R/W-0
R-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
ADRC
EXTSAM
PUMPEN(1)
SAMC4
SAMC3
SAMC2
SAMC1
SAMC0
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
ADCS7
ADCS6
ADCS5
ADCS4
ADCS3
ADCS2
ADCS1
ADCS0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 15
bit 14
bit 13
bit 12-8
x = Bit is unknown
00001 = 1 TAD
00000 = 0 TAD
bit 7-0
= Reserved
01000000
00111111 = 64TCY = TAD
DS39739B-page 51-11
Register 51-3:
Register 51-4:
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
bit 15
bit 8
U-0
U-0
U-0
U-0
U-0
R/W-0
R/W-0
R/W-0
DMABL2(2)
DMABL1(2)
DMABL0(2)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 15-3
Unimplemented: Read as 0
bit 2-0
Note 1:
2:
x = Bit is unknown
AD1CON4 is not implemented on all devices. For more information, refer to the specific device data sheet.
The DMABL<2:0> bits are only used when AD1CON1<11> = 1; otherwise, their value is ignored.
DS39739B-page 51-12
51
AD1CON5: A/D Control Register 5
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
U-0
R/W-0
R/W-0
ASEN
LPEN
CTMREQ
BGREQ(1)
VRSREQ(1)
ASINT1(2)
ASINT0(2)
bit 15
bit 8
U-0
U-0
U-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
WM1
WM0
CM1
CM0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 15
bit 14
bit 13
bit 12
bit 11
bit 10
Unimplemented: Read as 0
bit 9-8
bit 7-4
Unimplemented: Read as 0
bit 3-2
Note 1:
2:
DS39739B-page 51-13
Register 51-5:
Note 1:
2:
DS39739B-page 51-14
51
AD1CHS: A/D Sample Select Register
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CH0NB2
CH0NB1
CH0NB0
CH0SB4
CH0SB3
CH0SB2
CH0SB1
CH0SB0
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CH0NA2
CH0NA1
CH0NA0
CH0SA4
CH0SA3
CH0SA2
CH0SA1
CH0SA0
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 15-13
bit 12-8
CH0SB<4:0>: S/H Amplifier Positive Input Select for MUX B Multiplexer Setting bits
The number of implemented inputs, and the bit combinations assigned to them, vary significantly
between device families. In general, external analog inputs are sequentially assigned from 00000,
starting with AN0, potentially up to AN31 (11111).
If 32 external inputs are not implemented, inputs for internal band gap references, external voltage references, and other analog modules, such as the CTMU, are implemented following the external analog
channels. Refer to the specific device data sheet for a complete listing of implemented inputs for a
particular device.
If a sequential input is unimplemented, its corresponding bit value is also unimplemented. Also, any bit
combinations not explicitly listed in the device data sheet are unimplemented. Using an unimplemented
channel for a conversion will produce unpredictable results.
bit 7-5
bit 4-0
DS39739B-page 51-15
Register 51-6:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CHH31(1)
CHH30(1)
CHH29(1)
CHH28(1)
CHH27(1)
CHH26(1)
CHH25(1)
CHH24(1)
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CHH23(1)
CHH22(1)
CHH21(1)
CHH20(1)
CHH19(1)
CHH18(1)
CHH17(1)
CHH16(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 15-0
Note 1:
x = Bit is unknown
The actual number of channels available depends on which channels are implemented on a specific
device. Refer to the device data sheet for details. Unimplemented channels are read as 0.
Register 51-8:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CHH15(1)
CHH14(1)
CHH13(1)
CHH12(1)
CHH11(1)
CHH10(1)
CHH9(1)
CHH8(1)
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CHH7(1)
CHH6(1)
CHH5(1)
CHH4(1)
CHH3(1)
CHH2(1)
CHH1(1)
CHH0(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 15-0
Note 1:
x = Bit is unknown
DS39739B-page 51-16
51
AD1CSSH: A/D Input Scan Select Register (High Word)
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CSS31(1)
CSS30(1)
CSS29(1)
CSS28(1)
CSS27(1)
CSS26(1)
CSS25(1)
CSS24(1)
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CSS23(1)
CSS22(1)
CSS21(1)
CSS20(1)
CSS19(1)
CSS18(1)
CSS17(1)
CSS16(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 15-0
Note 1:
x = Bit is unknown
The actual number of channels available depends on which channels are implemented on a specific
device; refer to the specific device data sheet for details. Unimplemented channels are read as 0.
Do not select unimplemented channels for sampling, as indeterminate results may be produced.
Register 51-10:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CSS15(1)
CSS14(1)
CSS13(1)
CSS12(1)
CSS11(1)
CSS10(1)
CSS9(1)
CSS8(1)
bit 15
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CSS7(1)
CSS6(1)
CSS5(1)
CSS4(1)
CSS3(1)
CSS2(1)
CSS1(1)
CSS0(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 15-0
Note 1:
x = Bit is unknown
DS39739B-page 51-17
Register 51-9:
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
bit 8
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
x = Bit is unknown
bit 15-0
The actual number of channels available depends on which channels are implemented on a specific
device; refer to the specific device data sheet for details. Unimplemented channels are read as 0.
Note 1:
Register 51-12:
R/W-0
(1)
CTMEN15
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CTMEN14(1)
CTMEN13(1)
CTMEN12(1)
CTMUEN11(1)
CTMEN10(1)
CTMEN9(1)
CTMEN8(1)
bit 15
bit 8
R/W-0
CTMEN7
(1)
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CTMEN6(1)
CTMEN5(1)
CTMEN4(1)
CTMEN3(1)
CTMEN2(1)
CTMEN1(1)
CTMEN0(1)
bit 7
bit 0
Legend:
R = Readable bit
W = Writable bit
-n = Value at POR
1 = Bit is set
0 = Bit is cleared
bit 15-0
Note 1:
x = Bit is unknown
DS39739B-page 51-18
51
A/D MODULE CONFIGURATION
All of the registers described in the previous section must be configured for module operation to
be fully defined. An effective approach is first, to describe the signals and sequences for the particular application. Typically, it is an iterative process to assign signals to port pins, to establish
timing methods and to organize a scanning scheme, as well as to integrate the whole process
with the software design.
The various configuration and control functions of the module are distributed throughout the
modules six control registers. Control functions can be broadly sorted into four groups: input, timing,
conversion and output. Table 51-1 shows the register location of control or status bits by register.
Table 51-1:
A/D Function
Register(s)
Input
AD1CON2
AD1CON5
AD1CHS
AD1CSSH/L
Specific Bits
AD1CON1
AD1CON2
SMPI<4:0>
AD1CON3
EXTSAM
AD1CON5
Timing
AD1CON3
Output
AD1CON1
AD1CON2
AD1CON4
DMABL<2:0>
AD1CON5
WM<1:0>, CM<1:0>
Note 1:
Note:
2.
3.
The options for each configuration step are described in the subsequent sections.
DS39739B-page 51-19
51.4
Most versions of the A/D module will have a fixed conversion resolution of either 12 bits (the
majority) or 10 bits. Where configuration is permitted, the MODE12 bit (AD1CON1<10>) controls
output resolution. Setting this bit selects 12-bit resolution.
51.4.2
The voltage references for A/D conversions are selected using the PVCFG<1:0> and NVCFG
control bits (AD1CON2<15:13>). The upper voltage reference (VR+) may be AVDD, the external
VREF+ or an internal band gap reference voltage. The lower voltage reference (VR-) may be AVSS
or the VREF- input pin. The available options vary between device families.
The external voltage reference pins may be shared with the AN0 and AN1 inputs on low pin count
devices. The A/D Converter can still perform conversions on these pins when they are shared
with the VREF+ and VREF- input pins.
The voltages applied to the external reference pins must meet certain specifications. Refer to the
device data sheet for further details.
51.4.3
The A/D Converter has a maximum rate at which conversions may be completed. An analog
module clock, TAD, controls the conversion timing. The A/D conversion requires 14 clock periods
(14 TAD) for a 12-bit conversion and 12 clock periods (12 TAD) for a 10-bit conversion. The A/D
clock is derived from the device instruction clock.
The period of the A/D conversion clock is software selected using a 6-bit counter. There are
64 possible options for TAD, specified by the ADCSX bits in the AD1CON3 register. Equation 51-1
gives the TAD value as a function of the ADCSx control bits and the device instruction cycle clock
period, TCY. For correct A/D conversions, the A/D conversion clock (TAD) must be selected to
ensure a minimum TAD time, as specified by the device family data sheet.
Equation 51-1:
TAD
1
TCY
Note: Based on TCY = 2/FOSC; Doze mode and PLL are disabled.
The A/D Converter also has its own dedicated RC clock source that can be used to perform
conversions. The A/D RC clock source should be used when conversions are performed while
the device is in Sleep mode. The RC oscillator is selected by setting the ADRC bit
(AD1CON3<15>). When the ADRC bit is set, the ADCSx bits have no effect on A/D operation.
51.4.4
The A/D module does not have an internal provision to configure port pins for analog operation.
Instead, input pins are configured as analog inputs through the Analog Select registers (ANSn,
where n is the port name). A pin is configured as an analog input when the corresponding ANSn
bit is set. By default, pins with multiplexed analog and digital functions are configured as analog
pins on device Reset.
For external analog inputs, both the ANSn register and the corresponding TRIS register bits control the operation of the A/D port pins. The port pins that will function as analog inputs must also
have their corresponding TRIS bits set, specifying the pins as inputs. After a device Reset, all
TRIS bits are set. If the I/O pin associated with an A/D channel is configured as a digital output
(TRIS bit is cleared), while the pin is configured for Analog mode, the port digital output level (VOH
or VOL) will be converted.
Note 1: When reading a PORT register, any pin configured as an analog input reads as 0.
2: Analog levels on any pin that is defined as a digital input may cause the input buffer
to consume current that is out of the devices specification.
DS39739B-page 51-20
51
Input Channel Selection
The A/D Converter incorporates two independent sets of input multiplexers (MUX A and MUX B)
that allow users to choose which analog channels are to be sampled. The inputs specified by the
CH0SAx and CH0NAx bits are collectively called the MUX A inputs. The inputs specified by the
CH0SBx and CH0NBx bits are collectively called the MUX B inputs.
Functionally, MUX A and MUX B are very similar to each other. Both multiplexers allow any of
the analog input channels to be selected for individual sampling and allow selection of a negative
reference source for differential signals. In addition, MUX A can be configured for sequential
analog channel scanning. This is discussed in more detail in Section 51.4.5.1 Configuring
MUX A and MUX B Inputs and Section 51.4.5.3 Scanning Through Several Inputs.
Note:
51.4.5.1
Different PIC24F devices will have different numbers of analog inputs. Verify the
analog input availability against the particular devices data sheet.
The user may select any one of up to 32 inputs available to the A/D Converter as the positive
input of the S/H amplifier. For MUX A, the CH0SA<4:0> bits (AD1CHS<4:0>) normally select the
analog channel for the positive input. For MUX B, the positive channel is selected by the
CH0SB<4:0> bits (AD1CHS<12:8>).
All of the external analog channels are available as positive inputs. In addition to the external
inputs, these may also include device supply voltage (AVDD), the logic core supply voltage
(VDDCORE), the internal band gap voltage (VBG) and/or multiples or fractions of VBG. One or more
additional input channels are used for the CTMU. These selections leave the A/D disconnected
from all other inputs. The options vary by device family; refer to the specific device data sheet for
details.
The CTMU input is selected by the AD1CTMENH/L registers. Setting a particular bit in one of
these registers effectively assigns the analog output from the CTMU to the corresponding A/D
input channel, automatically enabling the CTMU. Many devices will already have a CH0SAx bit
combination designated for use of the CTMU. This setting disconnects the converter from any
other load. This channel should be the one selected by the appropriate AD1CTMEN bit. If
another channel is selected, verify that any other analog sources are disconnected from that
channel; otherwise, erroneous readings may result.
For the negative (inverting) input of the amplifier, the user has up to eight options, selected by
the CH0NA<2:0> and CH0NB<2:0> bits (AD1CHS<7:5> and AD1CHS<15:13>, respectively).
Options typically include the device ground (AVSS), the current VR- source designated by the
NVCFG bit (AD1CON2<13>), and one or more of the external analog input channels. As with the
non-inverting inputs, the options vary by device family.
51.4.5.2
By default, the A/D Converter only samples and converts the inputs selected by MUX A. The
ALTS bit (AD1CON2<0>) enables the module to alternate between two sets of inputs selected
by MUX A and MUX B during successive samples.
If the ALTS bit is 0, only the inputs specified by the CH0SAx and CH0NAx bits are selected for
sampling. When the ALTS bit is 1, the module will alternate between the MUX A inputs on one
sample and the MUX B inputs on the subsequent sample.
If the ALTS bit is 1 on the first sample/convert sequence, the inputs specified by the CH0SAx
and CH0NAx bits are selected for sampling. On the next sample/convert sequence, the inputs
specified by the CH0SBx and CH0NBx bits are selected for sampling. This pattern repeats for
subsequent sample conversion sequences.
DS39739B-page 51-21
51.4.5
When using MUX A to select analog inputs, the A/D module has the ability to scan multiple
analog channels. When the CSCNA bit (AD1CON2<10>) is set, the CH0SA bits are ignored and
the channels specified by the AD1CSSL register are sequentially sampled.
Each bit in the AD1CSSL register and AD1CSSH register (when implemented) corresponds to
one of the analog channels. If a bit in the AD1CSSL or AD1CSSH register is set, the corresponding analog channel is included in the scan sequence. Inputs are always scanned from lower to
higher numbered inputs, starting at the first selected channel after each interrupt occurs.
Note 1: If the number of scanned inputs selected is greater than the number of samples
taken per interrupt, the higher numbered inputs will not be sampled.
2: If the CTMU channel is to be included in a scan operation, verify that the proper
analog input channel is selected and that the AD1CTMEN register(s) are correctly
configured. For more information, see Section 51.4.5.1 Configuring MUX A and
MUX B Inputs.
The AD1CSSH/L registers bits specify the positive input of the channel. The CH0NAx bits still
select the negative input of the channel during scanning.
Scanning is only available on the MUX A input selection. The MUX B input selection, as specified
by the CH0SBx bits, will still select the alternating input. When alternated sampling between
MUX A and MUX B is selected (ALTS = 1), the input will alternate between a set of scanning
inputs specified by the AD1CSSH/L registers, and a fixed input specified by the CH0SBx bits.
Automatic scanning can be used in conjunction with the Threshold Detect feature to determine
if one or more analog channels meet a predetermined set of conditions while the CPU is inactive.
This is described in detail in Section 51.10 Threshold Detect Operation.
51.4.5.4
While the A/D module can scan and convert analog inputs in low-power modes, some internal
analog inputs may be unavailable in Sleep mode. The main examples are the CTMU module, the
internal band gap voltage source and the on-chip voltage regulator (for those devices that include
one). The A/D module provides a method to make these resources available automatically
through the CTMREQ, BGREQ and VRSREQ bits (AD1CON5<13:11>, respectively). Setting
one or more of these bits causes the corresponding internal analog source(s) to become active
during a channel scan.
The CTMREQ bit is available on all devices. The BGREQ bit is available on those devices that
make the internal band gap reference available to the A/D module as an input channel and/or as
VREF+.The VRSREQ bit is only available in device families that use an on-chip voltage regulator.
51.4.6
When the ADON bit (AD1CON1<15>) is set, the module is fully powered and functional. When
ADON is 0, the module is disabled. Although the digital and analog portions of the circuit are
turned off for maximum current savings, the contents of all registers are maintained.
Conversion data stored in the ADC1BUF registers will also be maintained, including any threshold values stored by the user. It may be necessary to re-initialize these registers to their proper
values before re-enabling the module.
When enabling the module by setting the ADON bit, the user must wait for the analog stages to
stabilize. For the stabilization time, refer to Section 51.18 Electrical Specifications.
DS39739B-page 51-22
51
INITIALIZATION
Example 51-1 shows a simple initialization code example for the A/D module. Operation in Idle
mode is disabled, output data is in unsigned fractional format, and AVDD and AVSS are used for
VR+ and VR-. The start of sampling, as well as the start of conversion (conversion trigger), are
performed directly in software. Scanning of inputs is disabled and an interrupt occurs after every
sample/convert sequence (one conversion result) with only one channel (AN0) being converted.
The A/D conversion clock is TCY/2.
In this particular configuration, all 16 analog input pins are set up as analog inputs. It is important
to note that with this A/D module, I/O pins are configured for analog or digital operation at the I/O
port with the ANSn Analog Select registers. The use of these registers is described in detail in
the I/O Port chapter of the specific device data sheet.
This example shows one method of controlling a sample/convert sequence by manually setting
and clearing the SAMP bit (AD1CON1<1>). This method, among others, is more fully discussed in Section 51.6 Controlling the Sampling Process and Section 51.7 Controlling
the Conversion Process.
Example 51-1:
AD1CON1 = 0x2200;
AD1CON2 = 0;
AD1CON3 = 0;
AD1CHS
= 0;
AD1CSSL = 0;
IFS0bits.AD1IF = 0;
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
IEC0bits.AD1IE = 1;
AD1CON1bits.ADON = 1;
AD1CON1bits.SAMP = 1;
Delay();
AD1CON1bits.SAMP = 0;
//
//
//
//
//
//
DS39739B-page 51-23
51.5
Manual Sampling
Setting the SAMP bit (AD1CON1<1>) while the ASAM bit (AD1CON1<2>) is clear causes the
A/D to begin sampling. Clearing the SAMP bit ends sampling and automatically begins the
conversion; however, there must be a sufficient delay between setting and clearing SAMP for the
sampling process to start (tPSS, Parameter AD61). Sampling will not resume until the SAMP bit
is once again set. For an example, see Figure 51-4.
51.6.2
Automatic Sampling
Setting the ASAM bit causes the A/D to automatically begin sampling after a conversion has
been completed. One of several options can be used as an event to end sampling and complete
the conversions. Sampling will continue on the next selected channel after the conversion in
progress has completed. For an example, see Figure 51-5.
51.6.3
The SAMP bit indicates the sampling state of the A/D. Generally, when the SAMP bit clears,
indicating the end of sampling, the DONE bit is automatically cleared to indicate the start of
conversion. If SAMP is 0 while DONE is 1, the A/D is in an inactive state.
51.6.4
Aborting a Sample
While in Manual Sampling mode, clearing the SAMP bit will terminate sampling. If
SSRC<3:0> = 0000, it may also start a conversion automatically.
Clearing the ASAM bit while in Automatic Sampling mode will not terminate an ongoing
sample/convert sequence; however, sampling will not automatically resume after a subsequent
conversion.
51.7
51.7.1
Manual Control
When SSRC<3:0> = 0000, the conversion trigger is under software control. Clearing the SAMP
bit (AD1CON1<1>) starts the conversion sequence.
Figure 51-4 is an example where setting the SAMP bit initiates sampling, and clearing the SAMP
bit terminates sampling and starts conversion. The user software must time the setting and
clearing of the SAMP bit to ensure adequate sampling time of the input signal.
Figure 51-5 is an example where setting the ASAM bit initiates automatic sampling, and clearing
the SAMP bit terminates sampling and starts conversion. After the conversion completes, the
module sets the SAMP bit and returns to the sample state. The user software must time the
clearing of the SAMP bit to ensure adequate sampling time of the input signal, understanding that
the time since previously clearing the SAMP bit includes the conversion time, which immediately
follows, as well as the next sampling time.
DS39739B-page 51-24
51
Converting One Channel, Manual Sample Start, Manual Conversion Start
A/D CLK
TSAMP
TCONV
SAMP
DONE
ADC1BUF0
Instruction Execution
Example 51-2:
Converting One Channel, Manual Sample Start, Manual Conversion Start Code
int ADCValue;
ANSB
= 0x0001;
AD1CON1 = 0x0000;
AD1CHS = 0x0002;
//
//
//
//
AD1CSSL = 0;
AD1CON3 = 0x0002;
AD1CON2 = 0;
AD1CON1bits.ADON = 1;
while (1)
{
AD1CON1bits.SAMP = 1;
Delay();
// repeat continuously
AD1CON1bits.SAMP = 0;
while (!AD1CON1bits.DONE){};
ADCValue = ADC1BUF0;
//
//
//
//
//
//
start sampling...
Ensure the correct sampling time has elapsed
before starting conversion.
start converting
conversion done?
yes then get ADC value
Figure 51-5:
A/D CLK
TAD0
TSAMP
TCONV
TAD0
TSAMP
TCONV
SAMP
ADC1BUF0
BSF AD1CON1, ASAM
Instruction Execution
DS39739B-page 51-25
Figure 51-4:
When ADRC = 1, the conversion trigger is under A/D clock control. The SAMCx bits
(AD1CON3<12:8>) select the number of TAD clock cycles between the start of sampling and the
start of conversion. After the start of sampling, the module will count a number of TAD clocks
specified by the SAMCx bits. The SAMCx bits must always be programmed for at least 1 clock
cycle to ensure sampling requirements are met.
Equation 51-2:
Figure 51-6 shows how to use the clocked conversion trigger with the sampling started by the
user software.
Converting One Channel, Manual Sample Start, TAD-Based Conversion Start
Figure 51-6:
A/D CLK
TSAMP
TCONV
SAMP
DONE
ADC1BUF0
Instruction Execution
Example 51-3:
Converting One Channel, Manual Sample Start, TAD-Based Conversion Start Code
int ADCValue;
ANSB
= 0x1000;
AD1CON1 = 0x00E0;
AD1CHS = 0x000C;
AD1CSSL = 0;
AD1CON3 = 0x1F02;
AD1CON2 = 0;
AD1CON1bits.ADON = 1;
while (1)
{
AD1CON1bits.SAMP = 1;
while (!AD1CON1bits.DONE){};
ADCValue = ADC1BUF0;
}
DS39739B-page 51-26
//
//
//
//
//
51
FREE-RUNNING SAMPLE CONVERSION SEQUENCE
Using the Auto-Convert Conversion Trigger mode (SSRC<3:0> = 0111), in combination with the
Auto-Sample Start mode (ASAM = 1), allows the A/D module to schedule sample/conversion
sequences with no intervention by the user or other device resources. This Clocked mode,
shown in Figure 51-7, allows continuous data collection after module initialization.
Note that all timing in this mode scales with TAD, either from the A/D internal RC clock or from
TCY (as prescaled by the ADCS<7:0> bits). In both cases, the SAMC<4:0> bits set the number
of TAD clocks in TSAMP. TCONV is fixed at 12 TAD.
Figure 51-7:
A/D CLK
TSAMP
TCONV
TSAMP
TCONV
SAMP
Reset by
Software
DONE
ADC1BUF0
ADC1BUF1
Instruction Execution BSF AD1CON1, ASAM
Example 51-4:
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
clear variable
initialize ADC1BUF pointer
clear ADC interrupt flag
auto start sampling for 31Tad
then go to conversion
conversion done?
yes then stop sample/convert
average the 2 ADC value
// repeat
DS39739B-page 51-27
51.7.2.1
The user must ensure the sampling time satisfies the sampling requirements, as outlined in
Section 51.12 A/D Sampling Requirements. Assuming that the module is set for automatic
sampling and using a clocked conversion trigger, the sampling interval is specified by the
SAMCx bits.
51.7.3
It is often desirable to synchronize the end of sampling and the start of conversion with some
other time event. Depending on the device family, the A/D module has up to 16 sources available
to use as a conversion trigger event. The event trigger is selected by the SSRC<3:0> bits
(AD1CON1<7:4>).
As noted, the available event triggers vary between device families. Refer to the specific device
data sheet for specific information. The examples that follow represent trigger sources that are
implemented in most devices. Note that the SSRCx bit assignments may vary in some devices.
51.7.3.1
When SSRC<3:0> = 0001, the A/D conversion is triggered by an active transition on the INT0
pin. The pin may be programmed for either a rising edge input or a falling edge input.
51.7.3.2
When SSRC<3:0> = 0010, the A/D is triggered by a match between the 32-bit timer pair,
TMR3/TMR2, and the 32-bit combined Period register, PR3/PR2. The match causes the timer to
generate a special A/D trigger event signal.
In some devices, this feature is also implemented for the TMR5/TMR4 timer pair. Refer to the
specific device data sheet for details.
51.7.3.3
The modes where an external event trigger pulse ends sampling and starts conversion may be
used in combination with auto-sampling (ASAM = 1) to cause the A/D to synchronize the sample
conversion events to the trigger pulse source. For example, in Figure 51-9, where
SSRC<3:0> = 0010 and ASAM = 1, the A/D will always end sampling and start conversions
synchronously with the timer compare trigger event. The A/D will have a sample conversion rate
that corresponds to the timer comparison event rate.
51.7.3.4
Different sample/conversion sequences provide different available sampling times for the S/H
channel to acquire the analog signal. The user must ensure the sampling time satisfies the
sampling requirements, as outlined in Section 51.12 A/D Sampling Requirements.
Assuming that the module is set for automatic sampling, and an external trigger pulse is used as
the conversion trigger, the sampling interval is a portion of the trigger pulse interval. The sampling
time is the trigger pulse period, less the time required to complete the conversion.
Equation 51-3:
TSMP = Trigger Pulse Interval (TSEQ) Conversion Time (TCONV) = TSEQ TCONV
DS39739B-page 51-28
51
Manual Sample Start, Conversion Trigger-Based Conversion Start
Figure 51-8:
Conversion Trigger
A/D CLK
TSAMP
TCONV
SAMP
ADC1BUF0
Instruction Execution BSF AD1CON1, SAMP
Figure 51-9:
Conversion Trigger
A/D CLK
TSAMP
TCONV
TSAMP
TCONV
SAMP
Reset by
Software
DONE
ADC1BUF0
ADC1BUF1
BSF AD1CON1, ASAM
Example 51-5:
Instruction Execution
Converting One Channel, Auto-Sample Start, Conversion Trigger-Based Conversion Start Code
int ADCValue;
ANSB
= 0x0001;
AD1CON1 = 0x0040;
AD1CHS = 0x0002;
AD1CSSL = 0;
AD1CON3 = 0x0000;
AD1CON2 = 0x0004;
TMR3
= 0x0000;
PR3
= 0x3FFF;
T3CON
= 0x8010;
AD1CON1bits.ADON = 1;
AD1CON1bits.ASAM = 1;
while (1)
{
while (!IFS0bits.AD1IF){};
ADCValue = ADC1BUF0;
IFS0bits.AD1IF = 0;
}
//
//
//
//
//
// turn ADC ON
// start auto sampling every 125 ms
// repeat continuously
// conversion done?
// yes then get first ADC value
// clear AD1IF
DS39739B-page 51-29
The DONE bit (AD1CON1<0>) indicates the conversion state of the A/D. Generally, when the
SAMP bit clears, indicating the end of sampling, the DONE bit is automatically cleared to indicate
the start of conversion. If SAMP is 0 while DONE is 1, the A/D is in an inactive state.
In some operational modes, the SAMP bit may also invoke and terminate sampling. In these
modes, the DONE bit cannot be used to terminate conversions in progress.
51.7.5
The SMPI<4:0> bits (AD1CON2<6:2>) control the generation of the A/D Interrupt Flag, AD1IF.
The A/D Interrupt Flag is set after the number of sample/conversion sequences is specified by
the SMPIx bits, after the start of sampling, and continues to recur after that number of samples.
The value specified by the SMPIx bits also corresponds to the number of data samples in the
buffer, up to the maximum of 16. To enable the interrupt, it is necessary to set the A/D Interrupt
Enable bit, AD1IE.
If DMAEN (AD1CON1<11>) is set, the interrupt is generated on every successful write to the
AD1DMBUF register. For more information, refer to Section 51.9 Extended DMA
Operations.
If auto-scan is enabled (AD1CON5<15> = 1), interrupt generation is controlled by the ASINTx
bits (AD1CON5<9:8>). For more information, refer to Section 51.10.4 Threshold Detect
Interrupts.
51.7.6
Aborting a Conversion
Clearing the ADON bit during a conversion will abort the current conversion. The A/D results buffer will not be updated with the partially completed A/D conversion sample; that is, the
corresponding ADC1BUF buffer location will continue to contain the value of the last completed
conversion (or the last value written to the buffer).
51.7.7
Offset Calibration
The module provides a simple calibration method to offset the effects of internal device noise.
While not always necessary, this may be helpful in situations where weak analog signals are
being converted. Calibration is performed by using the OFFCAL bit (AD1CON2<12>). This disconnects the S/H amplifier entirely from any inputs. With the OFFCAL bit set, a single reference
conversion is performed. The results of this conversion are value added by internal device noise.
This result can be stored by the application, then used as an offset value for future conversions.
51.8
DS39739B-page 51-30
This section describes buffer operation in Legacy mode (AD1CON5<3:2> = 00). Buffer operation is different when the Compare Only or Compare and Save modes are
used with the Threshold Detect feature. For more information, see Section 51.10
Threshold Detect Operation.
51
Number of Conversions Per Interrupt
The SMPI<4:0> bits select how many A/D conversions will take place before the CPU is
interrupted. This can vary from 1 to 16 samples per interrupt. The A/D Converter module always
starts writing its conversion results at the beginning of the buffer, after each interrupt. For
example, if SMPI<4:0> = 00000, the conversion results will always be written to the ADC1BUF0.
In this example, no other buffer locations would be used, since only one sequence per interrupt
is specified.
51.8.2
The results buffer can be configured to operate in either of two modes: a standard FIFO mode,
compatible with the earlier 10-bit A/D module (default), or a Channel Indexed mode. The Fill
mode is selected by the BUFREGEN bit (AD1CON2<11>).
51.8.2.1
FIFO MODES
When BUFREGEN = 0, the results buffer operates in FIFO mode. The first conversion results,
after initiating conversions, is written to the first available buffer address. Subsequent conversions are written to the next sequential buffer location, continuing until the process is interrupted.
If allowed to continue without interrupts, the module would fill each location and then wrap around
to the first address, continuing the process.
The BUFM bit (AD1CON2<1>) controls how the buffer is filled. When BUFM is 1, the buffer is
split into two equal halves: a lower half (ADC1BUF0 through ADC1BUF[(n/2) 1]) and an upper
half (ADC1BUF[n/2] through ADC1BUFn), where n is the number of available analog channels
(both internal and external). The buffers will alternately receive the conversion results after each
interrupt event. The initial buffer used after BUFM is set is the lower group.
When BUFM is 0, the entire buffer is used for all conversion sequences.
Note:
When the BUFM bit is set, the user should not program the SMPIx bits to a value
that specifies more than (n/2) conversions per interrupt.
The decision to use the split buffer feature will depend upon how much time is available to move
the buffer contents after the interrupt, as determined by the application. If the application can
quickly unload a full buffer within the time it takes to sample and convert one channel, the BUFM
bit can be 0, and up to 16 conversions may be done per interrupt. The application will have one
sample/convert time before the first buffer location is overwritten.
If the processor cannot unload the buffer within the sample and conversion time, the BUFM bit
should be 1. For example, if SMPI<4:0> = 00111, then eight conversions will be loaded into the
lower half of the buffer, following which, an interrupt may occur. The next eight conversions will
be loaded into the upper half of the buffer. The processor will, therefore, have the entire time
between interrupts to move the eight conversions out of the buffer.
51.8.2.2
When BUFREGEN = 1, FIFO operation is disabled. In this Fill mode, the conversion result for
each channel is written only to the buffer location that corresponds to that channel. For example,
any conversions performed on AN0 are stored only in ADC1BUF0. The same holds true for AN1
and ADC1BUF1, and so on. Subsequent conversions on a particular channel that occur, prior to
an interrupt, will result in any previous data in that location being overwritten.
Channel Indexed mode is particularly useful when used with the Threshold Detect feature, as this
allows the user to easily test for a particular condition on a specific analog channel without creating an excess of CPU overhead. This is covered in more detail in Section 51.10 Threshold
Detect Operation.
DS39739B-page 51-31
51.8.1
The results of each A/D conversion are 12 bits wide (optionally, 10 bits wide in some devices).
To maintain data format compatibility, the result of each conversion is automatically converted to
one of four selectable, 16-bit formats. The FORM<1:0> bits (AD1CON1<9:8>) select the format.
Figure 51-10 and Figure 51-11 show the data output formats that can be selected. Tables 51-2
through 51-5 show the numerical equivalents for the various conversion result codes.
Figure 51-10: A/D Output Data Formats (12-Bit)
RAM Contents:
d11 d10 d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Read to Bus:
Integer
Signed Integer
d11 d10 d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
d11 d11 d11 d11 d11 d10 d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Fractional (1.15)
d11 d10 d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
d11 d10 d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Table 51-2:
VIN/VREF
4095/4096
4095
2047
4094/4096
4094
2046
1
2049/4096
2049
2048/4096
2048
2047/4096
2047
-1
1/4096
-2047
0/4096
-2048
Table 51-3:
VIN/VREF
12-Bit
Output Code
4095/4096
0.999
0.499
4094/4096
0.998
0.498
2049/4096
0.501
0.001
2048/4096
0.500
0.000
2047/4096
0.499
-0.001
1/4096
0.001
-0.499
0/4096
0.000
-0.500
DS39739B-page 51-32
51
A/D Output Data Formats (10-Bit)
RAM Contents:
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Read to Bus:
Integer
Signed Integer
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
d09 d09 d09 d09 d09 d09 d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Fractional (1.15)
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Table 51-4:
VIN/VREF
10-Bit
Output Code
1023/1024
11 1111 1111
1023
511
1022/1024
11 1111 1110
1022
510
513/1024
10 0000 0001
513
512/1024
10 0000 0000
512
511/1024
01 1111 1111
511
-1
1/1024
00 0000 0001
-511
0/1024
00 0000 0000
-512
Table 51-5:
VIN/VREF
1023/1024
11 1111 1111
0.999
0.499
1022/1024
11 1111 1110
0.998
0.498
0.501
0.001
513/1024
10 0000 0001
512/1024
10 0000 0000
0.500
0.000
511/1024
01 1111 1111
0.499
-0.001
1/1024
00 0000 0001
0.001
-0.499
0/1024
00 0000 0000
0.000
-0.500
DS39739B-page 51-33
Figure 51-11:
51.9.1
Extended Buffer mode is enabled when DMABM (AD1CON1<12>) = 1. In this Buffer mode, the
DMA will assemble a buffer in memory in the order of conversions completed. It is useful for storing the results of conversions on the upper channels, which do not have their own memory
mapped buffers inside the A/D module. It can also be used to store the conversion results on any
A/D channel in any implemented address in data RAM. In Extended Buffer mode, in order to scan
multiple channels, the DMA must be configured for Peripheral Indirect mode. The DMADST
register points to the beginning of the buffer. The A/D module provides the inter-block address,
pointing to locations within the block of DMA data. The address to the buffer is formed from two
parts, ORed together. The DMA count and SMPIx bits (AD1CON2<6:2>) must be set to generate
an interrupt after the proper number of conversions. Conversion data is written to a destination
specified by the DMA Controller.
This allows users to implement dedicated storage for conversions of higher numbered channels,
which do not have their own memory mapped A/D buffer locations. When using Extended Buffer
mode, always set the BUFREGEN bit to disable FIFO operation. In addition, disable the Split
Buffer mode by clearing the BUFM bit.
51.9.2
Scatter/Gather Mode
When DMABM = 0, the A/D module is configured to function with the DMA Controller for
Scatter/Gather mode operations. The buffers are written in the order of the channels scanned.
The A/D module generates an 11-bit Indirect Address (IA). The IA is ORed with the destination
address in the DMA Controller to define where the A/D conversion data will be stored.
In Scatter/Gather mode, each analog channel is allocated a buffer for conversion data. Each
buffer is contiguous, and the size of each channels buffer is selected by the DMABLx bits
(AD1CON4<2:0>). The size options range from a single word per buffer to 128 words. The buffers are allocated to each channel, regardless of whether or not the channel will actually have
conversion data. The exception to this rule is when the buffer size per channel is large enough
that not all of the channels can be addressed (DMABL<2:0>= 110 or 111).
If Peripheral Indirect Addressing (PIA) is used, the Indirect Address (IA) is created by combining
the base address within a channel buffer with three to five bits (depending on the buffer size) to
identify the channel. The base address ranges from zero to seven bits wide, depending on the
buffer size. The address is right-padded with a 0 in order to maintain address alignment in the
data space. The concatenated channel and base address bits are then left-padded with zeroes,
as necessary, to complete the 11-bit IA.
The write address inside a channel buffer is controlled by the A/D module. The buffer location
counter is a simple counter. It is initialized to 0 when the A/D module is initialized. Its maximum
value is defined by the DMABLx bits. Upon reaching this maximum value, it rolls over back to 0.
It is incremented based on the SMPIx bits (AD1CON2<6:2>). Essentially, it is incremented when
the non-DMA version of the module would generate an A/D interrupt (for more information, refer
to Section 51.8.1 Number of Conversions Per Interrupt).
DS39739B-page 51-34
51
As with PIA operations for any DMA-enabled module, the base destination address in the
DMADST register must be masked properly to accommodate the IA. Table 51-6 shows how complete addresses are formed. Note that the address masking varies for each buffer size option.
Because of masking requirements, some address ranges may not be available for certain buffer
sizes. Users should verify that the DMA base address is compatible with the buffer size selected.
Figure 51-12 shows how the parts of the address define the buffer locations in data memory. In
this case, the module allocates 256 bytes of data RAM (1000h to 1100h) for 32 buffers of four
words each. However, this is not a hard allocation and nothing prevents these locations from
being used for other purposes. For example, in the current case, if Analog Channels 2, 7 and 29
are being sampled and converted, conversion data will only be written to the channel buffers,
starting at 1008h, 1038h and 10F0h. The empty locations in PIA buffer space can be used for
any other purpose.
Note that the generated buffer address is not checked for out of bounds conditions. It is the users
responsibility to keep track of buffer locations and preventing data overwrites. Also, the number
of potential analog inputs, multiplied by the buffer size specified in the DMABLx bits, cannot
exceed the total DMA buffer area.
Table 51-6:
DMABL<2:0>
Generated Offset
Address (lower 11 bits)
Available
Input
Channels
Allowable DMADST
Addresses
000
32
001
32
010
32
011
32
100
16
32
101
32
32
110
64
16
111
128
Legend: ccc = Channel number (three to five bits); n = Base buffer address (zero to seven bits);
x = User-definable range of DMADST for base address; 0 = Masked bits of DMADST for IA.
DS39739B-page 51-35
The IA is configured to auto-increment during write operations according to the SMPI<4:0> bits
(AD1CON2<6:2>). There are three useful cases of the SMPIx bits for use with the DMA. When
the A/D is set up to store one conversion per channel, the SMPIx bits should be set to 00000.
This indicates that the address will increment every sample. If ALTS = 1, indicating alternating
analog input selections, then set SMPI<4:0> = 00001 to allow two samples per address
increment. When scanning is used, then the SMPIx bits should be set to the number of inputs
being scanned. So, for example, if AD1CSSL = 0xFFFF, indicating AN0-AN15 are being
scanned, then set SMPI<4:0> = 01111 to increment the address after 16 sample/conversion
sequences.
A/D Module
DMABL<2:0> = 010
(16-Word Buffer Size)
Data RAM
BBA Channel
ccccc (0-31)
nn (0-3)
(Buffer Base Address)
1000h
1008h
1010h
1018h
Ch 7 Buffer (4 Words)
Ch 8 Buffer (4 Words)
1038h
1040h
Ch 29 Buffer (4 Words)
Ch 29 Buffer (4 Words)
Ch 31 Buffer (4 Words)
10F0h
10F8h
1100h
Destination
Range
DMADST
(PIA Mode)
DMA Channel
Ch 0 Buffer (4 Words)
Ch 1 Buffer (4 Words)
Ch 2 Buffer (4 Words)
Ch 3 Buffer (4 Words)
Buffer Address
Channel Address
Address Mask
DMA Base Address
Ch 0, Word 0
Ch 0, Word 1
Ch 0, Word 2
Ch 0, Word 3
Ch 1, Word 0
Ch 1, Word 1
Ch 1, Word 2
Ch 1, Word 3
51.9.3
1000h
1002h
1004h
1006h
1008h
100Ah
100Ch
100Eh
0001
0001
0001
0001
0001
0001
0001
0001
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0010
0100
0110
1000
1010
1100
1110
When using the Threshold Detect operation with the Extended DMA, it is important to note that
the Write mode must be configured to convert and save (ASINT<1:0> = 01), or else the
converted values will not be saved in the desired memory location. Also, if a channel fails to
generate a valid comparison, the undesired value will not be written to memory, which will be
interpreted by the DMA as an incomplete DMA session. As such, the user should poll the
AD1CHITL and/or AD1CHITH register to determine if a given memory location contains valid
data. For more information on DMA operation, refer to Section 54. Direct Memory Access
Controller (DMA). To learn more about Threshold Detect operation, refer to Section 51.10
Threshold Detect Operation.
DS39739B-page 51-36
The LPEN bit (AD1CON5<14>) allows Threshold Detect to function with a low-power feature. By
design, Threshold Detect can perform comparison operations when the device is in Sleep or Idle
modes, waking the CPU when it generates an interrupt. Setting LPEN configures the device to
return to low-power operation after the interrupt has been serviced.
The Compare Mode bits, CM<1:0> (AD1CON5<1:0>), select the type of comparison to be
performed. Four types are available:
The Write Mode bits, WM<1:0> (AD1CON5<3:2>), determine the disposition of the conversion.
Three options are available:
Discard the conversion after the comparison has been performed
Store the conversion after the comparison has been performed
Store the conversion without comparison (Legacy mode)
DS39739B-page 51-37
DS39739B-page 51-38
51
The following examples show the effect of valid comparisons on the results buffer and the
Compare Hit registers. In each figure, changes within the registers are indicated in bold.
For the sake of simplicity, the examples assume a device with only 16 analog inputs. Devices
with a greater number of channels, and thus, larger results buffers and two Compare Hit
registers, will function in a similar fashion.
When using any comparison mode, always use channel indexed buffer storage
(BUFREGEN = 1). Otherwise, the threshold values for other channels may be
overwritten, resulting in unpredictable comparisons.
Note:
ADC1BUF14
ADC1BUF13
ADC1BUF12
ADC1BUF11
Compare and
Store (01)
ADC1BUF15
ADC1BUF14
ADC1BUF13
ADC1BUF10
ADC1BUF12
ADC1BUF9
ADC1BUF11
ADC1BUF8
ADC1BUF10
ADC1BUF7
ADC1BUF9
ADC1BUF6
ADC1BUF8
ADC1BUF5
ADC1BUF7
ADC1BUF4
ADC1BUF6
ADC1BUF3
ADC1BUF5
ADC1BUF2
Threshold Value
ADC1BUF4
ADC1BUF1
ADC1BUF3
ADC1BUF0
ADC1BUF2
Threshold Value
Conversion Value
ADC1BUF1
ADC1BUF0
AD1CHITL
AD1CHITL
15
14
13
12
11
10
15
14
13
12
11
10
DS39739B-page 51-39
ADC1BUF14
ADC1BUF13
Compare and
Store (01)
ADC1BUF15
ADC1BUF14
ADC1BUF13
Threshold 2
ADC1BUF12
ADC1BUF11
ADC1BUF8
ADC1BUF10
Threshold 2
Threshold 2
ADC1BUF7
ADC1BUF9
ADC1BUF8
ADC1BUF7
ADC1BUF6
ADC1BUF5
Threshold 1
ADC1BUF4
ADC1BUF3
ADC1BUF2
Threshold 1
Conversion Value
ADC1BUF1
ADC1BUF0
ADC1BUF12
ADC1BUF11
ADC1BUF10
ADC1BUF9
ADC1BUF6
ADC1BUF5
ADC1BUF4
ADC1BUF3
ADC1BUF2
ADC1BUF1
ADC1BUF0
AD1CHITL
AD1CHITL
15
14
13
12
11
10
15
14
13
12
11
10
DS39739B-page 51-40
51
When the Compare Mode bits CM<1:0> are programmed as 11, the converter compares the
sampled value to see if it falls outside of the threshold values in the buffer and mirrored channel
location. Again, since the value in the mirrored channel location is always the greater value of
the two thresholds, the condition is met when either:
Converted Value >Threshold 2
or
Threshold 1 > Converted Value
In these cases, the following occurs:
The Compare Hit bit (CHHn) for the corresponding channel is set.
If the converted value is greater than Threshold 2, the CHHn bit for the mirrored channel is
also set. If it is less than Threshold 1, the mirrored channel bit remains 0.
If the Write Mode bits, WM<1:0> (AD1CON5<3:2>), are programmed to 01:
- If the converted value is above Threshold 2, the converted value is written to the
mirrored channel address, replacing the upper threshold value.
- If the converted value is below Threshold 1, the converted value is written to the
channel address, replacing the lower threshold value.
If WM<1:0> = 10, the converted value is discarded.
The changes to the result buffer and the Compare Hit register are shown in Figure 51-15 (over
the upper threshold) and Figure 51-16 (under the lower threshold).
Note that when a Windowed Comparison mode is selected and channel mirroring is enabled,
nothing prevents a conversion from another operation from being stored in the mirrored channel
location. In the previous examples of windowed operation, if AN10 is included in a Threshold
Detect operation, a conversion on AN10 might be tested against the upper threshold for AN2,
stored in that location. This could result in the threshold value being overwritten and/or the
CHH10 bit being set.
For this reason, users must always carefully consider the allocation and use of the upper analog
channels (both external and internal) when using Windowed Compare modes. Wherever
possible, exclude the upper analog channels for Threshold Detect operations, and convert and
test those channels in a separate routine.
DS39739B-page 51-41
ADC1BUF14
ADC1BUF13
ADC1BUF12
ADC1BUF11
Compare and
Store (01)
ADC1BUF15
ADC1BUF14
ADC1BUF13
ADC1BUF10
Threshold 2
ADC1BUF12
ADC1BUF9
ADC1BUF11
ADC1BUF8
ADC1BUF10
Threshold 2
Conversion Value
ADC1BUF7
ADC1BUF9
ADC1BUF6
ADC1BUF8
ADC1BUF5
ADC1BUF7
ADC1BUF4
ADC1BUF6
ADC1BUF3
ADC1BUF5
ADC1BUF2
Threshold 1
ADC1BUF4
ADC1BUF1
ADC1BUF3
ADC1BUF0
ADC1BUF2
Threshold 1
Threshold 1
ADC1BUF1
ADC1BUF0
AD1CHITL
AD1CHITL
15
14
13
12
11
10
15
DS39739B-page 51-42
14
13
12
11
10
8
0
51
Before Conversion and Comparison
ADC1BUF15
ADC1BUF14
ADC1BUF13
Compare and
Store (01)
ADC1BUF15
ADC1BUF14
ADC1BUF13
Threshold 2
ADC1BUF12
ADC1BUF11
ADC1BUF8
ADC1BUF10
Threshold 2
Threshold 2
ADC1BUF7
ADC1BUF9
ADC1BUF8
ADC1BUF7
ADC1BUF6
ADC1BUF5
Threshold 1
ADC1BUF4
ADC1BUF3
ADC1BUF2
Threshold 1
Conversion Value
ADC1BUF1
ADC1BUF0
ADC1BUF12
ADC1BUF11
ADC1BUF10
ADC1BUF9
ADC1BUF6
ADC1BUF5
ADC1BUF4
ADC1BUF3
ADC1BUF2
ADC1BUF1
ADC1BUF0
AD1CHITL
AD1CHITL
15
14
13
12
11
10
15
14
13
12
11
10
DS39739B-page 51-43
TSAMP
TSAMP
TSAMP
A/D CLK
TCONV
Analog Input
AN0
TCONV
AN0
TCONV
AN0
TCONV
AN0
ASAM
SAMP
DONE
ADC1BUF0
ADC1BUF1
ADC1BUFE
ADC1BUFF
AD1IF
BSF AD1CON1, ASAM
DS39739B-page 51-44
Instruction Execution
51
Sampling and Converting a Single Channel Multiple Times
//
//
//
//
//
//
//
//
clear value
initialize ADC1BUF pointer
clear ADC interrupt flag
auto start sampling for 31Tad
then go to conversion
conversion done?
yes then stop sample/convert
average the 16 ADC value
while (!IFS0bits.AD1IF){};
AD1CON1bits.ASAM = 0;
for (count = 0; count < 16; count++)
{
ADCValue = ADCValue + *ADC16Ptr++;
}
ADCValue = ADCValue >> 4;
// repeat
DS39739B-page 51-45
Example 51-6:
A/D Configuration:
Operational Sequence:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
DS39739B-page 51-46
Buffer Contents
at 1st AD1IF Event
AN0, Sample 1
AN0, Sample 2
AN0, Sample 3
AN0, Sample 4
AN0, Sample 5
AN0, Sample 6
AN0, Sample 7
AN0, Sample 8
AN0, Sample 9
AN0, Sample 10
AN0, Sample 11
AN0, Sample 12
AN0, Sample 13
AN0, Sample 14
AN0, Sample 15
AN0, Sample 16
Buffer Contents
at 2nd AD1IF Event
AN0, Sample 17
AN0, Sample 18
AN0, Sample 19
AN0, Sample 20
AN0, Sample 21
AN0, Sample 22
AN0, Sample 23
AN0, Sample 24
AN0, Sample 25
AN0, Sample 26
AN0, Sample 27
AN0, Sample 28
AN0, Sample 29
AN0, Sample 30
AN0, Sample 31
AN0, Sample 32
51
Figure 51-18 and Example 51-9 illustrate a typical setup, where all available analog input channels
are sampled and converted. In this instance, 16 analog inputs are assumed. The set CSCNA bit
specifies scanning of the A/D inputs to the S/H positive input. Other conditions are similar to those
located in Section 51.11.1 Sampling and Converting a Single Channel Multiple Times.
Initially, the AN0 input is sampled and converted. The result is stored in the ADC1BUFn buffer.
Then, the AN1 input is sampled and converted. This process of scanning the inputs repeats
16 times, until the buffer is full, and then the module generates an interrupt. The entire process
will then repeat.
Figure 51-18: Scanning All 16 Inputs Per Single Interrupt
Conversion
Trigger
TSAMP
TSAMP
TSAMP
TSAMP
A/D CLK
TCONV
TCONV
AN0
Analog Input
AN1
TCONV
AN14
TCONV
AN15
ASAM
SAMP
DONE
ADC1BUF0
ADC1BUF1
ADC1BUFE
ADC1BUFF
AD1IF
BSET AD1CON1, #ASAM
Example 51-8:
Instruction Execution
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
clear value
initialize ADC1BUF pointer
clear ADC interrupt flag
auto start sampling for 31Tad
then go to conversion
conversion done?
yes then stop sample/convert
average the 16 ADC value
while (!IFS0bits.AD1IF){};
AD1CON1bits.ASAM = 0;
for (count = 0; count < 16; count++)
{
ADCValue = ADCValue + *ADC16Ptr++;
}
ADCValue = ADCValue >> 4;
// repeat
DS39739B-page 51-47
A/D Configuration:
Operational Sequence:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
DS39739B-page 51-48
Buffer Contents
at 1st AD1IF Event
Sample 1 (AN0, Sample 1)
Sample 2 (AN1, Sample 1)
Sample 3 (AN2, Sample 1)
Sample 4 (AN3, Sample 1)
Sample 5 (AN4, Sample 1)
Sample 6 (AN5, Sample 1)
Sample 7 (AN6, Sample 1)
Sample 8 (AN7, Sample 1)
Sample 9 (AN8, Sample 1)
Sample 10 (AN9, Sample 1)
Sample 11 (AN10, Sample 1)
Sample 12 (AN11, Sample 1)
Sample 13 (AN12, Sample 1)
Sample 14 (AN13, Sample 1)
Sample 15 (AN14, Sample 1)
Sample 16 (AN15, Sample 1)
Buffer Contents
at 2nd AD1IF Event
Sample 17 (AN0, Sample 2)
Sample 18 (AN1, Sample 2)
Sample 19 (AN2, Sample 2)
Sample 20 (AN3, Sample 2)
Sample 21 (AN4, Sample 2)
Sample 22 (AN5, Sample 2)
Sample 23 (AN6, Sample 2)
Sample 24 (AN7, Sample 2)
Sample 25 (AN8, Sample 2)
Sample 26 (AN9, Sample 2)
Sample 27 (AN10, Sample 2)
Sample 28 (AN11, Sample 2)
Sample 29 (AN12, Sample 2)
Sample 30 (AN13, Sample 2)
Sample 31 (AN14, Sample 2)
Sample 32 (AN15, Sample 2)
51
Figure 51-19 and Example 51-10 demonstrate using dual buffers and alternating the buffer fill.
Setting the BUFM bit enables dual buffers. In this example, an interrupt is generated after each
sample. The BUFM setting does not affect other operational parameters. First, the conversion
sequence starts filling the buffer at ADC1BUF0. After the first interrupt occurs, the buffer begins
to fill at ADC1BUF8. The BUFS status bit is toggled after each interrupt.
Figure 51-19: Converting a Single Channel, Once Per Interrupt, Using Dual, 8-Word Buffers
Conversion
Trigger
TSAMP
TSAMP
TSAMP
A/D CLK
TCONVTCONVTCONVTCONV
Analog Input
AN3
TCONVTCONVTCONVTCONV
TCONVTCONVTCONVTCONV
AN3
AN3
SAMP
BUFS
ADC1BUF0
ADC1BUF8
AD1IF
BSET AD1CON1, #ASAM
Instruction Execution
DS39739B-page 51-49
Operational Sequence:
1.
2.
3.
4.
5.
DS39739B-page 51-50
Buffer Contents
at 1st AD1IF Event
Sample 1 (AN3, Sample 1)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
Buffer Contents
at 2nd AD1IF Event
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
Sample 2 (AN3, Sample 2)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
51
Figure 51-20 and Example 51-11 demonstrate alternate sampling of the inputs assigned to
MUX A and MUX B. Setting the ALTS bit enables alternating input selections. The first sample
uses the MUX A inputs specified by the CH0SAx and CH0NAx bits. The next sample uses the
MUX B inputs, specified by the CH0SBx and CH0NBx bits.
This example also demonstrates use of the dual, 8-word buffers. An interrupt occurs after every
8th sample, resulting in filling 8 words into the buffer on each interrupt.
Figure 51-20: Converting Two Inputs Using Alternating Input Selections
Conversion
Trigger
TSAMP
TSAMP
TSAMP
TSAMP
TSAMP
A/D CLK
TCONVTCONV
Analog
Input
AN1
TCONVTCONV
AN15
TCONVTCONV
AN15
TCONVTCONV
AN1
TCONVTCONV
AN15
ASAM
SAMP
Cleared
in Software
DONE
BUFS
ADC1BUF0
ADC1BUF1
ADC1BUF2
ADC1BUF3
ADC1BUF4
ADC1BUF5
ADC1BUF6
ADC1BUF7
ADC1BUF8
ADC1BUF9
ADC1BUFA
ADC1BUFB
AD1IF
Cleared by Software
DS39739B-page 51-51
Operational Sequence:
1.
2.
3.
4.
5.
6.
7.
8.
9.
DS39739B-page 51-52
Buffer Contents
at 1st AD1IF Event
Sample 1 (AN1, Sample 1)
Sample 2 (AN15, Sample 1)
Sample 3 (AN1, Sample 2)
Sample 4 (AN15, Sample 2)
Sample 5 (AN1, Sample 3)
Sample 6 (AN15, Sample 3)
Sample 7 (AN1, Sample 4)
Sample 8 (AN15, Sample 4)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
Buffer Contents
at 2nd AD1IF Event
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
(undefined)
Sample 9 (AN1, Sample 5)
Sample 10 (AN15, Sample 5)
Sample 11 (AN1, Sample 6)
Sample 12 (AN15, Sample 6)
Sample 13 (AN1, Sample 7)
Sample 14 (AN15, Sample 7)
Sample 15 (AN1, Sample 8)
Sample 16 (AN15, Sample 8)
51
A/D SAMPLING REQUIREMENTS
The Analog Input model of the 12-bit A/D Converter is shown in Figure 51-21. The total sampling
time for the A/D is a function of the holding capacitor charge time.
For the A/D Converter to meet its specified accuracy, the charge holding capacitor (CHOLD) must
be allowed to fully charge to the voltage level on the analog input pin. The source impedance
(RS), the interconnect impedance (RIC) and the internal sampling switch (RSS) impedance
combine to directly affect the time required to charge CHOLD. The combined impedance of the
analog sources must, therefore, be small enough to fully charge the holding capacitor within the
chosen sample time. To minimize the effects of pin leakage currents on the accuracy of the A/D
Converter, the maximum recommended source impedance, RS, is 2.5 k. After the analog input
channel is selected (changed), this sampling function must be completed prior to starting the
conversion. The internal holding capacitor will be in a discharged state prior to each sample
operation.
At least 1 TAD time period should be allowed between conversions for the sample time. For more
details, see Section 51.18 Electrical Specifications.
Figure 51-21: 12-Bit A/D Converter Analog Input Model
RIC 250
Rs
VA
ANx
Sampling
Switch
RSS
CPIN
ILEAKAGE
500 nA
RSS 3 k
CHOLD
= 4.4 pF
VSS
Legend: CPIN
= Input Capacitance
VT
= Threshold Voltage
ILEAKAGE = Leakage Current at the Pin due to
Various Junctions
RIC
= Interconnect Resistance
RSS
= Sampling Switch Resistance
CHOLD
= Sample/Hold Capacitance (from DAC)
Note: CPIN value depends on device package and is not tested. The effect of the CPIN is negligible if Rs 5 k.
DS39739B-page 51-53
51.12
TRANSFER FUNCTIONS
The transfer functions of the A/D Converter, in 12-bit and 10-bit resolution, are shown in
Figure 51-22 and Figure 51-23, respectively. In both cases, the difference of the input voltages,
(VINH VINL), is compared to the reference, ((VR+) (VR-)).
For the 12-bit transfer function:
The first code transition occurs when the input voltage is ((VR+) (VR-))/4096 or 1.0 LSb.
The 0000 0000 0001 code is centered at VR- + (1.5 * ((VR+) (VR-))/4096).
The 0010 0000 0000 code is centered at VREFL + (2048.5 * ((VR+) (VR-))/4096).
An input voltage less than VR- + (((VR-) (VR-))/4096) converts as 0000 0000 0000.
An input voltage greater than (VR-) + (4096((VR+) (VR-))/4096) converts
as 1111 1111 1111.
DS39739B-page 51-54
(VINH VINL)
VR+
4096
VR- +
4096
VR- +
4096
VR- +
Voltage Level
VR+ VR-
0
VR-
51
The first code transition occurs when the input voltage is ((VR+) (VR-))/1024 or 1.0 LSb.
The 00 0000 0001 code is centered at VR- + (1.5 * (((VR+) (VR-))/1024).
The 10 0000 0000 code is centered at VREFL + (512.5 * (((VR+) (VR-))/1024).
An input voltage less than VR- + (((VR-) (VR-))/1024) converts as 00 0000 0000.
An input voltage greater than (VR-) + ((1023 (VR+)) (VR-))/1024) converts as 11 1111 1111.
(VINH VINL)
VR+
1024
VR- +
1024
VR- +
1024
VR- +
Voltage Level
VR+ VR-
0
VR-
DS39739B-page 51-55
A/D ACCURACY/ERROR
Refer to Section 51.20 Related Application Notes for a list of documents that discuss A/D
accuracy.
51.15
For the A/D module to operate in Sleep, the A/D clock source must be set to RC
(ADRC = 1).
DS39739B-page 51-56
51
The Peripheral Module Disable (PMD) registers provide a method to disable the A/D module by
stopping all clock sources supplied to that module. When a peripheral is disabled via the appropriate PMDx control bit, the peripheral is in a minimum power consumption state. The control and
status registers associated with the peripheral will also be disabled, so writes to those registers
will have no effect and read values will be invalid. The A/D module is enabled only when the
ADC1MD bit in the PMDx register is cleared.
51.16
EFFECTS OF A RESET
A device Reset forces all registers to their Reset state. This forces the A/D module to be turned
off and any conversion in progress to be aborted. All pins that are multiplexed with analog inputs
will be configured as analog inputs. The corresponding TRIS bits will be set.
The values in the ADC1BUFn registers are not initialized during a Power-on Reset; they will
contain unknown data.
DS39739B-page 51-57
REGISTER MAPS
A summary of the registers associated with the PIC24F 10-Bit A/D Converter with Threshold Detect is provided in Table 51-7.
Table 51-7:
File Name
Bit 14
Bit 13
Bit 12
Bit 11
Bit 10
Bit 9
Bit 8
Bit 7
Bit 6
Bit 5
Bit 4
Bit 3
Bit 2
Bit 1
Bit 0
All
Resets
ADC1BUF0
xxxx
ADC1BUF1
xxxx
ADC1BUF2
xxxx
ADC1BUF3
xxxx
ADC1BUF4
xxxx
ADC1BUF5
xxxx
ADC1BUF6
xxxx
ADC1BUF7
xxxx
ADC1BUF8
xxxx
ADC1BUF9
xxxx
ADC1BUFn(1)
xxxx
AD1CON1
ADON
ADSIDL
DMABM(2)
DMAEN(2)
MODE12(2)
FORM1
FORM0
SSRC3
SSRC2
SSRC1
SSRC0
ASAM
SAMP
DONE
0000
AD1CON2
PVCFG1
PVCFG0
NVCFG
OFFCAL
BUFREGEN
CSCNA
BUFS
SMPI4
SMPI3
SMPI2
SMPI1
SMPI0
BUFM
ALTS
0000
AD1CON3
ADRC
SAMC4
SAMC3
SAMC2
SAMC1
SAMC0
ADCS7
ADCS6
ADCS5
ADCS4
ADCS3
ADCS2
ADCS1
ADCS0
0000
AD1CON4(4)
EXTSAM PUMPEN(2)
CH0NB2
CH0NB1
CH0NB0
CH0SB4
CH0SB3
CH0SB2
CH0SB1
CH0SB0
CH0NA2
CH0NA1
CH0NA0
CH0SA4
CH0SA3
CH0SA2
CH0SA1
CH0SA0
0000
AD1CSSH(3)
CSS31
CSS30
CSS29
CSS28
CSS27
CSS26
CSS25
CSS24
CSS23
CSS22
CSS21
CSS20
CSS19
CSS18
CSS17
CSS16
xxxx
AD1CSSL
CSS15
CSS14
CSS13
CSS12
CSS11
CSS10
CSS9
CSS8
CSS7
CSS6
CSS5
CSS4
CSS3
CSS2
CSS1
CSS0
0000
AD1CON5
ASEN
LPEN
CTMREQ
ASINT1
ASINT0
WM1
WM0
CM1
CM0
0000
AD1CHITH(3)
CHH31
CHH30
CHH29
CHH28
CHH27
CHH26
CHH25
CHH24
CHH23
CHH22
CHH21
CHH20
CHH19
CHH18
CHH17
CHH16
0000
ADCCHITL
CHH15
CHH14
CHH13
CHH12
CHH11
CHH10
CHH9
CHH8
CHH7
CHH6
CHH5
CHH4
CHH3
CHH2
CHH1
CHH0
0000
AD1CHS
BGREQ(2) VRSREQ(2)
0000
CTMEN27
CTMEN18
CTMEN17
CTMEN16
0000
AD1CTMENL
CTMEN11
CTMEN10
CTMEN2
CTMEN1
CTMEN0
0000
CTMEN9
CTMEN8
CTMEN7
CTMEN6
CTMEN5
CTMEN4
CTMEN3
x = unknown value on Reset, = unimplemented, read as 0. Reset values are shown in hexadecimal.
The number of AD1BUF registers is equal to at least the number of external analog channels, rounded up the nearest even number. Refer to the specific device data sheet for the exact number.
These bits are not implemented in all devices. Refer to the specific device data sheet.
These registers and bits are implemented in devices with more than 16 analog channels only. Not all bits may be implemented in H registers. Refer to the specific device data sheet.
AD1CON4 is not implemented on all devices. For more information, refer to the specific device data sheet.
The DMABL<2:0> bits are only used when AD1CON1<11> = 1; otherwise, their value is ignored.
51
DS39739B-page 51-58
Legend:
Note 1:
2:
3:
4:
5:
51.17
51
ELECTRICAL SPECIFICATIONS
(Note 2)
131
Q3/Q4
132
A/D CLK
135
130
(1)
A/D DATA
... ...
OLD DATA
ADC1BUFn
NEW DATA
AD1IF
TCY
SAMP
SAMPLING STOPPED
Note 1: If the A/D clock source is selected as RC, a time of TCY is added before the A/D clock starts. This allows the SLEEP
instruction to be executed.
2: This is a minimal RC delay (typically 100 ns) which also disconnects the holding capacitor from the analog input.
Table 51-8:
Param
Symbol
No.
AD61
tPSS
Characteristic
Sample Start Delay from Setting SAMP
Min
Typ
Max
Units
Conditions
TAD
300
ns
TOSC-based
250
ns
A/D RC mode
12
14
TAD
10-bit result
12-bit result (Note 1)
Acquisition Time
750
ns
(Note 2)
(Note 3)
Discharge Time
TAD
300
ns
AD130 TAD
AD131 TCNV
Conversion Time
(not including acquisition time)
AD132 TACQ
AD135 TSWC
AD137 TDIS
Note 1: The ADC1BUFn register may be read on the following TCY cycle.
2: The time for the holding capacitor to acquire the New input voltage when the voltage changes full scale
after the conversion (AVDD to AVSS or AVSS to AVDD).
3: On the following cycle of the device clock.
4: Specifications may change between devices. Refer to the family data sheet for information specific to a
given device.
DS39739B-page 51-59
51.18
DESIGN TIPS
Question 1:
Answer: There are three main things to consider in optimizing A/D performance:
1.
2.
3.
Make sure you are meeting all of the timing specifications. If you are turning the module
off and on, there is a minimum delay you must wait before taking a sample. If you are
changing input channels, there is a minimum delay you must wait for this as well, and
finally, there is TAD, which is the time selected for each bit conversion. This is selected in
AD1CON3 and should be within a certain range, as specified in Section 51.18 Electrical
Specifications. If TAD is too short, the result may not be fully converted before the conversion is terminated, and if TAD is made too long, the voltage on the sampling capacitor
can decay before the conversion is complete. These timing specifications are provided in
the Electrical Characteristics section of the device data sheets.
Often, the source impedance of the analog signal is high (greater than 2.5 k), so the
current drawn from the source by leakage, and to charge the sample capacitor, can affect
accuracy. If the input signal does not change too quickly, try putting a 0.1 F capacitor on
the analog input. This capacitor will charge to the analog voltage being sampled and
supply the instantaneous current needed to charge the internal holding capacitor.
Put the device into Sleep mode before the start of the A/D conversion. The RC clock
source selection is required for conversions in Sleep mode. This technique increases
accuracy, because digital noise from the CPU and other peripherals is minimized.
Question 2:
Answer: A good reference for understanding A/D conversions is the Analog-Digital Conversion
Handbook third edition, published by Prentice Hall (ISBN 0-13-03-2848-0).
Question 3:
Answer: This configuration is not recommended. The buffer will contain unknown results.
DS39739B-page 51-60
51
RELATED APPLICATION NOTES
This section lists application notes that are related to this section of the manual. These
application notes may not be written specifically for the PIC24F device family, but the concepts
are pertinent and could be used with modification and possible limitations. The current
application notes related to the 12-Bit A/D Converter with Threshold Detect module are:
Title
Application Note #
Note:
AN546
AN557
AN693
Please visit the Microchip web site (www.microchip.com) for additional application
notes and code examples for the PIC24F family of devices.
DS39739B-page 51-61
51.20
REVISION HISTORY
Revision A (March 2011)
This is the initial released revision of this document.
DS39739B-page 51-62
Note the following details of the code protection feature on Microchip devices:
Microchip products meet the specification contained in their particular Microchip Data Sheet.
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
Microchip is willing to work with the customer who is concerned about the integrity of their code.
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 Microchips 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.
Trademarks
The Microchip name and logo, the Microchip logo, dsPIC,
KEELOQ, KEELOQ logo, MPLAB, PIC, PICmicro, PICSTART,
PIC32 logo, rfPIC and UNI/O are registered trademarks of
Microchip Technology Incorporated in the U.S.A. and other
countries.
FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor,
MXDEV, MXLAB, SEEVAL and The Embedded Control
Solutions Company are registered trademarks of Microchip
Technology Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, chipKIT,
chipKIT logo, CodeGuard, dsPICDEM, dsPICDEM.net,
dsPICworks, dsSPEAK, ECAN, ECONOMONITOR,
FanSense, HI-TIDE, In-Circuit Serial Programming, ICSP,
Mindi, MiWi, MPASM, MPLAB Certified logo, MPLIB,
MPLINK, mTouch, Omniscient Code Generation, PICC,
PICC-18, PICDEM, PICDEM.net, PICkit, PICtail, REAL ICE,
rfLAB, Select Mode, Total Endurance, TSHARC,
UniWinDriver, WiperLock 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.
All other trademarks mentioned herein are property of their
respective companies.
2011, Microchip Technology Incorporated, Printed in the
U.S.A., All Rights Reserved.
Printed on recycled paper.
ISBN: 978-1-61341-869-7
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 Companys 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, Microchips quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
DS39739B-page 51-63
ASIA/PACIFIC
ASIA/PACIFIC
EUROPE
Corporate Office
2355 West Chandler Blvd.
Chandler, AZ 85224-6199
Tel: 480-792-7200
Fax: 480-792-7277
Technical Support:
http://www.microchip.com/
support
Web Address:
www.microchip.com
India - Bangalore
Tel: 91-80-3090-4444
Fax: 91-80-3090-4123
India - New Delhi
Tel: 91-11-4160-8631
Fax: 91-11-4160-8632
Austria - Wels
Tel: 43-7242-2244-39
Fax: 43-7242-2244-393
Denmark - Copenhagen
Tel: 45-4450-2828
Fax: 45-4485-2829
India - Pune
Tel: 91-20-2566-1512
Fax: 91-20-2566-1513
France - Paris
Tel: 33-1-69-53-63-20
Fax: 33-1-69-30-90-79
Japan - Osaka
Tel: 81-66-152-7160
Fax: 81-66-152-9310
Germany - Munich
Tel: 49-89-627-144-0
Fax: 49-89-627-144-44
Atlanta
Duluth, GA
Tel: 678-957-9614
Fax: 678-957-1455
Boston
Westborough, MA
Tel: 774-760-0087
Fax: 774-760-0088
Chicago
Itasca, IL
Tel: 630-285-0071
Fax: 630-285-0075
Cleveland
Independence, OH
Tel: 216-447-0464
Fax: 216-447-0643
Dallas
Addison, TX
Tel: 972-818-7423
Fax: 972-818-2924
Detroit
Farmington Hills, MI
Tel: 248-538-2250
Fax: 248-538-2260
Indianapolis
Noblesville, IN
Tel: 317-773-8323
Fax: 317-773-5453
Los Angeles
Mission Viejo, CA
Tel: 949-462-9523
Fax: 949-462-9608
Santa Clara
Santa Clara, CA
Tel: 408-961-6444
Fax: 408-961-6445
Toronto
Mississauga, Ontario,
Canada
Tel: 905-673-0699
Fax: 905-673-6509
Australia - Sydney
Tel: 61-2-9868-6733
Fax: 61-2-9868-6755
China - Beijing
Tel: 86-10-8569-7000
Fax: 86-10-8528-2104
China - Chengdu
Tel: 86-28-8665-5511
Fax: 86-28-8665-7889
China - Chongqing
Tel: 86-23-8980-9588
Fax: 86-23-8980-9500
Korea - Daegu
Tel: 82-53-744-4301
Fax: 82-53-744-4302
China - Hangzhou
Tel: 86-571-2819-3187
Fax: 86-571-2819-3189
Korea - Seoul
Tel: 82-2-554-7200
Fax: 82-2-558-5932 or
82-2-558-5934
China - Nanjing
Tel: 86-25-8473-2460
Fax: 86-25-8473-2470
Malaysia - Penang
Tel: 60-4-227-8870
Fax: 60-4-227-4068
China - Qingdao
Tel: 86-532-8502-7355
Fax: 86-532-8502-7205
Philippines - Manila
Tel: 63-2-634-9065
Fax: 63-2-634-9069
China - Shanghai
Tel: 86-21-5407-5533
Fax: 86-21-5407-5066
Singapore
Tel: 65-6334-8870
Fax: 65-6334-8850
China - Shenyang
Tel: 86-24-2334-2829
Fax: 86-24-2334-2393
China - Shenzhen
Tel: 86-755-8203-2660
Fax: 86-755-8203-1760
Taiwan - Kaohsiung
Tel: 886-7-536-4818
Fax: 886-7-330-9305
China - Wuhan
Tel: 86-27-5980-5300
Fax: 86-27-5980-5118
Taiwan - Taipei
Tel: 886-2-2500-6610
Fax: 886-2-2508-0102
China - Xian
Tel: 86-29-8833-7252
Fax: 86-29-8833-7256
Thailand - Bangkok
Tel: 66-2-694-1351
Fax: 66-2-694-1350
Italy - Milan
Tel: 39-0331-742611
Fax: 39-0331-466781
Netherlands - Drunen
Tel: 31-416-690399
Fax: 31-416-690340
Spain - Madrid
Tel: 34-91-708-08-90
Fax: 34-91-708-08-91
UK - Wokingham
Tel: 44-118-921-5869
Fax: 44-118-921-5820
China - Xiamen
Tel: 86-592-2388138
Fax: 86-592-2388130
China - Zhuhai
Tel: 86-756-3210040
Fax: 86-756-3210049
DS39739B-page 51-64
Japan - Yokohama
Tel: 81-45-471- 6166
Fax: 81-45-471-6122
11/29/11