e011026.PDF

(103 KB) Pobierz
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
BEGINNERS COURSE
PC Serial Peripheral
Design (5)
Analogue measurements
By B. Kainka
So far in this series we have used the PC only with digital signals: switching,
monitoring and counting. Now we turn to the analogue domain: our programs
will understand not just ‘yes’ and ‘no’, but ‘larger’ and ‘smaller’.
nected between an output that has
been set high and an input, there are
only two possible results: either the
resistor is sufficiently small that a
clear logic one is read at the input, or
it is not. If more precision is required,
a new approach is needed.
For a 100 µF capacitor and a 1 k
R
resistor we have:
T = 1000
Ω ×
0.0001 F = 0.1 s =
C
100 ms
A doubling of the resistance leads to
a doubling of the charging time. The
same goes for a doubling of the
capacitance. So, we can measure the
charging time and deduce the value
of either one of the resistor or the
capacitor, if the value of the other is
known. All that is needed is a pro-
gram that replaces the switch in Fig-
ure 1. Figure 3 shows a simple circuit
where the capacitor is connected not
to ground but to the TXD output.
There is a good reason for this: if an
electrolytic capacitor is used, it must
never be charged with the wrong
polarity, and this is guaranteed as
long as TXD remains at –10 V.
The circuit charges and dis-
charges the capacitor via DTR and
uses the DSR signal as an input to
determine when the set voltage is
reached. The threshold voltage will
be around 1.5 V. Comparing this with
the overall voltage range of –10 V to
+10 V, we see that the threshold is
about 11.5 V/20 V = 0.575 = 57.5 %
of the final voltage. This is not too far
from our value of 63.2 % given above
for the time constant. In any case,
the error factor introduced is con-
stant and can be compensated for
later. There are other sources of error,
which we discuss below.
000074 - 5 - 11
Charging and discharging
Figure 1. Charging and discharging a capacitor
If computers are good at one thing,
it is counting. We can use this to
measure time: a program simply
counts the seconds (or milliseconds)
that pass until a certain event hap-
pens, for example when an input
changes state. If we can convert an
analogue quantity such as a resis-
tance into a time period, then it will
be easy to measure it with a com-
puter. In this example, we can use
an RC network. Figure 1 shows a
capacitor C charged and discharged
through a resistor R. A time constant
T is associated with an RC network:
If a voltage is applied to a serial port input, it
will be read as either a logic Low (0) or a logic
High (1). The PC cannot determine the actual
voltage present. Likewise, if a resistor is con-
T
T = RC.
Here T is the time taken for the volt-
age across the capacitor to reach
63.2 % (=1-1/ e ) of its final value. This
can be derived from the exponential
charging characteristic, shown in
Figure 2 . We shall not go into the
details of the physics here: it is
enough to know that the time taken
to charge to a given voltage is
directly proportional to the capaci-
tance and to the value of the resistor.
T
000074 - 5 - 12
Figure 2. Charge/discharge curves and
definition of the time constant T
26
Elektor Electronics
1/2001
855738241.050.png 855738241.061.png 855738241.071.png 855738241.081.png 855738241.001.png 855738241.002.png 855738241.003.png
 
