1
CHAPTER 5-HARDWARE
INTERFACING
ANALOG TO DIGITAL CONVERTER
(ADC)
2
What is Analog To Digital Conversion?
• It’s a process of capturing the analog electric signal (such
as sound captured by a microphone) and converting it to a
series of numeric digital values to be processed by a
microcontroller.
• The electronic device which is used for this conversion
process is known as A/D or ADC (Analog-To-Digital
Converter).
3
Physical Quantity (1)
Example of physical
quantity:
• Temperature
• Pressure
• Humidity
• Velocity
• Light
• Weight
• Airflow
• Sound
• Voltage
• Current
4
Physical Quantity (2)
• Every physical quantity found in nature like temperature, humidity,
pressure, force are analog in nature.
• A physical quantity is converted to electrical signal using tranducer
(sensor).
• These sensors produce output in voltage or current, both are in
analog form.
• We need to convert these analog quantities into digital to process it
using a microcontroller.
• This is done by using Analog to Digital Converters (ADC).
5
PIC18 ADC
• ADC in PIC18F45K22 microcontroller allows conversion
of an analog input signal to a 10-bit binary representation
of that signal.
• It has 28 channels which mean 28 analog input signals
can be converted simultaneously using this module.
• Terms for ADC process:
• Reference Voltage
• Resolution
• Step Size
• Conversion Time
• Digital Output
6
ADC Block Diagram
Note: Additional ADC channels AN5-AN7 and AN20-AN27 are
only available on PIC18(L)F4XK22 devices.
7
ADC Reference Voltage
• The reference voltage specifies the minimum and
maximum voltage range of analog input.
• There are two reference voltage: Vref- and Vref+.
• The Vref- specifies the minimum input voltage of analog
input while the Vref+ specifies the maximum.
Vref = Vref+ - Vref-
8
ADC Resolution (1)
• The resolution specified in bits and determines how many
distinct output codes/number of steps (2n) the converter is
capable of producing.
• ADC has n-bit resolution, with n = 8,10,12,16,24.
• An 8-bit ADC produces 28 or 256 output codes.
• A 10-bit ADC produces 1024 (210) output codes.
• A 16-bit ADCs produces 65,536 (216) output codes.
• The resolution indicates how much the reference voltage
can be divided.
9
ADC Resolution (1)
• For a 10 bit resolution ADC, it is possible to divide up to
1024 (210) voltages.
• For a 4.096V reference voltage, the minimum voltage will
be 4.096 V/1024 = 4 mV.
• This means 4mV in the analog pin will be detected as 1
and 8mV 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.
10
ADC Step Size (1)
n-bit Number Step size (mV) - Step size is the smallest
ADC of steps with Vref=4.096V change
that can be detected by ADC.
8 256 4.096/256 = 16 - This specifies how accurately
the ADC measures the analog
10 1,024 4.096/1024 = 4 input signals.
12 4,096 4.096/4096 = 1 -The higher resolution provides a
smaller step size.
16 65,536 4.096/65536 = 0.0625
Number of Steps = 2n
Step size = Vref / Number of Steps
11
ADC Step Size (2)
Steps
4.096 1024
1023
1022
n bit ADC = 10, Vref = 4.096 V
4.096V will be divided to 1024 steps.
Step size = 4.096 V / 1024 = 4 mV
4
3
2
1
12
ADC Conversion Time & Output
ADC Conversion Time Digital Data Output
• Is defined as the time • DOUT = ADC digital data
taken by ADC to output (decimal)
convert the analog
input to a digital DOUT = Vin/step size
(binary) number.
13
ADC Registers
The module has five registers:
1. A/D Control Register 0 (ADCON0)
2. A/D Control Register 1 (ADCON1)
3. A/D Control Register 2 (ADCON2)
4. A/D Result High Register (ADRESH)
5. A/D Result Low Register (ADRESL)
6. ANSELx
7.VREFCON0
14
ADCON0
15
ADCON1
16
ADCON2
17
ANSELA
18
VREFCON0
19
ADC Configuration
• When configuring and using the ADC the following
functions must be considered:
• Port configuration
• Channel selection
• ADC voltage reference selection
• ADC conversion clock source Not covered in
this course.
• Interrupt control
• Results formatting
20
Port Configuration Channel Selection
• The ANSELx and • The CHS bits of the
TRISx registers ADCON0 register
configure the A/D port
determine which
pins.
channel is connected
• For analog input we
to ADC input.
make ANSx = 1 and
• AN0-AN27
TRISx = 1.
21
ADC Channel (AN0-AN27)
22
ADC Voltage Reference
• The PVCFG<1:0> and NVCFG<1:0> bits of the ADCON1
register control the positive and negative voltage
references.
The positive voltage The negative voltage
reference can be: reference can be:
1. VDD 1. VSS
2. The fixed voltage 2. An external voltage
reference (FVR BUF2) source (VREF-)
3. An external voltage
source (VREF+)
23
Result Formatting (1)
• The 10-bit A/D conversion result can be supplied in two
formats, left justified or right justified.
• The ADFM bit of the ADCON2 register controls the output
format.
24
Result Formatting (2)
25
LM35 (Temperature sensor)
• LM35 output is 10
mv/°C
Temp Output
(°C) (mV)
2 20
3 30
To ADC
5 50
10 100
25 250
50 500
100 1000
26
Relating ADC Value to Voltage (1)
• The ADC reports a ratiometric value.
• If Vref = 4.096V and n = 10, ADC assumes 4.096V is
1024 and anything less than 4.096V will be a ratio
between 0 and 1024.
27
Relating ADC Value to Voltage (2)
• If the analog voltage is 2.12V what will the ADC report as
a value?
1024 = x
4.096V 2.12V
1024 * 2.12V = x
4.096V
x = 530
28
ADC Output (1)
For an 10-bit ADC, we have Vref+ =
4.096V and Vref- = 0V. Calculate the Dout
if the analog input is:
(a) 4 mV
(b) 8 mV
(c) 12 mV
(d) 1V
(e) 2.5V
(e) 4V
(f) 4.096V
29
ADC Output (2)
Solution:
Number of steps = 210 = 1024
Step size = (4.096 – 0) /1024 = 4 mV
(a) Dout = 4 mV/4 mV = 1
(b)Dout = 8 mV/4 mV = 2 or 10
(c) Dout = 12 mV/4 mV = 3 or 11
(d)Dout = 1V/4 mV = 250 or 11111010
(e) Dout = 2.5V/4 mV = 625 or 1001110001
(f) Dout = 4V/4 mV = 1000 or 1111101000
(g) Dout = 4.096V/4 mV = 1024 or 1111111111
30
31
Example:
Find the value for ADCON0, ADCON1, ADCON2, ANSELA and
VREFCON0 with setting as below:
- ADC Channel = AN4
- Vref- = AVSS
- Vref+ = FVR BUF2 (1.024V)
- Right justified
- 20TAD
- FOSC / 64
Write program to show ADC output on Port D and Port B.
ADCON0 = 0b _ _ _ _ _ _ _ _;
ADCON1 = 0b _ _ _ _ _ _ _ _;
ADCON2 = 0b _ _ _ _ _ _ _ _;
ANSELA = 0b _ _ _ _ _ _ _ _;
VREFCON0 = 0b _ _ _ _ _ _ _ _;
32
//Program ADC output on Port D and Port B
#include <xc.h>
void main (void)
{
TRISA5 = 1; //RA5 as analog input
TRISD = 0;
TRISB0 = 0;
TRISB1 = 0;
ANSELA = 0b_ _ _ _ _ _ _ _; //Enable AN4
ADCON0 = 0b _ _ _ _ _ _ _ _; //On ADC Module, AN4
ADCON1 = 0b _ _ _ _ _ _ _ _; //Vref(+)=FVR, Vref(-)=AVss
ADCON2 = 0b _ _ _ _ _ _ _ _; //Right Justified, 20Tad, Fosc/64
VREFCON0 = 0b _ _ _ _ _ _ _ _; //Vref(+)=1.024V
while(1)
{
ADCON0bits.GO_DONE = 1; //Start ADC conversion
while(ADCON0bits.GO_DONE == 1); //Wait until conversion finish
LATD = ADRESL; //Copy ADC result (bit 0 to 7) at LATD
LATB = ADRESH; //Copy ADC result (bit 8 to 9)to LATB
}
}