You are on page 1of 9

COMP1208 COMPUTER SYSTEMS 2007

Computer Systems 4: Analogue to Digital Converter (ADC)

1. Introduction
We have seen that data is stored digitally in a microcomputer. However, if we wish to connect a digital
computer to external hardware containing an analogue device, such as a sensor, this requires the use of an
Analogue to Digital Converter (ADC). The ADC does the conversion from an analogue signal to a digital
representation of that signal.

Digital
signal
Analogue
Microcomputer
signal
1

1
Physical
phenomenum Analogue to 0
IN 0xDD
1 PORT
Digital
sensor Converter 1

(ADC) 1

Figure 1: Example – Connecting Sensor to a PC

In this chapter we explore the basic principles of interfacing analogue devices to a digital microcomputer.

Notes04-ADC.doc Page 1-1 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

2. Analogue-to-Digital Converters (ADC)


An Analogue-to-Digital Converter is a device that provides an output that digitally represents an input
voltage or current level. Most ADCs convert an input voltage to a digital word (i.e. 1’s and 0’s). Digital
words have a FIXED number of different values whereas analogue quantities have an INFINITE number
of different values.

Vref

D0
Vin A D1
D2
D D3
D4
C D5
D6
D7

D = 2 n x Vin/Vref

Figure 2: 8-bit ADC

ADC has an analogue reference voltage to which the analogue input is compared. The digital output tells
us what fraction of this reference voltage is the input voltage. This is given by the formula:

D = 2n x Vin/Vref
Where
D = decimal value of output code
n = number bits in digital output code = Resolution of ADC
Vin = input analogue signal
Vref = reference voltage

Here is a simple example of a 3-bit ADC. Because it has three output bits, this means there are 8 (23 )
possible output codes.

If Vref is 10V, this would give us the following table mapping input voltage ranges to an output binary
code:
Range Voltage Range (Vin) Binary Code
0 0.0 –> 1.25 000
1 1.25 –> 2.5 001
2 2.5 –> 3.75 010
3 3.75 –> 5.0 011
4 5.0 –> 6.25 100
5 6.25 –> 7.5 101
6 7.5 –> 8.75 110
7 8.75 –> 10.0 111

For example, if Vin = 3.5V, then

-> D = 2n x (Vin/Vref) = 23 x 3.5/10 = 210

-> Output code = 0102

Notes04-ADC.doc Page 2-2 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

As we cannot possibly represent all the possible analogue values with different digital combinations we
need to somehow limit the number of analogue values to some fixed, finite number that is less than or
equal to the number of digital combinations available. This restriction operation is achieved by having a
fixed number of analogue value sub-ranges, that is we divide up the full range of analogue values into a
number of sub-ranges, each one covering part of the total range of values expected. A common method
used is to linearly divide the analogue range into equal sized sub-ranges to completely cover the
maximum expected range of values in the original analogue signal. Other division schemes are also used
for different applications but are beyond the scope of our present study (e.g. telecommunications use a
scheme called 'companding').

Original Analogue Signal Sampled Signal

8 8
7
6
Voltage

Voltage
5
4
3
2
1
0 0
Time Time

Quantised Signal (Digital) Re-constructed Analogue Signal

8 8
111
7 110
Voltage
Voltage

6 101
5 100
4 011
3 010
2 001
1 000
0 0
Time Time

Quantisation Actual Value


Error
Quantised Value

Figure 3: Quantisation Errors

When we have our list of sub-ranges we can allocate each different analogue value to a particular sub-
range, we call this process 'Quantisation'. For each of these sub-ranges we can allocate a digital
combination or value, these digital values can thus 'represent' the analogue values that fall within their
particular sub-range.

Truncation or rounding may be employed. With truncation, the encoded value of the analogue sample
is assigned the next lower quantisation level. With rounding, the encoded value of an analogue sample is
assigned the nearest quantisation level. For example, if the code '0110' is used to represent voltage 3.0 V
and '0111' for 3.5 volts, then with truncation, samples 3.1 V and 3.4 V would be assigned the digital word
'0110', however with rounding, sample 3.1 V would be rounded down to 3.0 and assigned the digital word
'0110' and sample 3.4 would be rounded up to 3.5V and assigned digital word '0111'.

Notes04-ADC.doc Page 3-3 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

Example 1
An analogue signal of between 0 volts to 10 volts to be represented by a 3-bit binary word. A 3-bit code
has 23 or 8 combinations. We need to divide the analogue signal into 8 subranges. Each sub-range will
represent a voltage range of 10/8 volts. This gives 1.25 volts per sub-range, the first sub-range starting at
0 volts.
D = 2n x (Vin/Vref) = 23 x (Vin/10)
Range Voltage Range Binary Code
0 0.0 –> 1.25 000
1 1.25 –> 2.5 001
2 2.5 –> 3.75 010
3 3.75 –> 5.0 011
4 5.0 –> 6.25 100
5 6.25 –> 7.5 101
6 7.5 –> 8.75 110
7 8.75 –> 10.0 111

