The Beeb Body Building Course Part 13

A high speed A/D converter can turn your BBC Micro into an oscilloscope.

Volume 2

Number 1

March 1984

Here's an easy way to turn your Beeb into a real time oscilloscope

By MIKE COOK

WHILE the BBC Micro has more additional inputs than most other computers, it cannot hope to have all the different types we might need for every situation.

This is very pleasing for me as it keeps me in employment! However, it can be quite frustrating when you find the machine will not do quite what you want.

This occurred recently when a teacher asked me if it was possible to use the BBC Micro like an oscilloscope.

"No trouble," I said, "just use the built-in analogue input port".

Well it turns out that this is fine for slow waveforms, but anything over 30 Hz and the converter is just not fast enough to cope.

As the teacher wanted to display audio waveforms to the class, something extra needed to be built.

There are many analogue to digital (A/D) conversion techniques, and they are mainly a three way compromise between speed, accuracy and cost. So on a fixed budget you have to trade off speed for accuracy.

In all fairness, the BBC Micro has done very well with 10 bits accuracy and a 10 mS conversion speed. But, good as it is, it will not cope with all situations.

In the case I've just mentioned it is not fast enough to display an audio frequency waveform. There are other applications which we will meet later on in the Bodybuilding Course where a faster A/D converter is needed. So for this month's exercise we will look at how to add one to your micro.

The fast A/D converter board plugs into the user port and enables signals to be digitised over a range of ±5 volts. Each sample takes about 15uS to acquire, and is of 8 bits accuracy.

You see we have traded speed for accuracy. With 8 bits we can get a number from 0 to 255 whereas with the built-in converter we can have 0 to 1023.

Although the Basic ADVAL command returns a number between 0 and 65,536, there are only 1,024 significant increments.

So the built-in converter is four times more accurate but about 600 times slower.

The built-in A/D converter uses a technique known as a dual-ramp conversion process.

This is a very good way of getting a very accurate conversion but it is notoriously slow. In fact the one in the BBC Micro is the fastest one I have come across.

We will be covering the dual ramp process in a later article, so we will leave it there for the moment.

The A/D converter I have used here uses what is known as a successive approximation process. Most of the required components are on one chip, the Ferranti ZN427.

Successive approximation is a technique you may be familiar with, but you may not know it by that name.

Most people owning a computer must have played a number guessing game at some time or other.

It is the one where a computer guesses a number and you have to match it.

After each guess you enter you are told whether you are high, low or just right.

You start off just guessing wildly but pretty soon you develop a successive approximation technique.

Suppose the number is in the range 1 to 100. Your first guess should be 50, half the range.

If your guess is too high the number must be between 1 and 50. So your next guess should be 25, again half the range.

You can continue halving the range and eventually you are bound to find the correct number.

The successive approximation A/D converter works in a similar way. The block diagram is shown in Figure I.


Figure I: Block diagram of a successive approximation A to D converter

An unknown voltage is presented at the analogue input. This feeds into a comparator.

A comparator is simply an amplifier with two inputs whose output is high or low depending on which input has the higher voltage.

The second input is taken from a D/A (digital to analogue) converter.

As the name suggests this takes a digital number and converts it into a voltage. The register then makes a guess at the voltage by chosing the mid-point.

This is represented by the most significant bit of the digital input being high and the rest low.

The output of the comparator is fed back into the register which then decides whether to keep that bit high (if the input voltage is in the upper half of the range) or set it back to low (if it is in the lower half).

The register does this for each bit in turn in order of decreasing significance (bit 6, then bit 5 and so on).

After it has made the decision on all 8 bits, the number it is left with represents the value of the input voltage.

Note however that these are in arbitrary units and are relative to the maximum voltage produced by the D/A converter.

This does not matter in many applications as all we want is a number proportional to the size of the input voltage.

The speed of conversion is governed by a clock signal.

The slowest unit in the circuit is the comparator and the clock has to be sufficiently slow to allow it to settle.

However, as only one clock cycle is needed for each bit of accuracy (plus one to start it off) the technique is very fast.

Figure II shows the complete circuit of our fast A/D board. You will notice that as well as the ZN427 there are some other components.


Figure II: Circuit of the fast A/D

The A/D chip needs feeding with a clock signal to govern its speed of operation.

This is generated by IC 2 which is a 74LS13 consisting of two 4-input Schmitt input NAND gates.

One gate is wired up as a relaxation oscillator and basically it acts as an inverter.

When there is a logic one on all the inputs, there is a logic zero on the outputs.

Conversely, when there is a logic zero on one or more inputs there is a logic one on the output.

The gates have what is known as a Schmitt input, where the threshold voltage on the input - that is the voltage needed to make the input a zero or one - depends on what logic level is currently on the output.

This difference in threshold is known as the input hysteresis.

The oscillator works by connecting the output back to the input.

If the input is low then the output must be high. But if the output is high then the input is high therefore the output must be low!

What actually happens is that the logic state of the output constantly changes or oscillates.

