Following is a reprint of an updated article by Vic Rice that appeared in the March ’85 issue of CTM magazine. I have retyped it, eliminating his typing errors and introducing some of my own. We are grateful to Chet Lambert of CTM who allowed us to reprint this excellent article.
The article is written for the beginners, of which we have quite a few. You seasoned charter subscribers can ‘hark forward’ to the schematic and get busy!
Introduction
This article is a revision of one originally published in the Nov/Dec 1982 issue of SYNC magazine. Since I am still receiving correspondence from readers who are building these units, there is apparently a continuing interest in this type of project. The circuit which I will describe has been modified to some extent from the original to take into account newer technology (64k RAM packs, for instance were not available when the circuit was designed), and for usability.
The subject of this article is a circuit known as an ‘RS-232 serial interface’. With this device you will be able to connect your ZX81/TS1000 to a host of expensive peripheral equipment including such exotic components as telephone modems, CRT terminals, serial printers, plotters and on and on. The RS- 232 convention is pretty well the most widely used method of connecting computers and I/O devices, the exception being printers. Some printers come with an RS-232 interface, some with a ‘Centronics’ parallel interface, and some with both. This article covers only the RS-232, not the parallel. Virtually all modems are RS-232, though.
This circuit board can be adapted to run off a 2068/Spectrum. The chief difference is in the data clocking circuit (noted later in this article), and in the board to computer connector plug. In the case of the 2068, it should be I/O mapped (using IOREQ rather than MREQ) since the 2068 contains a full 64K of RAM.
The cost of this project will run you about $10-$20 depending on how much shopping around you are willing to do for parts. On the other hand, the cost of printer, modems, etc to attach to it is limited only by your bank account.
(Editor’s note: Just noted in Computer Shopper is the best deal I’ve seen on a Hayes compatible 300-1200 smart modem. The Smart Team modem is $165 from The Wholesale Outlet 1-800-344-4387 (518-459-7883 NY res.).
Requirements
Construction of this board is very straightforward, and requires a minimum of equipment and experience. Writing programs to use the facilities it provides can be done in BASIC as long as you only use the lower speed settings. To take advantage of the faster transmission speeds of which this is capable, you will have to start using machine code, since the BASIC interpreter is too slow to be able to keep up. (Ed. Note – 300 baud is fine for use with the TNC-2 packet system, even though it will be transmitting at 1200 baud on VHF). I have run this circuit on my ZX81 at 9600 baud with no problems using an assembler program, as well as at 300 baud with a BASIC program.
Theory of Data Transmission
Internally micro computer chips use a parallel data bus of 8 bits (in the case of the Sinclair) but up to 32 or 64 bits are used in the most modern ones. Using the Sinclair CPU (the Z80) as an example, this means that when the CPU requests a byte of data from RAM, ROM, or elsewhere, it expects to have the 8 bits presented simultaneously on 8 separate lines. Since these 8 data lines are directly connected to the pins on the CPU chip, the electronic signals used are of either 0 or 5 volts and of extremely low power. This method of moving data around works just fine inside a box measured in inches but quickly becomes unworkable as the distances increases. Amplifying the signals to overcome the internal resistance of the wires is feasible for limited distances but electrical noise then begins to become a problem, especially with the number conductors involved, One obvious solution to this problem would be the use of coaxial cable such as that used between the ZX81 and the TV set. Having the signal move down a conductor surrounded by a grounded shield eliminates the majority of noise, but for an 8 conductor parallel bus this would require 16 wires. If, however, the 8 bit byte is turned sideways, and the bits are then clocked down a single wire at a predefined rate, the connection can then be made with a single coax cable with simple connections and a high degree of noise immunity. To further increase the noise immunity the RS-232 convention specifies a plus and minus voltage signal. Thus a ‘1’ is represented by a -3 to -15 volt signal and a ‘0’ by +3 to +15 volts. The effect of using a higher voltage combined with the zero level crossing provides a far greater range and very much reduced interference than the TTL signals emitted by the computer itself. (This also eliminates the need for coax, allowing the use of cheaper ribbon cable).
The RS-232 convention also covers the connectors (normally 25 pin D connectors), with the pin assignment being determined by the type of equipment being used. A device is either a DTE (Data Terminal Equipment) or a DCE (Data Communications Equipment). Nowadays we use a computer as a terminal and the DCE can be the modem, printer, etc. It is often hard to determine which is which, but a DTE transmits data on pin 2, while a DCE receives data on pin 2.
In order for the serial communication to function properly there are several key points on which the sending and receiving devices must be in agreement. These are:
BAUD Rate
This is the speed at which the individual bits of each byte are transmitted. A baud rate of 300 means that each bit ‘pulse’ will last approximately 1/300 th of a second. The terms ‘baud rate’, bits per second, and BPS are used interchangeably. (Ed. Note: Lo and behold, it turns out that the number of words per minute is also approximately equal to the baud rate). Obviously the higher the baud rate the faster data can theoretically be transmitted. In actual practice the over all data rate is entirely dependent on the devices doing the transmitting; typing at a terminal connected to a 1200 baud modem will still only transmit at the speed at which you can type, whereas a computer replying to the same terminal will be able to respond at almost the rated speed.
Character Length
ASCII (meaning American Standard Code for Information Interchange) is the most common ‘alphabet’ used in micro computers and for data transmission. (ASCII is NOT what is used internally in the Sinclair computer). Values from hex “00” to hex “7F” are assigned by the ASCII code for a normal maximum of 7 bits per character. The transmission rate of serial data is a function of the number of bits per character being sent; the fewer the number of bits per character, the faster they can be sent. The trade off for this is the fewer the bits per character, the fewer character combinations can be formed from those bits. (Remember the old 5 level Baudot code, with only capital letters)? Generally speaking, the full ASCII set is the norm, or 7 DATA bits per character.
Parity
With any form of communication you can still run into glitches,, especially with data being transmitted via the telephone network. In order to minimize data errors various forms of error checking methods are used, one of the simplest being parity encoding. The device at one end sends a byte down the line, counting the number of “1” bits sent. For EVEN parity, if the number of “1’s” is odd, an additional “1” parity bit is sent, or a “0” is sent if the number of “1’s” is even. In other words, the total of the one bits in a character is forced to be even. For ODD parity the reverse is true. The device on the receiving end then totals the number of one bits as he receives them and can then get upset if the total is wrong. (Note that if two errors occur on one character, the parity check may not catch it).
Stop and Start Bits
These are used to synchronize the sending and receiving devices. The START bit is a low signal before the actual data bits are sent. This tells the receiver to get ready, data is on the way. The STOP bits(s) are high signals following the last data bit; there must be at least one bit period gap between the last STOP and the next START bit. The reason for the start and stop bits is the wide variety of devices supported. The worst case is the human operator typing on a terminal; an experienced typist can reach the equivalent of 50 to 75 baud. Because the flow of data is irregular with large gaps between individual characters, re-synchronization of the communicating devices is required with every byte of data being sent.
Circuit Overview
The 8251 USART
The SAVE routine in BASIC uses a machine code routine to toggle the cassette output port at a specific (250 baud) rate which is matched by the LOAD routine. This is a feasible method for serializing data and can actually be used to drive an RS-232 circuit via the cassette port. In practical terms however, the possible variety of character lengths, parity and baud rates poses a very interesting problem in machine coding. The execution times of the instructions required must be accurately determined and delay loops carefully constructed in order to match the data rate with that of the receiving device. After that, routines to calculate parity setting and character lengths must be written, which will then affect the delay loops.,
To alleviate all this pain Intel has created the 8251 USART (Universal Asynchronous Synchronous Receiver and Transmitter). With this chip, sending data consists of initializing the chip (informing it of the require baud rate, character format, number of STOP bits and parity). After that you merely have to provide full bytes of data to be sent and the 8251 will happily look after all the nit-picking details required to send it. Depending on the device to which it is connected, it can also tell you if you are sending data too fast for the receiver to accept. The same holds true for receiving data; it will inform you when it has built up a full byte of data and will also tell you if it detected a parity error in that byte. The 8251 will also inform you if your program is accepting the data bytes slower than they are being sent (resulting in lost data).
Thus, the 8251 USART is the central player on this circuit, in the same manner that the CPU is the center of a computer. In just the same way that a computer consists of much more than a simple CPU chip, this circuit board requires several other ‘sub-systems’, the function of which are detailed below.
Addressing Circuit
It is obviously necessary to inform a specific chip when you want to it to perform, and when to sleep. In the back of the ZX81 owners’ manual is a picture of the RAM pack connector on the rear of your computer. In the diagram you will see pin 2A labelled RAM CS. This stands for RAM chip select, and the line over the label means that it is active with a low voltage. When this pin is grounded it signals the internal 1 or 2K RAM of the computer to place data on the CPU data bus from the address currently on the address buss. A 16K RAM pack permanently connect this pin to +5 volts, which turns off the onboard storage. chips.
The addressing circuit on the interface board must perform a similar task for the 8251 USART. This circuit is wired in such a way that when you reference either memory location 8192 or 8193 (with a PEEK or POKE) it will in turn activate the 8251 control or data port.
This type of select circuit is referred to as ‘memory mapping’ since the chip is pretending to be a byte in memory. An alternative to this is called “I/O mapping” when the addressing circuit is activated by IN and OUT commands. ZX81/TS1000 BASIC doesn’t allow for IN and OUT commands, making I/O mapping a dubious alternative since it would require that all your programming be done in machine language. (Ed. Note: Not exactly true. A short machine language routine could be put in a REM statement to do the IN/OUT functions and the remaining BASIC program could PEEK/POKE into this routine to perform the desired functions). The 2068 version of this board should be I/O mapped, though since all of the accessible storage is accounted for, and I understand that IN and OUT commands are available in BASIC for it.
Clocking Circuit
This set of IC’s provides the rhythmic pulses used by the USART when sending and receiving the data bits. The ZX81 has an internal clock to control the action of the CPU; the USART timing is directly derived from that by the simple method of dividing the high speed CPU clock rate to one usable by the USART.
Now for a quick refresher in math. The internal clock rate of the ZX81 is 3.25 MHz – 3 1/4 million ticks per second in human terms. The 8251 will divide the send/receive clock rate by 16 or 64 under software control to obtain the baud rate. The baud we want to achieve is either 300 or 1200 – the most commonly used rates. Therefore, the clock rate would be: 300*64 = 19,200 or 1200*16 = 19,200 Hz.
How do we get this clock rate? Simple, given a rate of 3.25 MHz, and a desired rate of 19.2 kHz, we divide 3,250,000 by 19,200 and get a nice round 168 factor. This horrid sounding number can be further subdivided into a cascade of simpler dimensions: 7*3*8, or 7*6*4. I chose the first because if you tap the clock (with a switch) from the divide by 3 you can also get baud rates of 2400 and 9600 in addition to the 1200 and 300. The 2068/Spectrum doesn’t lend itself as easily to this kind of tampering since the clock rate on those machines is 3.5 MHz, which doesn’t divide down so prettily. If you want to adapt this project to a 2068 it would be necessary to resort to either a crystal oscillator or one of the new baud rate generating chips to replace this part of the circuit. (Ed. note; a 555 timer IC set to 4800 Hz will do the trick).
Power Supply
The power for all these components can ge taken from the +5 coming from the computer itself. Or you may use a 7805 regulator to convert the +9 volt line from the computer power supply to the +5 required for the ICs on the RS-232 board. This spreads the load considerably and causes no heat problems with the computer. The power supply which comes with the computer is quite capable of handling the additional load with no problems. The RS-232 voltages can be obtained by using the +9 volts from the computer and a small 9 volt battery, since its current consumption is nil.
RS-232 to TTL Converters
As mentioned previously the RS-232 convention uses +/- 3 to 15 volts. The 1488 & 1489 converter ICs are essentially just heavy duty transistor gates which will translate from TTL to RS-232 voltages.
Mechanical Components
I personally used a Radio Shack IC spaced perf board (#2276-1396) and wire wrap socket for the various ICs. If you are used to using etched copper clad board then by all means do so; otherwise I would strongly recommend wire wrapping. since it is very much simpler to correct mistakes.
You will also require an RS-232 connector to fit whatever device you will be driving. A male DB-25 pin connector is needed to connect to the TNC-2.
Circuit Components
Now that we have covered the building block modules of the project, let’s cover the usage of the actual ICs. IC1 (74LS00) eliminates address lines A14 & 15. Otherwise you get ‘echoes’ of the USART throughout storage similar to the RAM and ROM echoes used by the Sinclair display routines. IC2 & 3 (74LS138) decode the address down to the byte level. IC2 provides addresses in 1K increments from 8K through 15K, which can be used for additional RAM chips if you want. IC3 provides addresses from 8192 through 8199; these can be used to activate extra RS-232 circuits or any other devices you may wish to add to the board, since the 8251 USART only needs two addresses. IC4 (74LS08) combines the control lines for the 8251 chip select and control/data port select.
IC6 (74LS04) part of this chip is used to invert the CPU clock from the ZX81. IC7 (7473) divides the CPU clock by 7. IC8 &9 (74LS90) divides the CPU clock by 3 and by 8. SW1 if desired can be used to tap the data clocking from the output off IC8 to provide a baud rate 8 times the output of IC9. Note that the output from the data clocking circuit is re- inverted using another part of IC6 before being applied to the data clock inputs of the 8251. IC10 (1488) converts TTL signals to RS-232 levels, necessary to drive the peripheral equipment. IC11 (1489) does the opposite, converting the incoming RS-232 levels to TTL. IC12 (7805) provides a regulated 5 volts for the ICs.
Putting it all Together
Assuming that you now understand the purpose of all the components, here are a few suggestions before you drag out your soldering iron or wire wrapping tool. The 8251 is a CMOS chip; this means BE VERY CAREFUL with it. CMOS chips are fairly robust while in the circuit, but take an extremely dim view of static electricity. While doing any work on the board, remove the 8251 and keep it sitting on a piece of aluminum foil, and avoid touching the pins with your fingers or anything conductive.
Be careful numbering your IC pins, when looking from the reverse side, the numbering is different! Pins 14, 15, 16, 18, & 19 on the USART are not required and are not shown on the wiring diagram. The 74LS04 should have all unused pins tied high to prevent noise problems. This means connect pins 5, 9, 11, & 13 to the +5 volt source (pin 14) of that IC.
The unused chip select lines from the two 74LS138s may be left unconnected. The same goes for the pins left unaccounted for on the clock circuit ICs. Check and recheck your connections with a multimeter BEFORE you apply power to the board. It is frustrating to have a smoking component in the middle of the night when you can’t replace it.
A logic probe can be a very useful tool for ‘debugging’ problems with addressing, etc. If you don’t own one and can’t borrow one, you can make a primitive version quite simply. Take the smallest LED you can find and attach its long pin onto a handy +5 volt source on the board. Solder a 200 ohm resistor to the LEDs short leg and there you are. Hook a piece of wire to the resistor and connect to the IC pin in question, power up the Sinclair, and PRINT PEEK nnnn where nnnn is the address to which you expect the IC to react. If the LED flashes you are OK. A short loop program that continuously does the PEEK will keep the LED on longer. If you can’t get it to flash try hooking the LED across +5 and to ground to make sure it is actually working. As a last resort try checking your wiring connections!
Final Testing
When developing software to run the interface, start simple. Just use PEEKs & POKES to display or send data and to familiarize yourself with the contents of memory and the control port. Connecting pins 2 & 3 together on the 25 pin RS-232 connector will loop your signals back so you will receive the characters you type.
The next part of this article will cover the programming of the interface. The schematic of the project is elsewhere in this issue.
Good luck, packet radio, here we come!
