This is only a preview of the March 1990 issue of Silicon Chip. You can view 44 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "6/12V Charger For SLA Batteries":
Articles in this series:
Articles in this series:
Articles in this series:
Articles in this series:
|
COM.POTER BITS
By STEVE PAYOR
Digital wave£orm generation
This month we will look at some of the finer points
of digital signal generation, using the simple 8-bit
D-A converter described last month. Armed with a
good working knowledge of BASIC, you should have
little trouble in getting your PC to generate useful
signals up to several kHz.
To begin, let's consider the question of software speed.
The programming language
which the author prefers to use is
TURBO BASIC (formerly produced by
Borland). This language is easily
managed by anyone who is proficient with GWBASIC, and it will run
99% of GWBASIC programs without
any changes.
A program originally written in
GWBASIC will run from 2 to 100
times faster under TURBO BASIC.
The difference is that TURBO BASIC
is a compiler, whereas GWBASIC is
an interpreter.
An interpreter processes each
line of the program as it comes to it.
For example, a line such as "GOTO
500" is scanned character by
character until the word "GOTO"
has been recognised as a reserved
word, then the number "500" is put
together by taking the '' 5' ',
multiplying by 10, adding a "0",
multiplying by 10, and adding the
other "0". As if this isn't slow
enough, in order to perform the actual GOTO, the interpreter now has
to scan through all the line numbers
in the program listing to find line
500, if it exists.
The next time this statement is
encountered, the interpreter has to
go through the entire process again.
A compiler will initially process a
"GOTO 500" in the same way, but
having deciphered it, all that will
remain in the final machine code is
a single JMP instruction, which only
takes a few microseconds to
execute.
Traditionally, compilers have
tended to be rather awkward in interactive situations. Before a program can be run, it first has to be
compiled, and this used to take
some time and lots of keystrokes,
whereas an interpreter like
GWBASIC only requires you to type
RUN (or press the F2 key) and the
program is off and running.
TURBO BASIC is just as easy to use
in this regard. You can go from
editing to running a program with
two keystrokes, and the only difference you will notice is a delay of
around a second or so while the
program is being compiled.
Machine code
This photo shows the completed D-A converter with the filter components
added. The filter gives a response that is 13dB down at lOkHz.
80
SILICON CHIP
An alternative, and very effective way of speeding up a GWBASIC
program is to write the most critical
part of the program in machine
code. In our case, this is the loop
which outputs the bytes to the D-A
converter. The machine code for
this part only requires simple integer arithmetic. The rest of the
program, which contains tricky
stuff like floating point SIN calculations, can be left in its original, slow
UNFILTERED
OUTPUT
(1k)
CENTRONICS
PIN NUMBERS
(9) 07
JUMPER
2.7k
.01
10•1.
1.,.
* 5.6k
(8) 06
OUTPUT
ADJUST
VR1
2k
5.6k
(7) 05
22k
*470!J
(6) 04
* MAY NEED ADJUSTMENT
ALL RESISTORS 1%
(4) 02
180k
(3) 01 o--..JN,Ar--•
(19) TO (30) GNO~
running form, because it is only used once during the waveform setup.
Another way of getting a faster
program is to use a more machineoriented language such as "C". The
code produced by the TURBO C
compiler is almost as short as handwritten machine code. (We hope to
present some C routines in a future
issue. At this stage the results appear to be about 2-3 times faster
than TURBO BASIC).
In summary, the options, in order
of increasing speed, are:
(1). GWBASIC interpreter (waveforms up to a few hundred Hertz).
(2). TURBO BASIC or similar compiler
(approx. 10kHz).
(3). TURBO C compiler (approx.
20kHz).
(4). Any language with a machine
code subroutine (Z0kHz plus).
(5). Hardware buffer memory for
D-A converter (MHz).
Turbo Basic
Fig.1: the filter circuit consists of an inductor and two capacitors and is simply
tacked onto the output of the D-A converter circuit described last month. It
gives approximately .06dB ripple from 0-4.5kHz and 13dB of attenuation at
lOkHz.
For simplicity, we will stick with
TURBO BASIC for now. As mentioned
last month, we achieved a sample
rate of 20k samples/sec on a
4.77MHz PC-XT. This was achieved
using the tightest possible loop we
could write.
The fact that the loop execution
time is almost precisely 50µ.s is just
a coincidence, but it does make the
figures come out nice and round.
For example, the maximum waveform frequency we can construct is
tokHz.
Before we can start producing
useful waveforms however, we
need to add one more thing to our
D-A converter hardware - namely, a low-pass filter.
The need for a filter
Fig.2: what better way to reveal the filter's frequency response than with a
linear frequency sweep, from lkHz to beyond tokHz. Note the aliasing around
the lOkHz point. The spectrum folds back on itself beyond this point - a
direct consequence of the 20kHz sampling frequency.
Sampling theory says that if we
have a sampling frequency of
20kHz, then a low-pass 0-tokHz
filter is required to reconstruct the
desired waveform without distortion. Furthermore, this only applies
for a filter with an ideal, infinitely
sharp, rectangular cutoff.
If you try generating a t0kHz
signal, you may wonder why such a
sharp cutoff is necessary. There
will be exactly two samples per cycle, one up, one down, and the
MARCH 1990
81
Fig.3: This is a logarithmic frequency sweep, from 400Hz
to 4kHz (just r ight for testing a voice communications
channel perhaps?) The sweep time is .05s.
Fig.5: a rather short logarithmic frequency sweep, from
lkHz to 2kHz in 5ms, ju'st to show how clean the
waveform can be.
Fig.4: exactly the same waveform as Fig.3, but with the
filter out of circuit. What a mess!
Fig.6: the same as Fig.5 but without the filter to
"reconstruct" the waveform.
resultant square wave is easily
filtered to a pure sine wave by a
filter with a cutoff anywhere below
the third harmonic; ie. 30kHz.
However, if you try to generate a
9.9kHz signal, the true nature of
the problem becomes apparent. Because there are not quite exactly
two samples per cycle, the sampling
points on the waveform gradually
shift until they pa ss through the
zero crossings, and then the
amplitude begins to rise again. The
resulting waveform looks like a double sideband signal, which is exactly what it is.
You have actually generated two
frequencies, namely 9.9kHz and
10. lkHz. The 10. lkHz signal is an
"alias", or beat frequency , caused
by the beating of the 9.9kHz signal
with the ZOkHz sampling frequency.
Wanted: Your Circuit & Design Ideas
Have you got a good circuit idea languishing in the ol' brain cells? If
so, why not send it in to us and save us from circuit burnout?
We'll pay up to $ 50 for a really good circuit . So transfer your circuit to
paper and send it to SILICON CHIP, PO Box 139, Collaroy Beach, NSW
2097.
82
SILICON CHIP
The only way to convert this
heavily modulated signal to a pure
sine wave at 9.9kHz is to have a
filter which will pass the 9.9kHz
component, and completely reject
the 10. lkHz component. This is a
rather tall order.
Filter compromise
We chose a simple 3rd order lowpass filter for our demonstration
unit, mainly because it only requires one inductor to be wound
and the results repres ent a
reasonable compromise between
performance and complexity.
Referring to the circuit diagram,
the filter is simply tacked onto the
output of last month's D-A converter circuit. If you like, the ex-
' SILICON CHIP LOG/LINEAR FREQUENCY SWEEP GENERATOR
DEFINT A-Z
DIM WAVE(lOOOO)
'
'
'
'
(TURBO BASIC 1.1)
All variables are integers unless otherwise stated
Array containing output data (sufficient for approx.
1/2 sec of stored waveform, given the sample time
listed below)
'Program constants:
PORT.A=&H378
' Parallel port
Note: Other possible addresses
PORT.C=PORT.A+2
' addresses
for PORT.A are Hex 3BC or Hex 278
PI!=3.141593
SAMPLE.TIME!=50E-6 ' 50fsec (as determined by experiment for this computer, a
'4.77 MHz PC-XT, running this program. This will need to
' be changed to accommodate faster or slower systems.)
' Variables defining frequency sweep:
'
START.FREQ!=lOOO
' Hz
END.FREQ!=lOOOO ' Hz
DURATION!=.1
sec
Change these three variables
to produce the desired sweep
range and time
N=DURATION!/SAMPLE.TIME! ' Total no. of samples in sweep
PHASE!=O
'This section of code fills the output waveform
FOR I=O TON
'array with the required frequency sweep (this
WAVE(I)=127.5+128*SIN(PHASE!) 'takes a few seconds)
FREQ!=START.FREQ!*EXP(I/N*LOG(END.FREQ!/START.FREQ!))' Log sweep, or
'FREQ!=START.FREQ!+I/N*(END.FREQ!-START.FREQ!)
'Linear sweep
PHASE!=PHASE!+2*PI!*FREQ!*SAMPLE.TIME!
' (Disable either of the
NEXT I
'above statements by
' changing it to a remark)
OUT &H21,INP(&H21) OR 1 ' Disable DOS real time clock interrupt
WHILE NOT INSTAT
OUT PORT.C,O
'Keep repeating the frequency sweep until a key is pressed
' Positive edge of CRO sync pulse
'------------------------------------------------------------------------------
FOR I=O TON
' This FOR loop outputs the w~veform to the D-A converter.
OUT PORT.A,WAVE(I) ' As you can see, it is about the tightest loop that can be
NEXT I
'written in TURBO BASIC - It takes 50fsec.
'-----------------------------------------------------------------------------' "Zero" the waveform
OUT PORT.A,127
OUT PORT.C,1
WEND
' Negative edge of CRO sync pulse
OUT &H21,INP(&H21) AND &HFE ' Restore clock interrupt
END
Fig.7: this listing is for a sweep generator with programmable sweep time & start & end frequencies
isting .0047µF capacitor can be
combined with the .OlµF capacitor,
but we kept them separate for those
occasions when we might want to
use the D-A converter without a
filter eg, when generating
square pulses.
The component values chosen
give a Chebyshev response with approximately .06dB ripple from O to
4.5kHz, which is the usable frequency range for a flat, clean out-
put. The response is 13dB down at
lOkHz, which is not quite infinite,
resulting in some noticeable aliasing in this region (see Fig.2).
However, for a signal at say 4kHz,
the nearest alias is at 16kHz, where
the filter response is well down.
So, for frequencies up to 4kHz at
least, the signal quality is perfectly
acceptable: as good as any function
generator, and second only to a low
distortion analog oscillator, such
as a thermistor stabilised Wein
bridge.
Pick a waveform
The big advantage that a software driven D-A converter has over
other waveform generators is that
it can produce any waveform you
want. So what should we try our
hand at first?
The author's first thoughts were
along the lines of a tone burst
MARCH 1990
83
generator, to specification IHFA-202 1978 (see SILICON CHIP, July
1988}. However, you will have to
wait for this one. This month's software solves a more immediate
need.
For our first demonstration program we present a frequency
sweep generator, with programmable start and end frequencies, as
well as sweep time. You also have a
choice of logarithmic or linear frequency sweep.
If you have access to a CRO, this
is just the ticket for instant frequency response tests, and incredibly
useful for filter alignment, which is
why we chose .it first up: to align the
D-A's own filter.
Looking at the CRO photograph
(Fig.3} you will see that the frequency response is quite flat up to 4kHz.
(Each horizontal graticule ·division
is lkHz, starting at lkHz). Twiddling the inductance adjuster, you
will find that the response
" droops" slightly if the inductance
is too low, or " peaks" if the inductance is a shade too high. Adjusting
this filter for a flat response
How To Make The Inductor
First of all, you will need a
suitable ferrite core. We chose a
a PCB-mounting RM10 core
assembly, with an AL value of 400.
Nearly all the ferrite core manufacturers can supply this type and it is
very easy to assemble and mount.
If you are using a different core
type, you will need to know its AL
value in order to calculate the
number of turns required. The
calculation is quite simple:
( 1 ) . Express the desired inductance in nH; eg, 37 .3mH =
37,300,000nH
(2). Divide this by the AL value of
the core; eg, 37,300,000 + 400
= 93,250
(3). Take the square root; eg,
(93,250)½ = 305.4 = the required number of turns (305 and a
couldn't be easier!
One final note: In order to get a
perfectly stable CRO display, we
used bit O of PORT.C to output a
ANTRIM
TOROIDAL TRANSFORMERS
QUALITY TOROIDAL POWER TRANSFORMERS,
MANUFACTURED IN U.K. NOW AVAILABLE
EX-STOCK AT REALISTIC PR ICES.
half will be near enough).
If the core has an adjuster, find
out how much it can increase the
inductance and subtract about half
this amount before doing the
above calculation. For example,
the adjuster on our ferrite core had
an adjustment range of +20%, so
we calculated the number of turns
for an inductance of 37 .3mH less
10% = 33.6mH, which came to
290 turns.
We used 0.25mm enamelled
copper wire, for which there is ample room within the core window.
A complete RM10, AL 400 core
assembly, including adjuster, coil
former and mounting clips is
available from Radiospares Components (stock no. 228-258) for
$5. 7 4 plus sales tax.
TTL level trigger pulse. This is a
short, negative-going pulse, the
positive edge of which signifies the
start of the frequency sweep.
~
General
Construction
OUTER
W INDING
INNER
WINDING
CQ AE INSULA TION
TAX PAID PRICES
15VA
30VA
50VA
SOVA
120VA
160VA
225VA
300VA
500VA
625VA
10+
1- 9
32 .80
31.70
36.00
35.00
38.50
37.20
41.75 40.35
44.95
43.50
55.70
52.20
62.00
58.20
72.80
68.25
100.00 93.75
112.00 105.00
Enquiries from resellers and OE Ms welcome. Quantity prices and data sheets available on request.
Distributed in Australia by Harbuch Electronics Pty Ltd, 90 George St., HORNSBY, NSW, 2077
Phone (02)476-5854 Fax (02)476-3231
84
SILICON CHIP
|