If there were just a direct connection between input and output the frequency of oscillation would be determined by the propogation delay of the gate.

The propogation delay is the time it takes for a change on the input of a gate to affect the output.

This alone would produce an output of too high a frequency and so we have to introduce an extra delay. This is done by using a capacitor from the inputs to earth and a resistor in the feed-back path.

So as the voltage on the output goes high the capacitor will start to charge through the resistor until it reaches the positive threshold point where it switches the output to low.

The capacitor then starts to discharge through the resistor until the negative threshold point is reached when the output goes high again.

This produces a fixed frequency clock signal on the output of the gate, shown in Figure III.


Figure III: Waveforms of a relaxation oscillator

The resistor is split up into a fixed and variable resistor to allow the clock frequency to be altered slightly.

The ZN427 has a guaranteed conversion time of 15uS but, like all devices, can run slightly faster.

The speed depends on fine variables in the manufacture such as the exact purity of the semiconductor, the stray capacitance and what the machine operators had for lunch!

So by including the variable resistor you can get your converter working at its limit.

The second gate in the 74LS13 package is used to clean up the signal produced by the oscillator and provide a fast rise-time.

This clock signal is fed to the ZN427 and controls the conversion process.

The conversion is begun by applying a negative pulse to pin 4. This is known as the "Start of Conversion" signal or SC.

According to the data sheets, the positive edge of the SC pulse should not occur within 200nS of the falling edge of a clock pulse.

To ensure this, the SC pulse is also wired to the oscillating gate and when it goes low will force the output of the clock to go low also. When the SC goes high the oscillator will start again.

The SC pulse is wired up to the CB2 control line which can be made to pulse every time a number is written to the B side of the VIA.

As all the VIA data pins will be inputs this is known as a dummy write as it does not matter what is written only that something is written.

When the conversion has been completed the EOC line (end of conversion line) makes a positive transition.

This is connected to CB1 and can be made to generate an interrupt or set a flag.

In Basic programs, however, it is not necessary to look at this as the delay between starting off a conversion and getting the results is longer than the conversion time itself.

In order to make any form of A/D or D/A conversion it is necessary to have a voltage reference.

This is a voltage against which everything is measured.

The ZN427 has a built in voltage reference. But if you want to put your own in this can be done on pin 7. It should be in the range 1.5 to 3.0 volts.

This is useful when several A/D converters are being used so that they all have the same reference point.

The input circuitry consists of resistors R5-R7 which are arranged so that when the input is at earth, R6 is in parallel with R7.

As the latter pair are the same value (15k), their total resistance is halved (7k5), forming a potential divider with R5 to present half the reference voltage to the converter's input.

If the input goes below earth (negative) then the voltage on the input will drop below half way. Similarly when higher than earth it will rise to above half way.

The limits of conversion should be reached with the input voltage at ± 5 volts.

As there are resistors in the input circuitry you would have to supply in excess of ±10 volts to do the chip any damage.

The only fly in the ointment is that the comparator in the A/D chip needs a negative supply.

This could be taken from the auxiliary power socket on the BBC computer. But there is a better way.

As the current needed on this negative supply is very small it is possible to generate it from the positive supply.

As we already have an oscillator running it is fairly easy. We use something called a diode pump to generate a negative supply. Consider the circuit in Figure IV.


Figure IV: The voltage transitions on a capicitor

The capacitor has one side connected to +5 volts and the other side to earth through a resistor. Therefore there is a voltage across the capacitor and thus it has a charge stored in it.

If one end of the capacitor suddenly drops by 5 volts then the other end must also drop by 5 volts because instantaneously there is no change in charge.

So as one end drops from 5 volts to 0 volts the other end must drop from 0 volts to -5 volts.

The capacitor will then start to discharge through the resistor.

When the capacitor is discharged (no voltage across it) the input end is taken quickly to +5 volts again.

The other end responds by trying to rise to +5 volts to preserve the charge or lack of it. However the diode Dl then starts to conduct and rapidly charges the capacitor again.

This diode is said to clamp the capacitor to earth.

In Figure II diode D1 acts in the same way with diode D2 preventing any of the negative voltage getting back to the capacitor.

Therefore the capacitor pumps charge into the circuit which is stored in C3, producing our negative supply.

An alternative explanation is that if you waggle one end of a capacitor and clamp the other to earth you will get the inverse of the waggling at the clamped end.

To be honest that's the way I think of it as it is so much easier.

So with the circuit explained we can get on with the construction.

You can make it on Veroboard and connect it up to the user port using Body Build packs 1 and 2. That's the way I constructed the prototype.

Alternatively Body Build pack 10 contains all the parts and a printed circuit board.

You will still need a cable (Pack 2 or similar) to connect it up to the user port.

Just identify the component numbers on the screen printed circuit board with the component list or diagram and solder away.

The capacitors are marked with a + and the diodes are marked with a band to help you insert them the correct way round.

