Professional Documents
Culture Documents
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
In this chapter we explore the basic principles of interfacing analogue devices to a digital microcomputer.
Vref
D0
Vin A D1
D2
D D3
D4
C D5
D6
D7
D = 2 n x Vin/Vref
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
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').
8 8
7
6
Voltage
Voltage
5
4
3
2
1
0 0
Time Time
8 8
111
7 110
Voltage
Voltage
6 101
5 100
4 011
3 010
2 001
1 000
0 0
Time Time
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'.
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
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
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%.
• 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:
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:
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):
In Figure 7, the rate and the precision have been doubled again (40 gradations at 4,000 samples per
second):
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.
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
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.
return temperature;
}
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.
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
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
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int temp;
const int MAX_TEMP = 0x40;
turn_off_all();
return 0;
}
/* Function Definitions */
:
: