Analog to Digital Converter (ADC)

The Analog-to-Digital Converter (ADC) can convert an analog input signal to a 10-bit binary digital representation of that signal. The Microchip microcontrollers analog inputs, which are multiplexed into a single sample and hold circuit. The output of the sample and hold is connected to the input of the ADC. The ADC generates the 10-bit binary result via successive approximation and stores the conversion result into the ADC result registers.

adcblock.png

The ADC uses a voltage reference that is software-selectable to be either internally generated or externally supplied.

The ADC can also generate an interrupt upon completion of a conversion. This interrupt can be used to wake-up the device from SLEEP.

ADC Configuration

When the ADC is first set up it needs to have several configuration settings enabled. These include:

  • ADC port configuration
  • ADC channel selection
  • ADC voltage reference selection
  • ADC conversion clock source
  • ADC interrupt control
  • ADC result format

We'll step through each of these below.


ADC Port Configuration

The first configuration setting is the I/O pin setup. Most of the ADC I/O pins can be used as either an analog input or a digital input. When converting analog signals using the ADC, the I/O pin must be configured for analog input by setting the associated bits in the TRIS register and ANSEL register.

The TRIS register for the I/O pin needs to have its associated bit set to 1 to make it an input. If the I/O pin is part of the PORTA block then the TRISA register contains the bit.

trisa.png

The next step is to set the bit on the ANSEL register for the I/O pin and set the bit to 1 to enable the ADC on that pin. If the I/O pin is part of the PORTA block then the ANSELA register contains the bit.

ansela.png

ADC Channel Selection

The ADC multiplexer needs to be connected to the I/O pin prior to starting the sample and hold process. This is done with a set of bits in the ADCON0 register. Before an ADC sample is requested, these channel select bits are set to connect to the desired I/O pin. Only one pin can be connected to the ADC at a time. After the process is complete then the selection bits can be changed to connect to the next pin and the ADC process starts over again.

adcon0.png
adcselect.png

Some devices may have fewer channels


ADC Voltage Reference Selection

The ADC can use various voltage reference sources as the basis for analog voltage measurements.

Digital Value = [Analog Voltage / (VREF+ - VREF-) ] * 1024

The ADPREF bits of the ADCON1 register provide control of the positive voltage reference. The positive voltage reference can be:

  • VREF+
  • VDD
  • Fixed Voltage Reference (FVR)

The ADNREF bits of the ADCON1 register provide control of the negative voltage reference. The negative voltage reference can be:

  • VREF-
  • VSS

VDD and VSS are the connections to the voltage bus that powers the device.

VREF+ and VREF- are specific I/O pins on the device. An external voltage reference is connected to these pins.

FVR is a feature on many PIC® devices, though not all. It can include a single voltage or sometimes more than one voltage level is available.

The voltage reference selection bits are in the ADCON1 register and the selection options are shown below.

adcon1.png
vref.png

ADC Conversion Clock Source

The source of the conversion clock is software selectable via the ADCS bits of the ADCON1 register. There are up to seven possible clock options depending on the device being used:

  • FOSC/2
  • FOSC/4
  • FOSC/8
  • FOSC/16
  • FOSC/32
  • FOSC/64
  • FRC (dedicated internal oscillator)

FOSC is the system oscillator that is running the device instruction clock.

The clock is critical to producing the fastest but also accurate Analog to Digital Conversion.
The time to complete one bit conversion is defined as TAD. One full 10-bit conversion requires 11.5 TAD periods as shown here:

tad.png

For correct conversion, the appropriate TAD specification must be met. An ADC clock can be easily selected from the chart below. A similar chart appears in the device data sheet. The values that are the best show up in the middle of the chart in a white background.

tadchart.png

The internal oscillator FRC selection will be a slower conversion but will guarantee the TAD requirements are met. The FRC can also be used in sleep mode to run ADC measurements.


Interrupt Control

The ADC module has the ability to generate an interrupt upon completion of an Analog-to-Digital conversion. This interrupt can also be generated while the device is operating or while in SLEEP. If the device is in SLEEP, the interrupt will wake-up the device and then process the Interrupt Service Routine (ISR) as long as the interrupt bits are enabled.

Those interrupt bits include:

  • The ADC Interrupt Flag is the ADIF bit in the Peripheral Interrupt (PIR1) register 1.
  • The ADC Interrupt Enable is the ADIE bit in the Peripheral Interrupt Enable (PIE1) register.
  • The Global Interrupt Enable (GIE) bit and the Peripheral Interrupt Enable (PEIE) bits in the INTCON register must also be enabled.

After an interrupt from SLEEP mode is executed and the ISR and ADC are completed, the ADIF bit must be cleared in software.


ADC Result Format

The ADC conversion result is stored in two 8-bit wide registers; ADRESH and ADRESL. This register pair is 16-bits wide so the ADC module has the flexibility to left or right justify the 10-bit result in the 16-bit result register. The ADC Format Select bit (ADFM) in the ADCON1 register controls this justification. The extra bits in the ADRESH and ADRESL registers are loaded with ‘0’s.

justified.png
adfmbit.png

The result can then be copied to a variable or used in an equation to implement a function based on the ADC result.


MPLAB® Code Configurator - ADC Setup

MPLAB® Code Configurator (MCC) makes setting up the ADC code quite easy. All the configuration settings described above can be set up in a simple GUI screen within MPLAB X IDE. The ADC screen is shown here:

mc2.png

Each configuration option is selected as a checkbox or from a dropdown menu. After the selections are made, then the code is generated and placed in the project. Two files are created named adc.h and adc.c. They contain ADC setup code and also custom functions to use the ADC within your main project files.

mc2code.png

ADC Project Example

Here is a step by step example project for setting up the ADC using MCC.

proj3.png
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.