10 MODE 7
20 PRINT TAB(0,5);"THE BEEB BODY BUILDING COURSE No, 13"
30 PRINT "By Mike Cook"
40 PRINT
50 PRINT "FAST A/D CONVERTER TEST"
60 PRINT TAB(0,12);"CURRENT VALUE"
70 ?&FE62=0
80 ?&FE6C=(?&FE6C AND &F) OR &B0
90 ?&FE60=0
100 A%=?&FE60
110 PRINT TA8(0,10);A%;" "
120 GOTO 90
Listing I

Having constructed it, the program in Listing I should test it out. Line 70 ensures all the data lines on the user port are inputs. Line 80 sets the CB2 line to give a SC pulse when written to without affecting any control lines for the printer (side A of the VIA).

Line 90 starts the conversion, which is completed by the time line 100 is executed.

The result is plotted on the screen and the program then loops to get another sample. Forgive the GOTO but I have not been feeling well lately.

Run the program and apply a voltage to the input. As this voltage changes the number should respond.

Now to put it to some serious use. We could arrange the converter to generate an interrupt every time a conversion is completed just like the built in converter.

But, as the conversion time is very short, the computer would spend most of its time getting samples and you would think you had a ZX80 instead of a BBC! So we are only going to use it when it is necessary.

The program in the listing starting on Page 127 is for a digital storage oscilloscope using the fast A/D converter.

It can show signals quite clearly up to 2 KHz in real time and display even faster changes in the expanded mode.

The program works by first gathering 256 samples as fast as it can and storing them in memory. While it is doing this the interrupts are disabled so that the time between each sample is constant.

It then displays these samples as a dot plot, at the same time rubbing out the dot from the previous scan.

At first I tried using the operating system OSBYTE calls. This resulted in the program taking too long to run. The display was not updated fast enough.

So I had to write the program so that it altered the display by writing directly to memory.

To do this we need to know how the pixels on the screen relate to the memory locations. This changes depending what mode you are using.

I have written the program to use Mode 4 and it cannot easily be changed.

Figure V shows the relationship between address locations and screen position.


Figure V: Mode 4 address/pixel positions

Each bit in a byte corresponds to one pixel. So if we set bit 7 in memory location &5800, the top left hand pixel will come on.

As we move down the column the address location increases by one until it reaches &5807 and then the address jumps by &138. So the one under that is at address &5940.

In the X direction the address increases in steps of 8 from the first column.

It is obviously time consuming to make the calculation from an X-Y co-ordinate location to an address for each point plotted. So we do most of the calculations beforehand and store them in a look-up table.

This look-up table of the address of the first byte in each column is performed in lines 220 to 280.

The most significant byte is stored in a table starting at address &5700, and the least significant byte in a table at &5600. It is more convenient to split the values up in this way.

As we are plotting every possible X value we have to keep track of the number to add to the first column address.

A mask with a single bit in it is stored at &86 to tell us which bit in the pixel to change.

Whenever we plot a bit we store the address in a table (&5400) so that the next time round we can use the address directly to erase the point.

The first time the routine is entered there is no point to erase and so this table is filled with dummy points in the procedure WIPE. The addresses used are in ROM and so nothing will be affected.

This results in a very fast and responsive display. Try to alter the speed of the clock oscillator so that the data is gathered as fast as possible.

In order to view a stable trace it is necessary to start taking the samples from the same point on the waveform each scan.

This is made possible by supplying a trigger of threshold point.

The waveform must cross this point on a rising edge to trigger the data scan. If the waveform never reaches this point then there will be no display and the only way out is to press Break.

When the trigger point is set to zero there is no waiting, the scan proceeds immediately.

By pressing the space bar the trace can be frozen. You can then alter the trigger point or expand the scale of what you have. The dots may be isolated or joined up.

The scale factor may be any integer from 1 (no scaling) to 100 (quite a lot). The display always starts from the trigger point. You can make this a variable as well if you like. It is found in line 1290.

Well that just about raps it up for this month. Like all projects it is just a starting point for you.

You can calibrate the data capture rate so that you can guide a cursor through a frozen display and have a read-out of the time and amplitude at that point.

You could even include more complex triggering such as negative slope and delayed triggering. It is up to you. The hardware is there. It's just a matter of software.

The applications of this board are not just limited to displaying waveforms. I already have at least two projects in mind that will use this board so you should get your money's worth from it.

Well I'm not superstitious, but it's nice to get number 13 out of the way. See you next month.

Body Build Pack No. 10 consists of:

IC1 ZN427 A/D converter;
IC2 74LS13 NAND Gate;
R1 390R;
R2 1k
R3 47k;
R4 180R;
R5 7k5 1%;
R6 15k 1%;
R7 15k 1%;
VR1 100R preset;
Cl 4n7;
C2 1uF electro;
C4 1uF tant;
C5 0.1uF disc ceramic;
D1 D2 1N4148 diodes;
18 pin IC socket;
14 pin IC socket;
20 way IDC Header;
Fast A/D printed circuit board;
3 way terminal strip.

• You can order this pack and previous ones with the order form on Page 132.