BEGINNERS COURSE
Listing 1. Measuring the time constant in milliseconds
Private Sub Form_Load()
i = OPENCOM(“COM2,1200,N,8,1”)
If i = 0 Then
i = OPENCOM(“COM1,1200,N,8,1”)
Option1.Value = True
End If
If i = 0 Then MsgBox (“COM Interface Error”)
TXD 0
RTS 0
DTR 0
Counter1 = 0
Timer1.Interval = 2000
End Sub
DTR
R
DSR
C
100µ
–10V
TXD
000074 - 5 - 13
Figure 3. Automatic charging from the PC
Private Sub Form_Unload(Cancel As Integer)
CLOSECOM
End Sub
Private Sub Timer1_Timer()
DTR 1
TIMEINIT
While (DSR() = 0) And (TIMEREAD() < 1501)
DoEvents
Wend
Label1.Caption = Str$(TIMEREAD()) + “ ms”
DTR 0
End Sub
Counting time
The time measurement takes place
in a ‘while’ loop as shown in List-
ing 1 . The loop runs until either DSR
becomes set or the count reaches
1.5 seconds (timeout). The measure-
ment loop includes the command
DoEvents: this allows Windows to
process other events that may be
occurring in the system. During mea-
surements the user can still use the
mouse and other applications, and
indeed stop the program itself. This
is reassuring for the user, especially
when bugs in the program cause it
to function incorrectly. In general it
is always necessary, when program-
ming loops, to consider how the loop
can be forced to terminate. Other-
wise if the program gets into an infi-
nite loop the PC will need to be
reset, either by switching it off and
on again, or by using the familiar
Ctrl-Alt-Del chord. Adding a
DoEvents call makes the loop safe;
but this has a cost in timing accu-
racy, adding an uncertainty of
around 1 to 3 milliseconds to the
measured time.
one. As we said above, a 100 µF
capacitor and a 1 kΩ resistor give a
time constant of 100 ms. Similarly,
with a 10 µF capacitor we have a
time constant of 10 ms. This can be
tested by trying various capacitors
from the junk box. Often there will be
quite a large discrepancy between
the value measured and the value
printed on the capacitor: this is
down to the large tolerance (as much
as 50 %) quoted for electrolytics. The
capacitance of an electrolytic often
changes if it is stored for a long time.
The measurements are less accu-
rate for very large electrolytic capac-
itors, with values of say around
1000 µF. The indicated value will be
too small. The reason for this lies in
the program: the charged capacitor
must be discharged, which also
requires time. Our program uses a
timer with a period of two seconds:
one second is used for charging the
capacitor up to the input threshold
voltage; the remaining second, how-
ever, is not enough to discharge the
capacitor completely. The next
charge therefore takes less time.
There is a simple solution: a diode
can be used to accelerate the dis-
charge cycle. Figure 5 shows the cir-
cuit diagram, and Figure 6 shows
how it can be constructed. With this
modification to the circuit we can
Figure 4. Measurement with 100 µF and 1 k
DTR
R
D
1N4148
DSR
C
1...100µ
–10V
TXD
000074 - 5 - 15
Figure 5. Improved capacitance measurement
circuit
K1
000074-1
10
9
8
7
6
5
4
3
2
1
GND
RI
CTS
RTS
C
DSR
47 µ
GND
R
1k
D
DTR
µF not ms
TXD
RXD
DCD
1N4148
If the units ‘ms’ in the window in
Figure 4 are replaced by ‘µF’, the
value shown is not too far off the true
000074 - 5 - 16
Figure 6. Construction details.
1/2001
Elektor Electronics
27
855738241.004.png 855738241.005.png 855738241.006.png 855738241.007.png 855738241.008.png 855738241.009.png 855738241.010.png 855738241.011.png 855738241.012.png 855738241.013.png 855738241.014.png 855738241.015.png 855738241.016.png 855738241.017.png 855738241.018.png 855738241.019.png 855738241.020.png 855738241.021.png 855738241.022.png 855738241.023.png 855738241.024.png 855738241.025.png 855738241.026.png 855738241.027.png 855738241.028.png 855738241.029.png 855738241.030.png 855738241.031.png 855738241.032.png 855738241.033.png 855738241.034.png 855738241.035.png 855738241.036.png 855738241.037.png 855738241.038.png 855738241.039.png 855738241.040.png 855738241.041.png 855738241.042.png 855738241.043.png 855738241.044.png 855738241.045.png 855738241.046.png 855738241.047.png 855738241.048.png 855738241.049.png 855738241.051.png 855738241.052.png 855738241.053.png 855738241.054.png 855738241.055.png 855738241.056.png 855738241.057.png 855738241.058.png 855738241.059.png 855738241.060.png 855738241.062.png 855738241.063.png 855738241.064.png 855738241.065.png
 
