Analog to digital converters are electronic devices widely used in today’s digital world as most of the real-time signals are analog ones while the day-to-day devices of the contemporary world are mostly digital devices which can process only digital signals. This chapter describes the functioning of an analog to digital converter using PIC18F4550.

## Analog to Digital Converters Analog signals are continuous time-varying signals. Most of the real world signals around us are analog in nature. Analog to digital conversion becomes inevitable when these real-world signals need to be processed in a digital environment.
ADC is the primary means by which an analog signal is converted into digital data that can be processed easily using a digital computer. There exist many types of ADCs for different purposes like counter type, flash type, successive approximation type, etc. Selection depends upon the requirement. The most inexpensive ADC available in the electronic market is the successive approximation type.

## Successive Approximation ADC

The successive approximation ADC generates a series of digital codes each corresponding to a fixed analog level with an internal counter to compare with the analog signal under conversion. The generation is stopped when the analog level becomes larger than the analog signal. The digital code corresponding to the analog level is the desired digital representation of the analog signal. The performance of the ADC is measured using the resolution and speed as parameters. Resolution is nothing but the number of levels or intervals which can be divided from a certain analog input range which is expressed in bits. The resolution of an n-bit ADC is 1/(2^n). For example, an ADC with 10-bit resolution as one in PIC 18F4550 can have 2^10 (1024) intervals for the range of analog input signal.

## ADC in PIC Microcontroller (PIC18F4550)

The ADC in PIC18F4550 is a successive approximation ADC with a resolution of 10 bits. The resolution indicates how much the reference voltage can be divided. For a 10 bit resolution ADC, it is possible to divide up to 1024 (2^10) voltages. So for a 5V reference voltage, the minimum voltage will be 5/1024 = 4.8mV. This means 4.8mV in the analog pin will be detected as 1 and 9.6mV will be 2 (10 in binary). It can’t detect the difference between 1mV and 3mV since both will be detected as 0. This is where the resolution comes into the picture. The more the resolution is, ADC will be better at detecting the small voltage changes.

PIC18F4550 has 13 channels which mean 13 analog input signals can be converted simultaneously using the module. 8 choosable clock inputs are available for conversion and the module can be configured in auto triggering mode.

Other terminology usually appears while using ADC is the reference voltage. It specifies the minimum and maximum voltage range of analog input. PIC18F4550 has two reference voltage inputs, Vref- and Vref+. Vref- is the minimum input voltage of analog input and Vref+ is the maximum voltage. That means if Vref- is applied to the pin for conversion, the digital value after conversion will be 0 and for the Vref+, this will be the maximum value (1023 for 10-bit resolution). The PIC18F4550 pins for Vref+ are RA3 and Vref- is RA2. In normal cases, we set Vref- as 0 and Vref+ as 5V. The digital value is calculated based on the following equation.

Another term useful while using ADC is acquisition time. It is the time required to fully charge the charge holding capacitor to the input channel voltage level. In layman’s terms, it is the time required to wait before starting conversion and after applying input in the analog port pin. It depends on the holding capacitor value, an input impedance of the analog source, Vdd, conversion error, temperature etc. Based on the commonly used values, it is calculated as 2.45 us. What this means that we should start the conversion only after applying input analog and passing 2.45 us.

A/D conversion clock is the analog to digital conversion time per bit and denoted as Tad. A/D conversion requires 11 Tad per 10-bit conversion. There are seven possible options for Tad.

• 2 Tosc
• 4 Tosc
• 8 Tosc
• 16 Tosc
• 32 Tosc
• 64 Tosc
• Internal RC Oscillator

Tad must be greater than minimum Tad specified and must be as short as possible. From PIC18F4550 datasheet, it is 0.7 µs (page 400, table 28-29). So for a 20MHz PIC18F4550,

## ADC Register Descriptions

The module is configured by writing to certain special function registers. There are three control registers for the ADC module and two result registers.

### ADCON0(A/D Control Register 0) This register uses to select input channels and control conversion process

• ADCON0 needs to be configured for enabling the ADC peripheral (ADON bit)
• GO bit in the register starts the conversion of the input analog signal when set. The bit is also used as A/D conversion status bit.
• CH3: CH0 bits are the channel select bits from 13(0-12) variable channels which are multiplexed with digital I/O pins. The bit configurations to select particular conversion channels can be sourced from the device datasheet.

### ADCON1 (A/D control register 1) ADCON2 uses to select voltage reference and selection of port pins for analog inputs.

• PCFG0: PCFG3 bits in ADCON1 determines which pins of the controller should be configured as analog inputs. We have seen that the ADC module has 13 channels which are multiplexed with the I/O pins. The configuration of pins as digital or analog can be achieved through configuring these bits in ADCON1. Corresponding configuration values can be referred from the datasheet.
• VCGF1: VCGF0 is  the voltage configuration bits to select the reference voltage for conversion.

### ADCON2 (A/D Control Register 2) ADCON2, on the other hand, uses to select ADC data format, clock options and acquisition time

• ADCS2: ADCS0 configures the clock option for ADC peripheral. The system clock is divided to configure clock for AD conversion configurations for different clocks can be obtained from the datasheet.
• ACQT2: ACQT0 These bits are used to set the acquisition time of the ADC. It is the time required to charge and discharge the holding capacitor of ADC.
• ADFM this bit is used to select the format by which result should be stored in the ADRESH and ADRESL

### ADRESH & ADRESL

The result of conversion which is a 10-bit digital value is stored in the ADRESH and ADRESL registers in the prescribed format specified by the configuration of DFM bit.

## Steps to Perform A/D Conversion

From the above calculations and descriptions, we are going to implement an analog to digital conversion in pin AN0 for a maximum voltage of 5V and minimum voltage 0V. Below are the steps required to do the conversion.

### Configure the A/D module

• Configure analog pins, voltage reference and digital I/O

• Select A/D acquisition time and conversion clock, output result format

• Turn ON the ADC by setting ADON bit

### Start Conversion

• Start conversion by setting GO/DONE bit

• Wait for A/D conversion to complete. As long as the GODONE bit HIGH, the conversion is not complete.

• Read the result from ADRES register

• Repeat the start conversion section if it needs further conversion

## Analog to Digital Converter Using PIC18F4550 – Circuit

A varying signal of 0-5V is applied in the AN0 pin of PIC18F4550 and the analog value is converted to the corresponding digital value. ADC interfacing – PIC18F4550

Here the analog voltage is generated and varied using a potentiometer.

## Firmware Sample

This code is based on the above-described settings. For delay, we have used default delay routine of XC8 compiler. For more details, refer How can I create a delay in my XC8 program

The result reads into a variable which should be 16-bit size.

DON’T MISS OUT UPDATES!
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Subscribe, it is quick.
We hate spam too. Give it a try, you can unsubscribe anytime.
Spread the love, share this