Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
17
10-bit Analog-to-Digital Converter (ADC)
DS61104E-page 17-1
17.1
INTRODUCTION
The PIC32 10-bit Analog-to-Digital Converter (ADC) includes the following features: Successive Approximation Register (SAR) conversion Up to 16 analog input pins External voltage reference input pins One unipolar differential Sample-and-Hold Amplifier (SHA) Automatic Channel Scan mode Selectable conversion trigger source 16-word conversion result buffer Selectable Buffer Fill modes Eight conversion result format options Operation during CPU Sleep and Idle modes
Figure 17-1 illustrates a block diagram of the 10-bit ADC. The 10-bit ADC can have up to 16 analog input pins, AN0 through AN15. In addition, there are two analog input pins for external voltage reference connections. These voltage reference inputs may be shared with other analog input pins and may be common to other analog module references. The actual number of analog input pins and external voltage reference input configuration will depend on the specific PIC32 device. Refer to the specific device data sheet for more information. The analog inputs are connected through two multiplexers to one SHA. The analog input multiplexers can be switched between two sets of analog inputs between conversions. Unipolar differential conversions are possible on all channels, other than the pin used as the reference, using a reference input pin (see Figure 17-1). The Analog Input Scan mode sequentially converts user-specified channels. A control register specifies which analog input channels will be included in the scanning sequence. The 10-bit ADC is connected to a 16-word result buffer. Each 10-bit result is converted to one of eight 32-bit output formats when it is read from the result buffer.
DS61104E-page 17-2
VCFG<2:0> AN0 AN15 Channel Scan CH0SA<3:0> CH0SB<3:0> + SAR ADC ADC1BUF0 ADC1BUF1 ADC1BUF2
SHA
VREFH
VREFL
17
10-bit Analog-to-Digital Converter (ADC)
Alternate Input Selection Note 1: VREF+ and VREF- inputs can be multiplexed with other analog inputs.
DS61104E-page 17-3
SMPI<3:0>
= unimplemented, read as 0. This register has an associated Clear register at an offset of 0x4 bytes. These registers have the same name with CLR appended to the end of the register name (e.g.,AD1CON1CLR). Writing a 1 to any bit position in the Clear register will clear valid bits in the associated register. Reads from the Clear register should be ignored. This register has an associated Set register at an offset of 0x8 bytes. These registers have the same name with SET appended to the end of the register name (e.g.,AD1CON1SET). Writing a 1 to any bit position in the Set register will set valid bits in the associated register. Reads from the Set register should be ignored. This register has an associated Invert register at an offset of 0xC bytes. These registers have the same name with INV appended to the end of the register name (e.g., AD1CON1INV). Writing a 1 to any bit position in the Invert register will invert valid bits in the associated register. Reads from the Invert register should be ignored.
DS61104E-page 17-4
31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0 31:0
ADC Result Word 0 (ADC1BUF0<31:0>) ADC Result Word 1 (ADC1BUF1<31:0>) ADC Result Word 2 (ADC1BUF2<31:0>) ADC Result Word 3 (ADC1BUF3<31:0>) ADC Result Word 4 (ADC1BUF4<31:0>) ADC Result Word 5 (ADC1BUF5<31:0>) ADC Result Word 6 (ADC1BUF6<31:0>) ADC Result Word 7 (ADC1BUF7<31:0>) ADC Result Word 8 (ADC1BUF8<31:0>) ADC Result Word 9 (ADC1BUF9<31:0>) ADC Result Word A (ADC1BUFA<31:0>) ADC Result Word B (ADC1BUFB<31:0>) ADC Result Word C (ADC1BUFC<31:0>) ADC Result Word D (ADC1BUFD<31:0>) ADC Result Word E (ADC1BUFE<31:0>) ADC Result Word F (ADC1BUFF<31:0>)
17
10-bit Analog-to-Digital Converter (ADC)
= unimplemented, read as 0. This register has an associated Clear register at an offset of 0x4 bytes. These registers have the same name with CLR appended to the end of the register name (e.g.,AD1CON1CLR). Writing a 1 to any bit position in the Clear register will clear valid bits in the associated register. Reads from the Clear register should be ignored. This register has an associated Set register at an offset of 0x8 bytes. These registers have the same name with SET appended to the end of the register name (e.g.,AD1CON1SET). Writing a 1 to any bit position in the Set register will set valid bits in the associated register. Reads from the Set register should be ignored. This register has an associated Invert register at an offset of 0xC bytes. These registers have the same name with INV appended to the end of the register name (e.g., AD1CON1INV). Writing a 1 to any bit position in the Invert register will invert valid bits in the associated register. Reads from the Invert register should be ignored.
DS61104E-page 17-5
Bit 27/19/11/3
U-0
Bit 26/18/10/2
U-0
Bit 25/17/9/1
U-0
Bit 24/16/8/0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
R/W-0
U-0
R/W-0
U-0
U-0
R/W-0 R/W-0
R/W-0
R/W-0 R/C-0
ON(1)
R/W-0
R/W-0
SIDL
R/W-0
R/W-0
U-0
FORM<2:0>
R/W-0
SSRC<2:0>
CLRASAM
ASAM
SAMP
DONE(2)
Legend: R = Readable bit U = Unimplemented bit bit 31-16 bit 15 W = Writable bit P = Programmable bit r = Reserved bit C = Clearable bit -n = Bit Value at POR: (0, 1, x = Unknown)
Unimplemented: Read as 0 ON: ADC Operating Mode bit(1) 1 = ADC module is operating 0 = ADC is off Unimplemented: Read as 0 SIDL: Stop in Idle Mode bit 1 = Discontinue module operation when device enters Idle mode 0 = Continue module operation in Idle mode Unimplemented: Read as 0 FORM<2:0>: Data Output Format bits 011 = Signed Fractional 16-bit (DOUT = 0000 0000 0000 0000 sddd dddd dd00 0000) 010 = Fractional 16-bit (DOUT = 0000 0000 0000 0000 dddd dddd dd00 0000) 001 = Signed Integer 16-bit (DOUT = 0000 0000 0000 0000 ssss sssd dddd dddd) 000 = Integer 16-bit (DOUT = 0000 0000 0000 0000 0000 00dd dddd dddd) 111 = Signed Fractional 32-bit (DOUT = sddd dddd dd00 0000 0000 0000 0000) 110 = Fractional 32-bit (DOUT = dddd dddd dd00 0000 0000 0000 0000 0000) 101 = Signed Integer 32-bit (DOUT = ssss ssss ssss ssss ssss sssd dddd dddd) 100 = Integer 32-bit (DOUT = 0000 0000 0000 0000 0000 00dd dddd dddd) SSRC<2:0>: Conversion Trigger Source Select bits 111 = Internal counter ends sampling and starts conversion (auto convert) 110 = Reserved 101 = Reserved 100 = Reserved 011 = Reserved 010 = Timer3 period match ends sampling and starts conversion 001 = Active transition on INT0 pin ends sampling and starts conversion 000 = Clearing SAMP bit ends sampling and starts conversion CLRASAM: Stop Conversion Sequence bit (when the first ADC interrupt is generated) 1 = Stop conversions when the first ADC interrupt is generated. Hardware clears the ASAM bit when the ADC interrupt is generated. 0 = Normal operation, buffer contents will be overwritten by the next conversion sequence When using the 1:1 Peripheral Bus Clock (PBCLK) divisor, the user software should not read or write the peripherals SFRs in the SYSCLK cycle immediately following the instruction that clears the modules ON bit. The DONE bit is not persistent in automatic modes. It is cleared by hardware at the beginning of the next sample.
bit 14 bit 13
bit 7-5
bit 4
Note 1:
2:
DS61104E-page 17-6
bit 1
bit 0
17
10-bit Analog-to-Digital Converter (ADC)
Note 1:
2:
DS61104E-page 17-7
Bit 27/19/11/3
U-0
Bit 26/18/10/2
U-0
Bit 25/17/9/1
U-0
Bit 24/16/8/0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
R/W-0 R/W-0
R/W-0
R/W-0 R/W-0
R/W-0
U-0
R/W-0
U-0
U-0
VCFG<2:0>
U-0
OFFCAL
R/W-0
R/W-0
CSCNA
R/W-0
R/W-0
R/W-0
BUFS
SMPI<3:0>
BUFM
ALTS
r = Reserved bit
Unimplemented: Read as 0 VCFG<2:0>: Voltage Reference Configuration bits ADC VR+ 000 001 010 011 1xx AVDD External VREF+ pin AVDD External VREF+ pin AVDD ADC VRAVSS AVSS External VREF- pin External VREF- pin AVSS
bit 12
bit 11 bit 10
OFFCAL: Input Offset Calibration Mode Select bit 1 = Enable Offset Calibration mode VINH and VINL of the SHA are connected to VR0 = Disable Offset Calibration mode The inputs to the SHA are controlled by AD1CHS or AD1CSSL Unimplemented: Read as 0 CSCNA: Scan Input Selections for CH0+ SHA Input for MUX A Input Multiplexer Setting bit 1 = Scan inputs 0 = Do not scan inputs Unimplemented: Read as 0 BUFS: Buffer Fill Status bit Only valid when BUFM = 1 (ADRES split into 2 x 8-word buffers). 1 = ADC is currently filling buffer 0x8-0xF, user should access data in 0x0-0x7 0 = ADC is currently filling buffer 0x0-0x7, user should access data in 0x8-0xF Unimplemented: Read as 0 SMPI<3:0>: Sample/Convert Sequences Per Interrupt Selection bits 1111 = Interrupts at the completion of conversion for each 16th sample/convert sequence 1110 = Interrupts at the completion of conversion for each 15th sample/convert sequence
bit 1
bit 0
0001 = Interrupts at the completion of conversion for each 2nd sample/convert sequence 0000 = Interrupts at the completion of conversion for each sample/convert sequence BUFM: ADC Result Buffer Mode Select bit 1 = Buffer configured as two 8-word buffers, ADC1BUF(7...0), ADC1BUF(15...8) 0 = Buffer configured as one 16-word buffer ADC1BUF(15...0.) ALTS: Alternate Input Sample Mode Select bit 1 = Uses MUX A input multiplexer settings for first sample, then alternates between MUX B and MUX A input multiplexer settings for all subsequent samples 0 = Always use MUX A input multiplexer settings
DS61104E-page 17-8
Bit 28/20/12/4
U-0
Bit 27/19/11/3
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
R/W-0
U-0
U-0
R/W-0 R/W-0
R/W-0 R/W-0
(1)
R/W-0
R/W-0 R/W-0
R/W-0 R/W-0
ADRC
R/W-0
R/W-0
R/W-0
SAMC<4:0>
R/W-0
17
10-bit Analog-to-Digital Converter (ADC)
ADCS<7:0>
Legend: R = Readable bit U = Unimplemented bit bit 31-16 bit 15 W = Writable bit P = Programmable bit r = Reserved bit -n = Bit Value at POR: (0, 1, x = Unknown)
Unimplemented: Read as 0 ADRC: ADC Conversion Clock Source bit 1 = ADC internal RC clock 0 = Clock derived from Peripheral Bus Clock (PBCLK) Unimplemented: Read as 0 SAMC<4:0>: Auto-sample Time bits 11111 = 31 TAD 00001 = 1 TAD 00000 = 0 TAD (Not allowed) ADCS<7:0>: ADC Conversion Clock Select bits(1) 11111111 = TPB 2 (ADCS<7:0> + 1) = 512 TPB = TAD 00000001 = TPB 2 (ADCS<7:0> + 1) = 4 TPB = TAD 00000000 = TPB 2 (ADCS<7:0> + 1) = 2 TPB = TAD TPB is the PIC32 Peripheral Bus clock time period. Refer to Section 6. Oscillator (DS61112) for more information.
bit 7-0
Note 1:
DS61104E-page 17-9
Bit 27/19/11/3
R/W-0 R/W-0 U-0
Bit 26/18/10/2
R/W-0 R/W-0 U-0
Bit 25/17/9/1
R/W-0 R/W-0 U-0
Bit 24/16/8/0
R/W-0 R/W-0 U-0
CH0NB
R/W-0
U-0
U-0
U-0
CH0SB<3:0> CH0SA<3:0>
U-0
CH0NA
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
Legend: R = Readable bit U = Unimplemented bit bit 31 W = Writable bit P = Programmable bit r = Reserved bit -n = Bit Value at POR: (0, 1, x = Unknown)
CH0NB: Negative Input Select bit for MUX B 1 = Channel 0 negative input is AN1 0 = Channel 0 negative input is VRUnimplemented: Read as 0 CH0SB<3:0>: Positive Input Select bits for MUX B 1111 = Channel 0 positive input is AN15 1110 = Channel 0 positive input is AN14 1101 = Channel 0 positive input is AN13 0001 = Channel 0 positive input is AN1 0000 = Channel 0 positive input is AN0 CH0NA: Negative Input Select bit for MUX A Multiplexer Setting 1 = Channel 0 negative input is AN1 0 = Channel 0 negative input is VRUnimplemented: Read as 0 CH0SA<3:0>: Positive Input Select bits for MUX A Multiplexer Setting 1111 = Channel 0 positive input is AN15 1110 = Channel 0 positive input is AN14 1101 = Channel 0 positive input is AN13 0001 = Channel 0 positive input is AN1 0000 = Channel 0 positive input is AN0 Unimplemented: Read as 0
bit 23
bit 15-0
DS61104E-page 17-10
Bit 27/19/11/3
U-0
Bit 26/18/10/2
U-0
Bit 25/17/9/1
U-0
Bit 24/16/8/0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
PCFG15
R/W-0
PCFG14
R/W-0
PCFG13
R/W-0
PCFG12
R/W-0
PCFG11
R/W-0
PCFG10
R/W-0
PCFG9
R/W-0
PCFG8
R/W-0
PCFG7
PCFG6
PCFG5
PCFG4
PCFG3
PCFG2
PCFG1
PCFG0
17
10-bit Analog-to-Digital Converter (ADC)
r = Reserved bit
Unimplemented: Read as 0 PCFG<15:0>: Analog Input Pin Configuration Control bits 1 = Analog input pin in Digital mode, port read input enabled, ADC input multiplexer input for this analog input connected to AVss 0 = Analog input pin in Analog mode, digital port read will return as a 1 without regard to the voltage on the pin, ADC samples pin voltage The AD1PCFG register functionality will vary depending on the number of ADC inputs available on the selected device. Refer to the specific device data sheet for additional details on this register. The AD1PCFG register is not available on all PIC32 devices. Refer to the specific device data sheet for availability of this register. AD1CSSL: ADC Input Scan Select Register(1) Bit 31/23/15/7
U-0
Note 1: 2:
Bit 27/19/11/3
U-0
Bit 26/18/10/2
U-0
Bit 25/17/9/1
U-0
Bit 24/16/8/0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
U-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
R/W-0
CSSL15
R/W-0
CSSL14
R/W-0
CSSL13
R/W-0
CSSL12
R/W-0
CSSL11
R/W-0
CSSL10
R/W-0
CSSL9
R/W-0
CSSL8
R/W-0
CSSL7
CSSL6
CSSL5
CSSL4
CSSL3
CSSL2
CSSL1
CSSL0
r = Reserved bit
Unimplemented: Read as 0 CSSL<15:0>: ADC Input Pin Scan Selection bits 1 = Select ANx for input scan 0 = Skip ANx for input scan The AD1CSSL register functionality will vary depending on the number of ADC inputs available on the selected device. Refer to the specific device data sheet for additional details on this register.
Note 1:
DS61104E-page 17-11
17.3.1
Overview of Operation
Analog sampling consists of two steps: acquisition and conversion (see Figure 17-2). During acquisition, the analog input pin is connected to the Sample and Hold Amplifier (SHA). After the pin has been sampled for a sufficient period, and the sample voltage is equivalent to the input, the pin is disconnected from the SHA to provide a stable input voltage for the conversion process. The conversion process then converts the analog sample voltage to a binary representation. An overview of the ADC is presented in Figure 17-1. The 10-bit ADC has a single SHA. The SHA is connected to the analog input pins through the analog input multiplexers, MUX A and MUX B. The analog input multiplexers are controlled by the AD1CHS register. There are two sets of MUX control bits in the AD1CHS register. These two sets of control bits allow the two different analog input to be independently controlled. The ADC can optionally switch between MUX A and MUX B configurations between conversions. The ADC can also optionally scan through a series of analog inputs using a single MUX. Acquisition time can be controlled manually or automatically. The acquisition time may be started manually by setting the SAMP bit (AD1CON1<1>), and ended manually by clearing the SAMP bit in user software. The acquisition time may be started automatically by the ADC hardware and ended automatically by a conversion trigger source. The acquisition time is set by the SAMC bits (AD1CON3<12:8>). The SHA has a minimum acquisition period; refer to the specific device data sheet for acquisition time specifications. Conversion time is the time required for the ADC to convert the voltage held by the SHA. The ADC requires one ADC clock cycle (TAD) to convert each bit of the result, plus two additional clock cycles. Therefore, a total of 12 TAD cycles are required to perform the complete conversion. When the conversion time is complete, the result is written into one of the 16 ADC result registers (ADC1BUF0 through ADC1BUFF). The sum of the acquisition time and the analog-to-digital conversion time provides the total sample time (refer to Figure 17-2). There are multiple input clock options for the ADC that are used to create the TAD clock. The user must select an input clock option that does not violate the minimum TAD specification. The sampling process can be performed once, periodically, or based on a trigger as defined by the module configuration. Figure 17-2: ADC Sample/Conversion Sequence ADC Total Sample Time Acquisition Time Conversion Time
Analog-to-digital conversion complete, result is written into the ADC result buffer. Optionally generate interrupt.
SHA is disconnected from input and holds the signal. Analog-to-digital conversion is started by the conversion trigger source. SHA is connected to the analog input pin for sampling.
DS61104E-page 17-12
17
10-bit Analog-to-Digital Converter (ADC)
DS61104E-page 17-13
Note:
14. To configure ADC interrupt (if required): a) Clear the AD1IF bit (IFS1<1>) (see 17.7). b) Select ADC interrupt priority AD1IP<2:0> (IPC<28:26>) and subpriority AD1IS<1:0> (IPC<24:24>) if interrupts are to be used (see 17.7). 15. Start the conversion sequence by initiating sampling (see 17.4.15).
DS61104E-page 17-14
The AD1PCFG register and the TRISB register control the operation of the ADC port pins. AD1PCFG specifies the configuration of device pins to be used as analog inputs. A pin is configured as an analog input when the corresponding PCFGn bit (AD1PCFG<n>) = 0. When the bit = 1, the pin is set to digital control. When configured for analog input, the associated port I/O digital input buffer is disabled so it does not consume current. The AD1PCFG register is cleared at Reset, causing the ADC input pins to be configured for analog input by default at Reset. TRIS registers control the digital function of the port pins. The port pins that are desired as analog inputs must have their corresponding TRIS bit set, specifying the pin as an input. If the I/O pin associated with an ADC input is configured as output, the TRIS bit is cleared, the ports digital output level (VOH or VOL) will be converted. After a device Reset, all of the TRIS bits are set. Note 1: When reading a PORT register that shares pins with the ADC, any pin configured as an analog input reads as a 0 when the PORT latch is read. Analog levels on any pin that is defined as a digital input (including the AN15:AN0 pins), but is not configured as an analog input, may cause the input buffer to consume current that is out of the devices specification. 2: The AD1PCFG register is not available in all the PIC32 devices. Refer to the specific device data sheet for availability.
17
10-bit Analog-to-Digital Converter (ADC)
17.4.2
The AD1CHS register is used to select which analog input pin is connected to MUX A and MUX B. Each multiplexer has two inputs referred to as the positive and the negative input. The positive input to MUX A is controlled by the CH0SA<3:0> bits (AD1CHS<19:16>) and the negative input is controlled by the CH0NA bit (AD1CHS<23>). The positive input for MUX B is controlled by the CH0SB<3:0> bits (AD1CHS<27:24>) and the negative input is controlled by the CH0NB bit (AD1CHS<31>). The positive input can be selected from any one of the available analog input pins. The negative input can be selected as the ADC negative reference or AN1. The use of AN1 as the negative input allows the ADC to be used in Unipolar Differential mode. Refer to the specific device data sheet for AN1 input voltage restrictions when used as a negative reference. Note: When using Scan mode, the CH0SA<3:0> bits may be overridden. Refer to 17.4.8 Selecting the Scan Mode for more information.
17.4.3
The data in the ADC result register can be read as one of eight formats. The format is controlled by the FORM<2:0> bits (AD1CON1<10:8>). The user can select from integer, signed integer, fractional, or signed fractional as a 16-bit or 32-bit result. Figure 17-3 and Figure 17-4 illustrate how a result is formatted. Table 17-2 and Table 17-3 provide examples of results for the select results in each of the four formats with 32-bit and 16-bit results. Note: There is no numeric difference between 32-bit and 16-bit modes. In 32-bit mode, the sign extension is applied to all 32-bits. In 16-bit mode, the sign extension is applied only to the lower 16-bits of the result.
DS61104E-page 17-15
Figure 17-3:
RAM Contents:
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Read to Bus:
Integer
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Signed Integer
d09 d09 d09 d09 d09 d09 d09 d09 d09 d09 d09 d09 d09 d09 d09 d09 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
Figure 17-4:
RAM Contents:
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Read to Bus:
Integer
d09 d08 d07 d06 d05 d04 d03 d02 d01 d00
Signed Integer
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 d09
17
Numerical Equivalents of Select Result Codes for FORM<2> (AD1CON1<10>) = 1, 32-bit Result
10-bit Output Code 32-bit Integer Format 32-bit Signed Integer Format 32-bit Fractional Format 32-bit Signed Fractional Format
1023/1024 11 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1100 0000 0111 1111 1100 0000 0000 0011 1111 1111 0000 0001 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 = 1023 = 511 = 0.999 = 0.499 1022/1024 11 1111 1110 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 1000 0000 0111 1111 1000 0000 0000 0011 1111 1110 0000 0001 1111 1110 0000 0000 0000 0000 0000 0000 0000 0000 = 1022 = 510 = 0.998 = 0.498
513/1024
10 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000 1000 0000 0100 0000 0 000 0000 0100 0000 0010 0000 0001 0000 0000 0000 0001 0000 0000 0000 0000 0000 = 513 = 1 = 0.501 0000 0000 0000 0000 = 0.001 10 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1000 0000 0000 0000 0000 0010 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 = 512 = 0 = 0.500 0000 0000 0000 0000 0000 0000 0000 0000 = 0.000
512/1024
511/1024
01 1111 1111 0000 0000 0000 0000 1111 1111 1111 1111 0111 1111 1100 0000 1111 1111 1100 0000 0000 0001 1111 1111 1111 1111 1111 1111 0000 0000 0000 0000 0000 0000 0000 0000 = 511 = -1 = .499 = -0.001
1/1024
00 0000 0001 0000 0000 0000 0000 1111 1111 1111 1111 0000 0000 0100 0000 1000 0000 0100 0000 0000 0000 0000 0001 1111 1110 0000 0001 0000 0000 0000 0000 0000 0000 0000 0000 = 1 = -511 = 0.001 = -0.499 00 0000 0000 0000 0000 0000 0000 1111 1111 1111 1111 0000 0000 0000 0000 1000 0000 0000 0000 0000 0000 0000 0000 1111 1110 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 = 0 = -512 = 0.000 = -0.500
0/1024
Table 17-3:
VIN/VR
Numerical Equivalents of Select Result Codes for FORM<2> (AD1CON1<10>) = 0, 16-bit Result
10-bit Output Code 16-bit Integer Format 16-bit Signed Integer Format
0000 0001 1111 1111 = 511
1022/1024 11 1111 1110 0000 0011 1111 1110 0000 0001 1111 1110 1111 1111 1000 0000 = 1022 = 510 = 0.998
513/1024
10 0000 0001 0000 0010 0000 0001 0000 0000 0000 0001 1000 0000 0100 0000 = 513 = 1 = 0.501 10 0000 0000 0000 0010 0000 0000 0000 0000 0000 0000 1000 0000 0000 0000 = 512 = 0 = 0.500
512/1024 511/1024
01 1111 1111 0000 0001 1111 1111 1111 1111 1111 1111 0111 1111 1100 0000 1111 1111 1100 0000 = 511 = -1 = .499 = -0.001
1/1024 0/1024
00 0000 0001 0000 0000 0000 0001 1111 1110 0000 0001 0000 0000 0100 0000 1000 0000 0100 0000 = 1 = -511 = 0.001 = -0.499 00 0000 0000 0000 0000 0000 0000 1111 1110 0000 0000 0000 0000 0000 0000 1000 0000 0000 0000 = 0 = -512 = 0.000 = -0.500
DS61104E-page 17-18
It is often desirable to synchronize the end of sampling and the start of conversion with some other time event. The ADC module may use one of four sources as a conversion trigger. The selection of the conversion trigger source is controlled by the SSRC<2:0> bits (AD1CON1<7:5>).
17.4.4.1
MANUAL CONVERSION
To configure the ADC to end sampling and start a conversion when the SAMP bit (AD1CON1<1>) is cleared (= 0), set the SSRC<2:0> bits to 000.
17.4.4.2
The ADC is configured for this trigger mode by setting the SSRC<2:0> = 010. When a period match occurs for the 32-bit timer, TMR3/TMR2, or the 16-bit Timer3, a special ADC trigger event signal is generated by Timer3. This feature does not exist for the TMR5/TMR4 timer pair or for 16-bit timers other than Timer3. Refer to Section 14. Timers (DS61105) for more details. 17.4.4.2.1 External INT0 Pin Trigger
17
10-bit Analog-to-Digital Converter (ADC)
To configure the ADC to begin a conversion on an active transition on the INT0 pin, the SSRC<2:0> bits are set to 001. The INT0 pin may be programmed for either a rising edge input or a falling edge input to trigger the conversion process. 17.4.4.2.2 Auto-Convert
The ADC can be configured to automatically perform conversions at the rate selected by the SAMC<4:0> bits (AD1CON3<12:8>). The ADC is configured for this Trigger mode by setting the SSRC<2:0> bits to 111. In this mode, the ADC will perform continuous conversions on the selected channels.
17.4.5
The modes where an external event trigger pulse ends sampling and starts conversion (SSRC<2:0> bits = 001, 010 or 011) may be used in combination with auto-sampling (ASAM bit (AD1CON1<2>) = 1) to cause the ADC to synchronize the sample conversion events to the trigger pulse source. For example, in Figure 17-13 where SSRC<2:0> = 010 and ASAM = 1, ADC will always end sampling and start conversions synchronously with the timer compare trigger event. The ADC will have a sample conversion rate that corresponds to the timer comparison event rate. See Example 17-5 for a code example.
17.4.6
17.4.6.1
Sampling can be started manually or automatically when the previous conversion is complete.
Clearing the ASAM bit (AD1CON1<2>) disables the Auto-Sample mode. Acquisition will begin when the SAMP bit (AD1CON1<1>) is set by software. Acquisition will not resume until the SAMP bit is once again set. Figure 17-8 illustrates an example.
17.4.6.2
AUTOMATIC SAMPLING
Setting the ASAM bit (AD1CON1<2>) enables Auto-Sample mode. In this mode, the sampling will start automatically after the pervious sample has been converted. Figure 17-9 illustrates an example.
DS61104E-page 17-19
The user can select the voltage reference for the ADC module. The reference can be internal or external. The VCFG<2:0> bits (AD1CON2<15:13>) select the voltage reference for analog-to-digital conversions. The upper voltage reference (VR+) and the lower voltage reference (VR-) may be the internal AVDD and AVSS voltage rails, or the VREF+ and VREF- input pins. The external ADC voltage reference may be used to reduce noise in the converter. The external voltage reference pins may be shared with the AN0 and AN1 inputs on low pin count devices. The ADC 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 Electrical Characteristics section in the specific device data sheet for more information. Note: External references, VREF+ and VREF-, must be selected for high conversion. Refer to the specific device data sheet for more information. The external VREF+ and VREF- pins may be shared with other analog peripherals. Refer to the specific device data sheet for more information.
17.4.8
The ADC module has the ability to scan through a selected vector of inputs. The CSCNA bit (AD1CON2<10>) enables the MUX A input to be scanned across a selected number of analog inputs.
17.4.8.1
Scan mode is enabled by setting the CSCNA bit (AD1CON2<10>). When Scan mode is enabled, the positive input of MUX A is controlled by the contents of the AD1CSSL register. Each bit in the AD1CSSL register corresponds to an analog input. Bit 0 corresponds to AN0, bit 1 corresponds to AN1, and so on. If a particular bit in the AD1CSSL register is 1, the corresponding input is part of the scan sequence. The input is always scanned from lower-numbered input to higher-numbered input, starting at the first selected channel after each interrupt occurs. When Scan mode is enabled, the CH0SA<3:0> bits (AD1CHS<19:16>) are ignored. Note: If the number of scanned input selected is greater than the number of samples taken per interrupt, the higher numbered inputs will not be sampled. The AD1CSSL register specifies only the input of the positive input of the channel. The CH0NA bit (AD1CHS<23>) selects the input of the negative input of the channel during scanning.
17.4.8.2
When the CSCNA bit = 0, Scan mode is disabled and the positive input to MUX A is controlled by the CH0SA<3:0> bits.
DS61104E-page 17-20
The Scan and Alternate modes may be combined to allow a vector of inputs to be scanned and a single input to be converted every other sample. This mode is enabled by setting the CSCNA bit (AD1CON2<10>) = 1, and setting the ALTS bit (AD1CON2<0>) = 1. The CSCNA bit enables the scan for MUX A, and the CH0SB<3:0> bits (AD1CHS<27:24>) and the CH0NB bit (AD1CHS<31>) are used to configure the inputs to MUX B. Scanning only applies to the MUX A input selection. The MUX B input selection, as specified by the CH0SB<3:0> bits, will still select a single input. The following sequence is an example of three scanned channels (MUX A) and a single fixed channel (MUX B): 1. 2. 3. 4. 5. 6. The first input in the scan list is sampled. The input selected by CH0SB<3:0> and CH0NB is sampled. The second input in the scan list is sampled. The input selected by CH0SB<3:0> and CH0NB is sampled. The third input in the scan list is sampled. The input selected by CH0SB<3:0> and CH0NB is sampled.
17
10-bit Analog-to-Digital Converter (ADC)
17.4.9
The SMPI<3:0> bits (AD1CON2<5:2>) select how many analog-to-digital conversions will take place before a CPU interrupt is generated. This also defines the number of locations that will be written in the result buffer starting with ADC1BUF0 (ADC1BUF0 or ADC1BUF8 for Dual Buffer mode). This can vary from one sample to 16 samples (one to eight samples for Dual Buffer mode). After the interrupt is generated, the sampling sequence restarts, with the result of the first sample being written to the first buffer location. For example, if the SMPI<3:0> bits = 0000, the conversion results will always be written to ADC1BUF0. In this example, no other buffer locations would be used. For example, if the SMPI<3:0> bits = 1110, 15 samples would be converted and stored in buffer locations, ADC1BUF0 through ADC1BUFE. An interrupt would be generated after ADC1BUFE is written. The next sample would be written to ADC1BUF0. In this example, ADC1BUFF would not be used. The data in the result registers will be overwritten by the next sampling sequence. The data in the result buffer must be read before the completion of the first sample after the interrupt is generated. The Buffer Fill mode can be used to increase the time between interrupt generation and the overwriting of data. Refer to 17.4.10 Buffer Fill Mode. The user cannot program a combination of samples and SMPI bits that results in more than 16 conversions per interrupt when the BUFM bit (AD1CON2<1>) is 1, or more than eight conversions per interrupt when the BUFM bit (AD1CON2<1>) is 0. Attempting to create a conversion list with the number of samples greater than 16 will result in the sampling sequence being truncated to 16 samples.
DS61104E-page 17-21
17.4.11 Selecting the MUX to be Connected to the ADC (Alternating Sample Mode)
The ADC has two input multiplexers that connect to the SHA. These multiplexers are used to select which analog input is to be sampled. Each of the multiplexers have a positive and a negative input (see Figure 17-5 and Figure 17-6). Note: The number of analog inputs will vary among different devices. Consult the specific device data sheet to verify the analog input availability.
DS61104E-page 17-22
17
10-bit Analog-to-Digital Converter (ADC)
When the PBCLK is used as the conversion clock source, the ADRC bit = 0, the TAD is the period of the PBCLK after the prescaler ADCS<7:0> bits (AD1CON3<7:0>) are applied. The ADC has a maximum rate at which conversions may be completed. An Analog module clock, TAD, controls the conversion timing. The analog-to-digital conversion requires 12 clock periods (12 TAD). The period of the ADC conversion clock is software selected using an 8-bit counter. There are 256 possible options for TAD, which are specified by the ADCS<7:0> bits (AD1CON3<7:0>). Equation 17-1 gives the TAD value as a function of the ADCS bits and the device instruction cycle clock period, TCY. Equation 17-1: ADC Conversion Clock Period T AD = 2 ( T PB ( ADCS + 1 ) ) T AD ADCS = ------------------ 1 2 T PB For correct analog-to-digital conversions, the ADC conversion clock (TAD) must be selected to ensure a minimum TAD time of 83.33 ns (see 17.10 Related Application Notes). Equation 17-2: Available Sampling Time, Sequential Sampling
T SMP = TriggerPulseInterval ( T SEQ ) ConversionTime ( T CONV ) T SMP = T SEQ T CONV Note: TSEQ is the trigger pulse interval time.
DS61104E-page 17-23
2.
Calculate the ideal TAD. The ADC requires one or more TAD (sample time) and 12 TAD (convert time) to perform a sample/conversion. a) Calculate the ADC sample plus convert time with a minimum sample time (1 TAD), as shown in Equation 17-4. The ADC minimum requirements for TAD is met, but not the sample time. ADC Sample Plus Convert Time with a Minimum Sample Time 1s -------------- = 76.9ns = T AD Desired ADC clock period 12 + 1
Equation 17-4:
b) c)
Increase the sample period to 2 TAD. Repeat the ADC clock calculation with a sample time equal to 2 TAD. This meets the ADC minimum requirements for TAD and sample time. ADC Clock Calculation 1s -------------- = 71.4ns = T AD 12 + 2
Equation 17-5:
3.
Calculate the ADC clock divisor value using the values from the previous steps. The closest available higher integer divisor value is 4 (ADCS = 1). The closest available lower integer divisor value is 2 (ADCS = 0). ADC Clock Divisor 71.4ns ----------------------- = 2.31 1 ----------------- 30MHz Desired ADC clock divisor
Equation 17-6:
DS61104E-page 17-24
Equation 17-7:
17
10-bit Analog-to-Digital Converter (ADC)
Equation 17-8:
Equation 17-9: 1 ----------------- = 33.3ns = TPB 30MHz 33.3ns 2 = 66.6ns = T AD T AD 3 = 66.6ns 3 = 200ns = sampletime Summary: ADCS = 2: ADC clock is PB divided by 2 SAMPC = 3: Sample time is 3 TAD periods
DS61104E-page 17-25
17.5
17.5.2
Aborting a Conversion
Clearing the ON bit (AD1CON1<15>) during a conversion will abort the current conversion. The ADC Result register will not be updated with the partially completed analog-to-digital conversion sample. That is, the corresponding result buffer location will continue to contain the value of the last completed conversion (or the last value written to the buffer).
17.5.3
When the conversion result buffer is split using the BUFM bit (AD1CON2<1>), the BUFS bit (AD1CON2<7>) indicates which half of the buffer the ADC is currently filling. If the BUFS bit = 0, the ADC is filling ADC1BUF0 to ADC1BUF7 and the user software should read conversion values from ADC1BUF8 to ADC1BUFF. If the BUFS bit = 1, the situation is reversed and the user software should read conversion values from ADC1BUF0 to ADC1BUF7.
DS61104E-page 17-26
The ADC module provides a method of measuring the internal offset error. After this offset error is measured, it can be subtracted, in software, from the result of a analog-to-digital conversion. Use the following steps to perform an offset measurement: 1. 2. 3. 4. 5. Configure the ADC in the same manner as it will be used in the application. Set the OFFCAL bit (AD1CON2<12>). This overrides the input selections and connects the sample-and-hold inputs to AVss. If auto-sample is used, set the CLRASAM bit (AD1CON1<4>) to stop conversions when the number of samples stated by SMPI is reached. Enable the ADC and perform a conversion. The value that is written to the ADC result buffer is the internal offset error. Clear the OFFCAL bit (AD2CON<12>) to return the ADC to normal operation. Note: Only positive ADC offsets can be measured with this method.
17
10-bit Analog-to-Digital Converter (ADC)
17.5.5
The CLRASAM bit provides a method to terminate auto-sample after the first sequence is completed. Setting the CLRASAM bit and starting an auto-sample sequence will cause the ADC to complete one auto-sample sequence (the number of samples as defined by the SMPI<3:0> bits (AD1CON2<5:2>)). Hardware will the clear the ASAM bit (AD1CON1<2>) and set the interrupt flag. This will stop the sampling process to allow inspection of the result buffer without results being overwritten by the next automatic conversion sequence. The CLRASAM bit must be cleared by software to disable this mode. Note: Disabling Interrupts or masking the ADC interrupt has no effect on the operation of the CLRASAM bit.
17.5.6
The DONE bit (AD1CON1<0>) is set when a conversion sequence is complete. In Manual mode, the DONE bit is persistent. It remains set until it is cleared by software. The DONE bit can be polled to determine when the conversion has completed. In all automatic sample modes (ASAM bit = 1), the DONE bit is not persistent. It is set at the end of a conversion sequence and cleared by hardware when the next acquisition is started. Polling the DONE bit is not recommended when operating the ADC in automatic modes. The AD1IF flag bit (IFS1<1>) is latched after a conversion sequence is completed and can therefore be polled. Figure 17-5 shows the ADC configuring for Alternate Sampling mode. Figure 17-6 shows the ADC configuration for Scan mode. Figure 17-7 shows the ADC configuration for a combination of Alternate Sampling mode and Scan mode.
DS61104E-page 17-27
32 VR+ VR-
Comparator VINH AN0 AN1 AN2 AN3 AN4 AN5 AN6 AN7 AN8 AN9 MUX B AN10 AN11 AN12 AN13 AN14 AN15 Pin Config Control Input MUX Control
CH0NB CH0SB<3:0> CH0SA<3:0> VR+ SHA
VINL
DAC
VINH
10-bit SAR
Conversion Logic
VRVINL
CHONA
MUX A
Data Formatting
VINH
VR-
VINL
Sample Control
Control Logic
Conversion Control
DS61104E-page 17-28
32 VR+ VR-
Comparator VINH AN0 AN1 AN2 AN3 AN4 AN5 AN6 AN7 AN8 AN9 AN10 AN11 AN12 AN13 AN14 AN15 Pin Config Control Input MUX Control Sample Control Control Logic VRVINL
CH0NA AD1CSSL VR+ SHA
VINL
DAC
17
10-bit Analog-to-Digital Converter (ADC)
Conversion Logic
VINH
10-bit SAR
MUX A
Data Formatting
Conversion Control
DS61104E-page 17-29
32 VR+ VR-
Comparator VINH AN0 AN1 AN2 AN3 AN4 AN5 AN6 AN7 AN8 AN9 MUX B AN10 AN11 AN12 AN13 AN14 AN15 Pin Config Control Input MUX Control
CH0NB CH0SB<3:0> AD1CSSL VR+ SHA
VINL
DAC
VINH
10-bit SAR
Conversion Logic
VRVINL
CH0NA
MUX A
Data Formatting
VINH
VR-
VINL
Sample Control
Control Logic
Conversion Control
DS61104E-page 17-30
The following configuration examples show the ADC operation in different sampling and buffering configurations. In each example, setting the ASAM bit (AD1CON2<1>) starts automatic sampling. A conversion trigger ends sampling and starts conversion.
17.5.8
When the SSRC<2:0> bits (AD1CON1<7:5>) = 000, the conversion trigger is under software control. Clearing the SAMP bit (AD1CON1<1>) starts the conversion sequence. Figure 17-8 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 acquisition time of the input signal. See Example 17-1 for a code example. Figure 17-8:
ADCLK TACQ TCONV
17
10-bit Analog-to-Digital Converter (ADC)
Example 17-1:
AD1PCFG = 0xFFFB; AD1CON1 = 0x0000; AD1CHS = 0x00020000; AD1CSSL = 0; AD1CON3 = 0x0002; AD1CON2 = 0;
AD1CON1SET = 0x8000; // while (1) // { AD1CON1SET = 0x0002; // DelayNmSec(100); // AD1CON1CLR = 0x0002; // while (!(AD1CON1 & 0x0001));// ADCValue = ADC1BUF0; // } //
turn on the ADC repeat continuously start sampling ... for 100 ms start Converting conversion done? yes then get ADC value repeat
DS61104E-page 17-31
Figure 17-9 is an example in which setting the ASAM bit (AD1CON1<2>) initiates automatic acquisition, and clearing the SAMP bit (AD1CON1<1>) terminates sampling and starts conversion. After the conversion completes, the module will automatically return to a acquisition state. The SAMP bit is automatically set at the start of the acquisition interval. The user software must time the clearing of the SAMP bit to ensure adequate acquisition time of the input signal, understanding that the time between clearing of the SAMP bit includes the conversion time as well as the acquisition time. See Example 17-2 for a code example. Figure 17-9:
ADCLK TAD0 SAMP DONE ADC1BUF0 set ASAM = 1 Set = 0 Set = 0 Instruction Execution TACQ TCONV TAD0 TACQ TCONV
Example 17-2:
AD1CON1SET = 0x8000; // while (1) // { DelayNmSec(100); // AD1CON1SET = 0x0002; // while (!(AD1CON1 & 0x0001));// ADCValue = ADC1BUF0; // } //
turn ON the ADC repeat continuously sample for 100 mS start Converting conversion done? yes then get ADC value repeat
DS61104E-page 17-32
17
10-bit Analog-to-Digital Converter (ADC)
ADCLK TSAMP = 31 TAD SAMP DONE ADC1BUF0 Instruction Execution set SAMP = 1 TCONV
Example 17-3:
Converting 1 Channel, Manual Sample Start, TAD Based Conversion Start Code
// all PORTB = Digital; RB12 = analog // SSRC bit = 111 implies internal // counter ends sampling and starts converting // Connect RB12/AN12 as CH0 input // in this example RB12/AN12 is the input // Sample time = 31 TAD
AD1CON1SET = 0x8000; while (1) { AD1CON1CLR = 0x0002; while (!(AD1CON1 & 0x0001)); ADCValue = ADC1BUF0; }
// turn ON the ADC // repeat continuously // // // // // start sampling then... after 31Tad go to conversion conversion done? yes then get ADC value repeat
DS61104E-page 17-33
ADCLK TSAMP = 15 TAD SAMP DONE ADC1BUF0 ADC1BUF1 Instruction Execution set ASAM = 1 TCONV TSAMP = 15 TAD TCONV
Example 17-4:
AD1CHS
= 0x00020000;
// Connect RB2/AN2 as CH0 input // in this example RB2/AN2 is the input // Sample time = 15 TAD // Interrupt after every 2 samples
AD1CON1SET = 0x8000; while (1) { ADCValue = 0; ADC16Ptr = &ADC1BUF0; IFS1CLR = 0x0002; AD1CON1SET = 0x0004;
// clear value // initialize ADC1BUF0 pointer // clear ADC interrupt flag // auto start sampling // for 31 TAD, and then go to conversion while (!IFS1 & 0x0002); // conversion done? AD1CON1CLR = 0x0004; // yes, stop sample/convert for (count = 0; count < 2; count++)// average the two ADC values { ADCValue = ADCValue + *(ADC16Ptr++); ADCValue = ADCValue >> 1; } // repeat
DS61104E-page 17-34
17
10-bit Analog-to-Digital Converter (ADC)
Figure 17-12: Converting 1 Channel, Manual Sample Start, Conversion Trigger Based Conversion Start
Figure 17-13: Converting 1 Channel, Auto-Sample Start, Conversion Trigger Based Conversion Start
Conversion Trigger ADCLK TSAMP TCONV TSAMP TCONV
DS61104E-page 17-35
// Sample time is TMR3, TAD = internal TPB * 2 // Interrupt after 2 conversions // set TMR3 to time out every 125 ms
TMR3= 0x0000; PR3= 0x3FFF; T3CON = 0x8010; AD1CON1SET = 0x8000; AD1CON1SET = 0x0004; while (1) { while (!IFS1 & 0x0002){}; ADCValue = ADC1BUF0; IFS1CLR = 0x0002; } // turn ON the ADC // start auto sampling every 125 mSecs // repeat continuously // // // // conversion done? yes then get first ADC value clear ADIF repeat
DS61104E-page 17-36
17
10-bit Analog-to-Digital Converter (ADC)
TSAMP TCONV TCONV
Conversion Trigger
ADCLK
TSAMP TCONV
TSAMP TCONV
TSAMP
Input to MUX A ASAM SAMP DONE ADC1BUF0 ADC1BUF1 ADC1BUFE ADC1BUFF ADIF
AN0
AN0
AN0
AN0
set ASAM = 1
Instruction Execution
DS61104E-page 17-37
CONTROL BITS Sequence Select SMPI<2:0> = 1111 Interrupt on 15th sample BUFM = 0 Single 16-word result buffer ALTS = 0 Always use MUX A input select MUX A Input Select CH0SA<3:0> = 0000 Select AN0 for CH0+ input CH0NA = 0 Select VR- for CH0- input CSCNA = 0 No input scan CSSL<15:0> = n/a Scan input select unused MUX B Input Select CH0SB<3:0> = n/a Mux B positive input unused CH0NB = n/a Mux B negative input unused
Interrupt Repeat Buffer Address ADC1BUF0 ADC1BUF1 ADC1BUF2 ADC1BUF3 ADC1BUF4 ADC1BUF5 ADC1BUF6 ADC1BUF7 ADC1BUF8 ADC1BUF9 ADC1BUFA ADC1BUFB ADC1BUFC ADC1BUFD ADC1BUFE ADC1BUFF Buffer @ 1st Interrupt 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 Buffer @ 2nd Interrupt AN0 sample 16 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
DS61104E-page 17-38
17
10-bit Analog-to-Digital Converter (ADC)
TSAMP TCONV
TSAMP TCONV
TSAMP TCONV
TSAMP TCONV
Input MUX A ASAM SAMP DONE ADC1BUF0 ADC1BUF1 ADC1BUFE ADC1BUFF ADIF set ASAM = 1
AN0
AN1
AN14
AN15
Instruction Execution
DS61104E-page 17-39
CONTROL BITS Sequence Select SMPI<2:0> = 1111 Interrupt on 16th sample BUFM = 0 Single 16-word result buffer ALTS = 0 Always use MUX A input select MUX A Input Select CH0SA<3:0> = n/a Overridden by CSCNA CH0NA = 0 Select VR- for MUX A negative input CSCNA = 1 Scan inputs CSSL<15:0> = 1111 1111 1111 1111 Scan input select MUX B Input Select SB<3:0> = n/a MUX B positive input unused CH0NB = n/a MUX B negative input unused
Buffer Address ADC1BUF0 ADC1BUF1 ADC1BUF2 ADC1BUF3 ADC1BUF4 ADC1BUF5 ADC1BUF6 ADC1BUF7 ADC1BUF8 ADC1BUF9 ADC1BUFA ADC1BUFB ADC1BUFC ADC1BUFD ADC1BUFE ADC1BUFF
Buffer @ 1st Interrupt AN0 sample 1 AN1 sample 2 AN2 sample 3 AN3 sample 4 AN4 sample 5 AN5 sample 6 AN6 sample 7 AN7 sample 8 AN8 sample 9 AN9 sample 10 AN10 sample 11 AN11 sample 12 AN12 sample 13 AN13 sample 14 AN14 sample 15 AN15 sample 16
DS61104E-page 17-40
17
Conversion Trigger ADCLK TCONV Input to MUX A AN0 AN1 TCONV AN2 TCONV
TSAMP
TSAMP
TSAMP
DS61104E-page 17-41
CONTROL BITS Sequence Select SMPI<2:0> = 0010 Interrupt after every third sample BUFM = 1 Dual 8-word result buffers ALTS = 0 Always use MUX A MUX A Input Select CH0SA<3:0> = n/a MUX A positive input select is not used CH0NA = 0 Select VR- for MUX A negative input CSCNA = 1 Enable input scan CSSL<15:0> = 0x0007 Scan input select scan list consisting of AN0, AN1, and AN2 AD1PCFG = 0X0007 Select Analog Input mode for AN0, AN1, and AN2 MUX B Input Select CH0SB<3:0> = n/a MUX B positive input unused CH0NB = n/a MUX B negative input unused
Buffer Address ADC1BUF0 ADC1BUF1 ADC1BUF2 ADC1BUF3 ADC1BUF4 ADC1BUF5 ADC1BUF6 ADC1BUF7 ADC1BUF8 ADC1BUF9 ADC1BUFA ADC1BUFB ADC1BUFC ADC1BUFD ADC1BUFE ADC1BUFF
DS61104E-page 17-42
17
10-bit Analog-to-Digital Converter (ADC)
Conversion Trigger
ADCLK
TSAMP
TCONV
TSAMP
TSAMP TCONV
Input to MUX A Input to MUX B ASAM SAMP DONE ADC1BUF0 ADC1BUF1 ADC1BUFE ADC1BUFF ADIF
AN0 AN1
AN0 AN1
set ASAM = 1
Instruction Execution
DS61104E-page 17-43
CONTROL BITS Sequence Select SMPI<2:0> = 0011 Interrupt on 4th sample BUFM = 1 Dual 8-word result buffers ALTS = 1 Alternate MUX A/B input select MUX A Input Select CH0SA<3:0> = 0000 Select AN0 for MUX A positive input CH0NA = 0 Select VR- for MUX A negative input CSCNA = 0 No input scan CSSL<15:0> = n/a Scan input select unused MUX B Input Select CH0SB<3:0> = 0001 Select AN1 for MUX B positive input CH0NB = 0 Select VR- for MUX B negative input
Buffer Address ADC1BUF0 ADC1BUF1 ADC1BUF2 ADC1BUF3 ADC1BUF4 ADC1BUF5 ADC1BUF6 ADC1BUF7 ADC1BUF8 ADC1BUF9 ADC1BUFA ADC1BUFB ADC1BUFC ADC1BUFD ADC1BUFE ADC1BUFF
Buffer @ 1st Interrupt AN0 sample 1 AN1 sample 1 AN0 sample 2 AN1 sample 2
DS61104E-page 17-44
17
10-bit Analog-to-Digital Converter (ADC)
Conversion Trigger
ADCLK
TSAMP TCONV
TSAMP TCONV
TSAMP TCONV
TSAMP TCONV
Input to MUX A Input to Max B ASAM SAMP DONE ADC1BUF0 ADC1BUF1 ADC1BUFE ADC1BUFF ADIF
AN0 AN2
AN1 AN2
set ASAM = 1
Instruction Execution
DS61104E-page 17-45
VR Select
VR+
32
VRVINH
AD1CSSL VRVR+
VINH
10-bit SAR
Conversion Logic
MUX A VINL
CHONA
Data Formatting
VINH
VINL
Sample Control
Control Logic
Conversion Control
DS61104E-page 17-46
CONTROL BITS Sequence Select SMPI<2:0> = 0011 Interrupt on 4th sample BUFM = 0 Single 16-word result buffer ALTS = 1 Alternate MUX A/B input select MUX A Input Select CH0SA<3:0> = n/a Not used CH0NA = 0 Select VR- for CH0- input CSCNA = 1 Enable input scan CSSL<15:0> = n/a Scan input select scan list consisting of AN0 and AN1 MUX B Input Select CH0SB<3:0> = 0010 Select AN7 for CH0+ input CH0NB = 0 Select VR- for CH0- input
17
10-bit Analog-to-Digital Converter (ADC)
Buffer Address ADC1BUF0 ADC1BUF1 ADC1BUF2 ADC1BUF3 ADC1BUF4 ADC1BUF5 ADC1BUF6 ADC1BUF7 ADC1BUF8 ADC1BUF9 ADC1BUFA ADC1BUFB ADC1BUFC ADC1BUFD ADC1BUFE ADC1BUFF
Buffer @ 1st Interrupt AN0 sample 1 AN2 sample 2 AN1 sample 3 AN2 sample 4
Buffer @ 2nd Interrupt AN0 sample 5 AN2 sample 6 AN1 sample 7 AN2 sample 8
DS61104E-page 17-47
10 0000 0011 (= 515) 10 0000 0010 (= 514) 10 0000 0001 (= 513) 10 0000 0000 (= 512) 01 1111 1111 (= 511) 01 1111 1110 (= 510) 01 1111 1101 (= 509)
00 0000 0001 (= 1) 00 0000 0000 (= 0) VRVR- + VR+ VR1024 VR- + 512*(VR+ VR-) 1024 VR- + 1023*(VR+ VR-) 1024 (VINH VINL) VR+
DS61104E-page 17-48
VDD
10 F
17
10-bit Analog-to-Digital Converter (ADC)
75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51
VDD
VDD 10K
VDD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76
VDD
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 R1 10 AVSS AVDD VDD AVSS C8 1 F C7 0.1 F C6 0.01 F VDD VDD VDD VDD VDD C5 1 F
VDD C4 0.1 F
VDD C3 0.01 F
DS61104E-page 17-49
VT = 0.6V
Note: The CPIN value depends on the device package and is not tested. The effect of the CPIN is negligible if Rs 5 k. Legend: CPIN = input capacitance RSS = sampling switch resistance RS = source resistance ILEAKAGE = leakage current at the pin due to various junctions VT = threshold voltage RIC = interconnect resistance CHOLD = sample/hold capacitance
DS61104E-page 17-50
17
10-bit Analog-to-Digital Converter (ADC)
AD1PCFG = 0x0000;
AD1CON1 = 0x2208;
AD1CON2 = 0x0000;
AD1CSSL = 0x0000;
IFS1CLR = 2;
// Configure ADC interrupt priority bits (AD1IP<2:0>) here, if // required. (default priority level is 4) IEC1SET = 2; AD1CON1SET = 0x8000; AD1CON1SET = 0x0002; DelayNmSec(100); /* Enable ADC conversion interrupt*/ /* Turn on the ADC module */ /* Start sampling the input */ /* Ensure the correct sampling time has elapsed before starting a conversion.*/ /* End Sampling /* The DONE bit is finished. /* The ADIF bit and start Conversion*/ is set by hardware when the convert sequence */ will be set. */
AD1CON1CLR = 0x0002; : :
DS61104E-page 17-51
AD1CHS
= 0x00020000;
AD1CSSL = 0; AD1CON3 = 0x0203; AD1CON2 = 0x6004; AD1CON1bits.ADON = 1; while (1) { ADCValue = 0; ADC16Ptr = &ADC1BUF0; IF1bits.AD1IF = 0; AD1CON1bits.ASAM = 1;
while (!IFS0bits.ADIF); AD1CON1bits.ASAM = 0; for (count = 0; count <2; count++) { ADCValue = ADCValue + *ADC16Ptr++; ADCValue = ADCValue >> 1; } }
//
repeat
DS61104E-page 17-52
17
10-bit Analog-to-Digital Converter (ADC)
Note:
Some PIC32 devices feature persistent interrupts. On such devices, clearing the AD1IF flag bit will not have any effect unless ADC1BUFx register is read. Refer to the specific device data sheet and Section 8. Interrupts (DS61108) for more information.
DS61104E-page 17-53
17.8.1
When the device enters Sleep mode, all clock sources to the module are shut down and stay at logic 0. If Sleep occurs in the middle of a conversion, the conversion is aborted unless the ADC module is clocked from its internal RC clock generator. The converter will not resume a partially completed conversion on exiting from Sleep mode. ADC register contents are not affected by the device entering or leaving Sleep mode.
17.8.2
The ADC module can operate during Sleep mode if the ADC clock source is set to the internal RC oscillator (ADRC bit (AD1CON3<15>) = 1). This reduces the digital switching noise from the conversion. When the conversion is completed, the DONE bit (AD1CON1<0>) will be set and the result loaded into the ADC result buffer, ADC1BUFx. If the ADC interrupt is enabled (AD1IE bit (IEC<1>) = 1), the device will wake up from Sleep when the ADC interrupt occurs. Program execution will resume at the ADC Interrupt Service Routine (ISR), if the ADC interrupt is greater than the current CPU priority. Otherwise, execution will continue from the instruction after the WAIT instruction that placed the device in Sleep mode. If the ADC interrupt is not enabled, the ADC module will then be disabled, although the ON bit (AD1CON1<15>) will remain set. To minimize the effects of digital noise on the ADC module operation, the user should select a conversion trigger source that ensures the analog-to-digital conversion will take place in Sleep mode. The automatic conversion trigger option can be used for sampling and conversion in Sleep (SSRC<2:0> bits (AD1CON1<7:5>) = 111). To use the automatic conversion option, the ADC ON bit should be set in the instruction prior to the WAIT instruction. Note: For the ADC module to operate in Sleep mode, the ADC clock source must be set to the internal RC oscillator (ADRC = 1).
17.8.3
For the ADC, the SIDL bit (AD1CON1<13>) specifies whether the module will stop on Idle or continue on Idle. If the SIDL bit = 0, the ADC module will continue normal operation when the device enters Idle mode. If the ADC interrupt is enabled (AD1IE bit = 1), the device will wake up from Idle mode when the ADC interrupt occurs. Program execution will resume at the ADC ISR, if the ADC interrupt is greater than the current CPU priority. Otherwise, execution will continue from the instruction after the WAIT instruction that placed the device in Idle mode. If the SIDL bit = 1, the ADC module will stop in Idle mode. If the device enters Idle mode in the middle of a conversion, the conversion is aborted. The converter will not resume a partially completed conversion on exiting from Idle mode.
DS61104E-page 17-54
17.9.2
Power-on Reset
Following a Power-on Reset (POR) event, all of the ADC control registers (AD1CON1, AD1CON2, AD1CON3, AD1CHS, AD1PCFG and AD1CSSL) are reset to a value of 0x00000000. This disables the ADC module and sets the analog input pins to Analog Input mode. The values in the ADC1BUFx registers are initialized during a POR. ADC1BUF0 through ADC1BUFF will contain 0x00000000.
17
10-bit Analog-to-Digital Converter (ADC)
17.9.3
Following a Watchdog Timer (WDT) reset, all of the ADC control registers (AD1CON1, AD1CON2, AD1CON3, AD1CHS, AD1PCFG and AD1CSSL) are reset to a value of 0x00000000. This disables the ADC module and sets the analog input pins to Analog Input mode. Any conversion that was in progress will terminate and the result will not be written to the result buffer. The values in the ADC1BUFx registers are initialized after a WDT reset. ADC1BUF0 through ADC1BUFF will contain 0x00000000.
DS61104E-page 17-55
Please visit the Microchip web site (www.microchip.com) for additional Application Notes and code examples for the PIC32 family of devices.
DS61104E-page 17-56
17
10-bit Analog-to-Digital Converter (ADC)
DS61104E-page 17-57
DS61104E-page 17-58
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.
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 buyers 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.
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. 2007-2011, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved. Printed on recycled paper. ISBN: 978-1-61341-575-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.
DS61104E-page 17- 59
ASIA/PACIFIC
Asia Pacific Office Suites 3707-14, 37th Floor Tower 6, The Gateway Harbour City, Kowloon Hong Kong Tel: 852-2401-1200 Fax: 852-2401-3431 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 China - Hangzhou Tel: 86-571-2819-3187 Fax: 86-571-2819-3189 China - Hong Kong SAR Tel: 852-2401-1200 Fax: 852-2401-3431 China - Nanjing Tel: 86-25-8473-2460 Fax: 86-25-8473-2470 China - Qingdao Tel: 86-532-8502-7355 Fax: 86-532-8502-7205 China - Shanghai Tel: 86-21-5407-5533 Fax: 86-21-5407-5066 China - Shenyang Tel: 86-24-2334-2829 Fax: 86-24-2334-2393 China - Shenzhen Tel: 86-755-8203-2660 Fax: 86-755-8203-1760 China - Wuhan Tel: 86-27-5980-5300 Fax: 86-27-5980-5118 China - Xian Tel: 86-29-8833-7252 Fax: 86-29-8833-7256 China - Xiamen Tel: 86-592-2388138 Fax: 86-592-2388130 China - Zhuhai Tel: 86-756-3210040 Fax: 86-756-3210049
ASIA/PACIFIC
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 India - Pune Tel: 91-20-2566-1512 Fax: 91-20-2566-1513 Japan - Yokohama Tel: 81-45-471- 6166 Fax: 81-45-471-6122 Korea - Daegu Tel: 82-53-744-4301 Fax: 82-53-744-4302 Korea - Seoul Tel: 82-2-554-7200 Fax: 82-2-558-5932 or 82-2-558-5934 Malaysia - Kuala Lumpur Tel: 60-3-6201-9857 Fax: 60-3-6201-9859 Malaysia - Penang Tel: 60-4-227-8870 Fax: 60-4-227-4068 Philippines - Manila Tel: 63-2-634-9065 Fax: 63-2-634-9069 Singapore Tel: 65-6334-8870 Fax: 65-6334-8850 Taiwan - Hsin Chu Tel: 886-3-5778-366 Fax: 886-3-5770-955 Taiwan - Kaohsiung Tel: 886-7-536-4818 Fax: 886-7-330-9305 Taiwan - Taipei Tel: 886-2-2500-6610 Fax: 886-2-2508-0102 Thailand - Bangkok Tel: 66-2-694-1351 Fax: 66-2-694-1350
EUROPE
Austria - Wels Tel: 43-7242-2244-39 Fax: 43-7242-2244-393 Denmark - Copenhagen Tel: 45-4450-2828 Fax: 45-4485-2829 France - Paris Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79 Germany - Munich Tel: 49-89-627-144-0 Fax: 49-89-627-144-44 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
08/02/11
DS61104E-page 17- 60