BEGINNERS COURSE
Listing 2. Modifications for program Cmeas2.frm
Private Sub Timer1_Timer()
F = 1.19
DTR 1
REALTIME (True)
TIMEINITUS
While (DSR() = 0) And (TIMEREADUS() < 1500000)
Wend
T = TIMEREADUS() * F
REALTIME (False)
T = Int(T)
Label1.Caption = Str$(T) + “ nF”
DTR 0
End Sub
Figure 7. Capacitance display in nF
Listing 3. Measuring the time constant in microseconds
Private Sub Timer1_Timer()
DTR 1
REALTIME (True)
TIMEINITUS
While (DSR() = 0) And (TIMEREADUS() < 1500000)
Wend
t = TIMEREADUS()
REALTIME (False)
Label1.Caption = Str$(t) + “ us”
DTR 0
End Sub
DTR
R
D
1N4148
1...22k
DSR
C
47µ
–10V
TXD
000074 - 5 - 18
Figure 8. Circuit 1 with 10 k
potentiometer.
Listing 4. Determining the charging resistance
Private Sub Timer1_Timer()
DTR 1
REALTIME (True)
TIMEINITUS
While (DSR() = 0) And (TIMEREADUS() < 1500000)
Wend
T = TIMEREADUS()
T = T * 1.0000000001
R = 2200 + 7800 * (T - 76300) / (294600 - 76300)
REALTIME (False)
R = Int(R)
Label1.Caption = Str$(R) + “ Ohm”
DTR 0
End Sub
Figure 9. Measuring the charging time to
microsecond resolution
reliably measure capacitors up to
about 1500 µF. Larger values are pos-
sible if more time is allowed by mak-
ing the appropriate changes to the
program. The timeout value in the
measurement routine and the overall
timer period must both be increased,
so that the program waits long
enough for the measurement to be
completed.
What about capacitors smaller
than 1 µF? In principle the charging
resistor could be increased. How-
ever, a problem then arises: the
impedance of the DSP input (around
3k
increased. This problem could for
example be overcome using an oper-
ational amplifier with a high input
impedance, but this is outside the
scope of this series.
700
600
Software enhancements
T
(ms)
500
400
It is much more interesting to try to
get around these limitations in soft-
ware. In particular it is possible to
use a timer with a resolution of one
microsecond. This increases the res-
olution of the capacitance measure-
ment one thousandfold, allowing
measurements in nanofarads ( Fig-
ure 7 ). Library PORT.DLL provides
functions
300
200
100
0
0
5
10
15
20
25
R (k
)
000074 - 5 - 20
introduces measurement errors
that get more and more severe as the
value of the charging resistor is
Ω)
Figure 10. Charging time plotted against
charging resistance
TIMEINITUS
and
28
Elektor Electronics
1/2001
855738241.066.png 855738241.067.png 855738241.068.png 855738241.069.png 855738241.070.png 855738241.072.png 855738241.073.png 855738241.074.png 855738241.075.png 855738241.076.png 855738241.077.png 855738241.078.png 855738241.079.png
 
BEGINNERS COURSE
TIMEREADUS for this purpose,
where ‘US’ stands for microseconds
(µs). These functions are used in
Listing 2 .
When we consider making mea-
surements in microseconds, we must
look at the effect of Windows on the
timing accuracy. In principle other
process running in parallel can inter-
rupt the measurement program and
cause large inaccuracies. This can be
prevented by raising the priority of
the measurement task, for which a
special function is provided in
PORT.DLL. Using REALTIME (True)
we can obtain greater reliability. It is
essential to set REALTIME (False)
after the measurement has been
taken. The exact accuracy achieved
depends on the PC: with a 200 MHz
Pentium MMX we measured varia-
tions of about 50 µs in the measured
value; with a faster PC this may be
reduced. If the same experiment is
carried out in Delphi, the timing
accuracy is about 20 times better.
The method is described in the Elek-
tor Electronics book ‘PC Interfaces
under Windows’, to be published
soon). It is nonetheless impressive
that an interpreted language such as
Visual Basic can achieve such timing
accuracy.
Alongside these changes to the
program, we can also improve the
basic accuracy of the measurements.
We have already seen two sources of
error in the simple equation t/ms =
C/µF, namely the threshold voltage
being slightly too low and the input
impedance of the DSR signal. There
is a third source of error: the DTR out-
put does not switch exactly between
-10 V and +10 V, but has an internal
resistance of roughly 430 Ω. The
charging resistance is therefore in
effect about 1430 Ω. Further, this
internal resistance is non-linearly
dependent on the current. The TXD
output also has an internal resis-
tance, making the voltage on the
capacitor slightly higher than
expected. The effects are too compli-
cated to be analysed mathematically,
and so we take the course preferred
by experienced engineers in the face
of a complicated calculation: test;
measure; calibrate. All the errors can
be condensed into a single correction
factor F which can be determined
with a calibration measurement. For
this we require a capacitor whose
value is accurately known (or which
can be accurately measured). Then
the correction factor can be adjusted
until the reading is correct. On the
author’s PC the value of F was found
to be 1.19; this value can of course be
used on any PC, but there will be
individual variations from machine to
machine which can only be compen-
sated for by determining the correct
value of F in each case.
Resistance measurement
Figure 11. Measurement using a 15 k
resistor.
We can measure resistance using
the same method; this is similar to
the way that potentiometers are
read by PC games cards. The circuit
for measuring resistance is shown in
Figure 8 and is essentially the same
as the capacitance-measuring cir-
cuit. Here, however, we use a fixed
capacitor and work with various
resistor values.
To test this circuit we use the pro-
gram ( Listing 3 ) which measures the
charging time to the highest accu-
racy. We use again the technique
described above for measuring small
capacitors: REALTIME (True) gives
us good timing accuracy. Figure 9
shows how the results appear on the
screen.
The circuit can be tested using
metal film resistors with a tolerance
of 1 %. Measurements with an accu-
rate ohmmeter indicate that in gen-
eral the tolerance of such resistors is
rather better. A set of tests using
C=47 µF gave the following results:
The reasons for deviation from the ideal
linear curve are the same as were found in
measuring capacitance. With very small
charging resistances we get an error due to
the non-linear internal resistance of the DTR
output driver; with large resistances (say
around 22 kΩ) the low impedance of the DSR
input causes error.
The data obtained can be converted into
an explicit equation which will let us calcu-
late a resistance with high accuracy. Listing 4
shows the measurement routine for resis-
tance. The calculation in question reads:
R = 2200 + 7800 * (T - 76300) / (294600 -
76300)
Using this formula we can obtain a measure-
ment accuracy of around 1 % in the range
1.5 k
. It should be borne in mind,
however, that this function, specific to a par-
ticular PC, will not give the same accuracy on
a different machine. The various measure-
ments and calculations must be carried out
afresh for each PC. It is sufficient to measure
the charging times for two resistors, say
2.2 kΩ and 10 kΩ and substitute these in the
formula. Failing this, it is possible simply to
consider where the greatest sources of error
lie: in this case our attention turns immedi-
ately to the electrolytic capacitor. Electrolyt-
ics often have a capacitance very different
from the value printed on them, which has a
significant effect on the charging time for a
given resistance. If a suitable correction fac-
tor is inserted in the line
to 15 k
R/k
T/ms
0
33.7
0.1
34
0.22
34.5
0.56
37.9
1
45.5
2.2
76.3
4.7
147.5
6.8
204.9
8.2
245.9
10
294.6
15
433.7
22
661.2
T = T * 1.0000000001
From the numbers alone a strong lin-
ear dependency can be seen. An
exact determination of the linearity
is possible using a graph: an analy-
sis using Excel produced the curve
shown in Figure 10 . It can be seen
that linearity is good between
around 2.2 k
then this error will be compensated for, and
reasonably usable results (see Figure 11 ) can
be obtained. A correction factor of greater
than or less than one will be required accord-
ing to whether the capacitor has a value
lower or higher than nominal.
and 10 k
.
(00074-5)
1/2001
Elektor Electronics
29
855738241.080.png 855738241.082.png 855738241.083.png
Zgłoś jeśli naruszono regulamin