Table 1: Example using 3-bit Binary Code


Example 2
An analogue signal of between 0 volts to 8 volts to be represented by a 4-bit binary word. A 4-bit code
has 24 or 16 combinations. We need to divide the analogue signal into 16 subranges.

Each sub-range will represent a voltage range of 8/16 volts. This gives 0.5 volts per sub-range, the first
sub-range starting at 0 volts.

D = 2n x (Vin/Vref) = 24 x (Vin/8)
Range Voltage Range Binary Code
0 0.0 – >0.5 0000
1 0.5 -> 1.0 0001
2 1.0 – >1.5 0010
3 1.5 –> 2.0 0011
4 2.0 –>2.5 0100
5 2.5 –>3.0 0101
6 3.0 -> 3.5 0110
7 3.5 –> 4.0 0111
8 4.0 - >4.5 1000
9 4.5 –> 5.0 1001
10 5.0 –> 5.5 1010
11 5.5 –> 6.0 1011
12 6.0 –> 6.5 1100
13 6.5 –> 7.0 1101
14 7.0 –> 7.5 1110
15 7.5 –> 8.0 1111
Table 2: Example using 4-bit Binary Code

Notes04-ADC.doc Page 4-4 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

2.1 Quantisation Errors


When we examine the digital values we use to represent analogue sub-ranges we see that the same binary
code can be used to represent a number of different analogue values. In Table 2 above we see that the
binary code '0110' is used to represent voltages such as 3.05, 3.21, 3.3456, 3.4995, etc, in fact any voltage
between 3.0 and 3.5 volts. If at some later time we wish to recover the original analogue signal from its
stored digital representation we do not know which particular voltage was present in the original signal,
all we know is that it was 'somewhere between 3.0 and 3.5 volts'. Whatever voltage we convert '0110' to
will result in an error for many of the original values we converted.

The errors introduced in this process are called 'Quantisation Errors' as the original analogue values have
been forced, or quantised, to one of a set of fixed values. Thus when we see the reconstructed signal the
voltages seem to 'jump' between these fixed values rather than changing gradually as they would have
done in the original signal.

The average error is less when rounding than with truncation, but there are some situations in which
truncation is preferred. In general, for unipolar encoding and rounding, the normalised quantisation error
is given by: +/-(1/2n-1 ). Thus if there are 4-bits the normalised quantisation error = +/- (1/23 ) => +/-
12.5%.

2.2 Sampling Rate and Precision


When you sample the wave with an analog-to-digital converter you have control over two variables:
• Sampling rate - controls how many samples are taken per second

• Sampling precision - controls how many different gradations (quantisation levels) are possible
when taking the sample

In the following figure, let's assume that the sampling rate is 1,000 per second and the sampling precision
is 10:

Figure 4: ADC - Sampling rate is 1KHz,


Precision is 10 Figure 5: Recreated Waveform

The rectangles represent samples. Every 1/1000th of a second, the ADC looks at the wave and picks the
closest number between zero and 9. The number chosen is shown along the bottom of the figure above.
These numbers are a digital representation of the original wave. When the DAC recreates the wave from
these numbers, you get the solid line shown in the following figure:

Notes04-ADC.doc Page 5-5 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

You can see that the solid line lost quite a bit of the detail originally found in the original line, which
means the fidelity of the reproduced wave is not very good. This is the sampling error. You reduce
sampling error by increasing both the sampling rate and the precision. In Figure 6, both the rate and the
precision have been improved by a factor of 2 (20 gradations at a rate of 2,000 samples per second):

Figure 6: ADC - Sampling rate is 2kHz,


Precision is 20

In Figure 7, the rate and the precision have been doubled again (40 gradations at 4,000 samples per
second):

Figure 7: ADC - Sampling rate is kHz,


Precision is 40

You can see that as the rate and precision improve, the fidelity (similarity between the original wave and
the DAC's output) improves. In the case of CD sound, fidelity is an important goal, so the sampling rate is
44,100 samples per second (44.1 KHz) and the number of gradations is 65,536 (216 i.e. a 16 bit ADC). At
this level, the output of the DAC so closely matches the original waveform that the sound is essentially
"perfect" to most human ears.

Notes04-ADC.doc Page 6-6 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

3. Example of Temperature Control System Using ADC


Analogue signals are input to a sensor, conditioned, converted into bits that a computer can read, and
analysed to extract meaningful information. Data from a computer is converted into an analogue signal
and output to an actuator. This data acquisition hardware is either internal and installed directly into an
expansion slot inside your computer, or external and connected to your computer through an external
cable.

Temperature Control Example


Using ADC on Application Board

PC
LEDS
7 6 5 4 3 2 1 0
MOTOR
Direction Parallel
Relay Port
B Port
O/P C
D o
a n
t t Control
HEATER
a r program
o
P l
TEMP Signal
sensor Conditioning ADC Port o P
A r o
t r
I/P t

Figure 8: Block Diagram of a Temperature Control System.

Given the Temperature Control system in the figure above, it is possible to write a simple temperature
control program to monitor the temperature sensor and turn on and off the fan motor and heater to control
the temperature.

3.1.1 C++ Function to Data from Temperature Sensor


int read_temperature()
{
int temperature;

_outp(CNTRL_PORT,0x20); // set bit 5 on Control Reg -> read data

temperature = _inp(DATA_PORT); //byte read from the Data Port

_outp(CNTRL_PORT,0x00); // reset bit 5 on Control Register

return temperature;
}

The temperature sensor produces an analogue output voltage proportional to temperature.


The development of thermistor technology has lead to the IC temperature sensor in which the
temperature-sensitive junction(s) and the required signal conditioning circuits are provided in a single
monolithic package. The analogue voltage is converted by the ADC to an 8-bit code with value 0 to 255.
To read the ADC output bit 5 of the control port is first set to indicate that the data port shall be read. In
read mode, the data port is connected to Port A of the application board. Then the data port value is read
into local variable called temperature which is then returned to the calling function.

Notes04-ADC.doc Page 7-7 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

3.1.2 C++ Function to Turn on Heater


void turn_on_heater(void)
{

_outp(CNTRL_PORT,0x01); // set bit 0 on Control Reg -> write data

_outp(DATA_PORT,0x20); // Data port bit 5 set -> Heater ON

_outp(CNTRL_PORT,0x00); // reset bit 0 on Control Register


}

To turn on the heater in this example, bit 0 of the control port is first set to indicate that the data port shall
be written to the data port Then bit 5 of the data port is set, as this sets the port B bit 5 on the application
board which is turns on the Heater.

3.1.3 C++ Function to Turn on Motor (Forward)


void turn_on_motorFWD(void)
{

_outp(CNTRL_PORT,0x01); // set bit 0 on Control Reg -> write data

_outp(DATA_PORT,0x40); // Data port bit 6 set -> Motor ON Forward

_outp(CNTRL_PORT,0x00); // reset bit 0 on Control Register


}

To turn on motor in this example, bit 0 of the control port is first set to indicate that the data port shall be
written to the data port. Then, bit 6 of the data port is set, as this sets the port B bit 6 on the application
board which is turns the motor on in the forward direction

3.1.4 C++ Function to Turn on Motor (Forward)


void turn_on_motorRev(void)
{

_outp(CNTRL_PORT,0x01); // set bit 0 on Control Reg -> write data

_outp(DATA_PORT,0x80); // Data port bit 7 set -> Motor ON Reverse

_outp(CNTRL_PORT,0x00); // reset bit 0 on Control Register


}

To turn on motor in reverse in this example, bit 0 of the control port is first set to indicate that the data
port shall be written to the data port. Then, bit 7 of the data port is set, as this sets the port B bit 6 on the
application board which is turns the motor on in reverse

3.1.5 C++ Function to Turn off Heater and Motor


void turn_off_all(void)
{

_outp(CNTRL_PORT,0x01); // set bit 0 on Control Reg -> write data

_outp(DATA_PORT,0x00); // Data port bit 5,6,7 reset


// -> Heater off and motor off

_outp(CNTRL_PORT,0x00); // reset bit 0 on Control Register


}

Notes04-ADC.doc Page 8-8 26/02/08


COMP1208 COMPUTER SYSTEMS 2007

3.1.6 Simple Program Example


This program monitors the rising temperature until it is above a maximum defined value when a fan is
turned on until the temperature reduces.

#include <conio.h>
#include <iostream>
using namespace std;

void delay(int time);


void turn_on_motorFWD(void);
void turn_on_heater(void);
void turn_off_all(void);
int read_temperature(void);

#define DATA_PORT 0x378 // Address of Data Register


#define CNTRL_PORT 0x37A // Address of Control Register

int main()
{
int temp;
const int MAX_TEMP = 0x40;

// Turn on heater and read temperature until it rises above MAX_TEMP


turn_on_heater();
do{
temp = read_temperature();
delay(5000);
cout << "Current temperature in hex = 0x"
<< hex << temp <<endl;
}while( temp <= MAX_TEMP );

// Turn on fan and read temperature until it falls below MAX_TEMP


turn_on_motorFWD();
do{
temp = read_temperature();
delay(5000);
cout << "Current temperature in hex = 0x"
<< hex << temp <<endl;
}while( temp >= MAX_TEMP );

turn_off_all();

return 0;
}
/* Function Definitions */
:
:

3.1.7 In class Exercise - Temperature Control Program


Write a temperature control program to monitor the temperature sensor continuously. If the temperature
exceeds MAX_TEMP, the program should turn off the heater and turn on the motor until the temperature
drops below MIN_TEMP. The temperature monitor function should run in a continuous loop until the
user requests the program to stop

Notes04-ADC.doc Page 9-9 26/02/08

You might also like