ADC | Analog to Digital Converter

Physical sensors measuring natural phenomenon are all analog in nature. They are continuously variable and can take an infinite range of different values, whether we are talking about temperature, sound level, frequency or other variables. It is necessary, therefore, for the microcontroller to be able to read values that are analog and if necessary generate output values that are analog, even though internally the microcontroller is a digital device. Thus the requirement of ADC – Analog to Digital Converter arises. It also includes discussion about comparators.

Once data from the outside world has been acquired, it needs to be processed and put to use. It may also need to be averaged, scaled, linearised or stored. Quite possibly it will be used for some form of control purpose and it may need to be displayed, or transmitted to another device.

In PIC microcontroller, this is done using an integrated circuit to convert external analog input to a digital output usable by the microcontroller called Analog to Digital Converter (ADC). There are several different designs of ADCs, which are based on three basically different approaches.

Flash converter

Flash converter or parallel encoder uses circuits called comparators (figure below). These comparators have two inputs, the analog input voltage that is to be converted and a reference voltage. If the analog input exeeds the reference voltage then the output logic is 1 otherwise the output logic is 0. They have a very high accuracy and the chance of it accepting the two voltages as the same level are extremely slight and doesn’t happen in practice. Multiple comparators are used in the system with different reference voltages. When an input is passed, the comparators give out 0 or 1 depending on the reference level which gives us a binary equivalent of that analog voltage. The more the comparators we have, the more logic levels we can get which in turn increases the accuracy of the reading.


Ramp generator

These are a combination of a binary counter that counts up from zero to its maximum value. As the binary count proceeds, a ramp voltage is made to steadily increase. A single comparator is used to compare the output of the ramp voltage with the analog voltage being converted. As soon as the ramp voltage exceeds the input voltage, the comparator signal stops the counter. The counter output is then the digital equivalent of the analog signal.

Ramp Generator Circuit and Graph
Ramp Generator Circuit and Graph

(An ADC that uses ramp voltage)

successive approximation

If we were to use a 3-bit digital signal to convert an analog voltage of between 0 V and 4 V we could have the 3 bits representing voltages of 4 V, 2 V and 1 V. This is how the circuit responds to an input of 3.5 V. The digits are initially set to 000. The left-hand bit is switched on and its 4 V is compared with the input. The input is seen to be less than this so this digit is reset to zero. It then tries the next bit and its 2 V are compared and found to be less than the input so it remains set. The digital signal is now 010. The circuit now adds the 1 V from the last digit. The result is a total of 3 V, which is compared with the input analog signal. The input of 3.5 V still exceeds the current value of 3 V so the last bit is set. The final digital output is 011. The circuit has tried all the available values until it finds the one that provides the result closest, but less than the input signal. As before, the more bits we are using, the more accurate is the result.

Controlling the ADC

In PIC 16F877A Microcontroller, the ADC ( Analog to Digital Converter ) is controlled by two Special Function Registers, ADCON0 and ADCON1. The result of the conversion is placed in two further SFRs, ADRESH and ADRESL. Any bits used for analog input must be set as inputs in registers TRISA and TRISE. Registers PIR1 and PIE1, which contain the ADC interrupt flag and interrupt enable bits respectively, are also used.

ADC can be controlled in the following ways:

Switching on

The ADC is switched on and off by the ADON bit of ADCON0. Switching it off when not needed helps save power.

Setting the conversion speed

Operation of the PIC 16F877A ADC is governed by the ADC ( Analog to Digital Converter ) clock, which has a period T. A full 10-bit conversion takes around 12 T cycles, depending slightly on which clock source is chosen. The user can select the clock frequency from a number of options. Although one generally wants a conversion to take place as quickly as possible, there is an upper limit to the clock frequency. For the 16F87XA the minimum clock period for correct operation is specified as 1.6 µs, or a frequency of 625 kHz. This implies a fastest conversion time of 19.2 µs. At the other extreme, if conversion is too slow, charge leaks from the storage capacitance and the conversion becomes inaccurate. Best practice is therefore to set the ADC clock frequency such that it has a period equal to or just less than 1.6 µs. Speed control can also be done using potentiometer in electronics ( Optional Topic – Click here to refer )

Selection of the ADC clock source is controlled by bits ADCS2 in ADCON1, and ADCS1 and ADCS0 in ADCON0. This shows that various divisions of the main clock frequency are possible. There is also a dedicated RC oscillator which can be chosen. This has a typical period of 4 µs, but may range from 2 to 6 µs. Configuring the input channels and selecting the voltage reference The ADC clock source selection is done by bits ADCS2 in ADCON1, and ADCS1 and ADCS0 in ADCON0.

ADCON0 Register in PIC Microcontroller
ADCON0 Register in PIC Microcontroller

A fast system clock is usually used to derive the clock source, whereas it is better to use the RC oscillator when the system clock is slow.

Configuring the input channels and selecting the voltage reference

The way the input port bits are used is defined by the setting of bits PGFC3 to PGFC0 of ADCON1. It is worth looking at this closely in the figure below. The variety of opportunity is impressive, both in terms of input channels and voltage reference. We can see that it ranges from just a single Port A channel used for input (PGFC3:PGFC0 = 1110) to all eight analog inputs in play (PGFC3:PGFC0 = 0000). Many combinations which include an external reference are also possible. Note again that any port pin that is to be used as an analog input must be set as an input in its TRIS register. Otherwise, the pin will act as an output and the (unintended) digital output value will be converted.

ADCON1 Register in PIC Microcontroller
ADCON1 Register in PIC Microcontroller

Selecting the input channel

The channel select bits CHS2 to CHS0 select the input channel present in ADCON0. These bits determine which switch in the ADC to close.

Starting a conversion and flagging its end

The GO/DONE bit in register ADCON0 initiates a conversion. When the conversion is complete the bit is returned to zero by the hardware. Completion of conversion is also signalled by an ADC interrupt flag ADIF.

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *