ZX Burglar Alarm

Author(s): D. C. McMahon
Publication: LISTing Newsletter
Date: June 1997
Pages: 5-8

Feeling insecure? Try this neat burglar alarm interface for your ZX81.

THIS MICROPROCESSOR controlled security system will monitor up to eight remote switch positions each of which can be either normally open or normally closed, allowing you to use any combination of pressure mats, magnetic catches, window foil or other devices as the sensors. When triggered, it immediately sounds an alarm and then displays the number of the affected input on a seven-segment LED. It can be used with the ZX81 and quite aside from its value as an intruder alarm it provides a useful introduction to the techniques of microprocessor interfacing using machine code and the Z80 Parallel Input/Output Controller.

The Z80 PIO consists of two groups of eight lines, port A and port B, and each line can act as either a data input or output. If the IN instruction is used, the data on those lines defined as inputs is loaded into the accumulator. If the OUT instruction is used, data on the accumulator is latched onto the output lines. The PIO can be programmed to act in any one of four modes:

Mode 0 — Output mode
Mode 1 — Input mode
Mode 2 — Bidirectional mode
Mode 3 — Control mode

The first three modes require the use of the handshaking facilities and so for this design the much simpler control mode has been used, allowing easy input and output of data to and from both eight bit ports on the PIO. Port A provides the eight inputs from the sensing switches while port B is split into four inputs and four outputs, the inputs being used to enter data while the outputs feed the seven segment display. An eight bit word is entered via port B into the register which tells the microcomputer which of the input lines should be high and which low (corresponding to normally open and normally closed switches respectively). The contents of this register are then continuously compared with the inputs to port A and if any discrepancy is discovered a ‘9’ is sent out to the display. A timing loop, formed by loading a high number into the registers H, L, and then successively subtracting one until the result is zero, ensures that the nine is displayed for at least a second or so, after which the number of the affected input is displayed. An AND gate monitors the A and D data lines into the display driver and thus goes high when the 9 is output, the delay ensuring that it remains high long enough to latch the relay and thus sound the alarm.

Construction

Most of the components, including the relay and the transformer, are mounted on the PCB, the only off board components beings the switches, the LED display, the edge connector to suit the ZX81, and the siren or other output transducer. Make sure that all four ICs are inserted the right way around, and similarly check the electrolytic capacitors C1, C4, and C7, and the diodes. Provision has been made for the use of connectors for the LED display and the input lines but if you prefer you can, of course, solder directly to the board. It is intended that the relay should switch a siren or similar device which draws its power from an external source, e.g. the mains, but if your particular application does not specifically demand an eardrum piercing, complaint eliciting siren, you might prefer to use an audible warning device of some sort instead. Providing this does not draw more than 100 mA or so and will run from 17V or less, you can connect it directly in the collector circuit of Q1 and dispense with the relay entirely. The edge connector for the ZX81 should be wired in accordance with Fig. 2.

The choice of case is left entirely to the constructor, but since there is mains on the board it is advisable to have some sort of enclosure. Mounting the switches should present no problems but the LED display is not so easy. If you’re after a particularly neat appearance you would perhaps do best to go for an easily cut plastic case, and to cut out an aperture for the LED and then mount it flush in epoxy. The input lines, mains input, and connections to the microcomputer could either be taken through grommets or, if you’re really fussy, through appropriate connectors, although it is probably most convenient to use a connector only for the input lines.

Fig. 1 Circuit diagram of the burglar alarm.
Fig. 1 Circuit diagram of the burglar alarm.
                  TOP                        BOTTOM

                  R̅F̅S̅H̅             23        R̅O̅M̅C̅S̅
                  M̅1̅               22        A4
-12V --           R̅E̅S̅E̅T̅            21        A5
+12V --           B̅U̅S̅R̅Q̅            20        A[illegible]
                  W̅A̅I̅T̅             19        A7
-5V --            B̅U̅S̅A̅C̅K̅           18        A[illegible]      -- R̅E̅S̅E̅T̅
                  W̅R̅               17        A[illegible]      -- B̅U̅S̅R̅Q̅
                  R̅D̅               16        A10               -- U
                  I̅O̅R̅Q̅             15        A11               -- V
                  M̅R̅E̅Q̅             14        A12               -- Y
                  H̅A̅L̅T̅             13        A13               -- VID
                  N̅M̅I̅              12        A14               -- 0V
                  I̅N̅T̅              11        A15               -- I̅O̅R̅Q̅G̅E̅
                  D4               10        A3
                  D3                9        A2
                  D5                8        A1
                  D[illegible]      7        A0
                  D2                6        CLK
                  D1                5        0V
                  D0                4        0V
                                   KEYWAY
NC --             R̅A̅M̅C̅S̅             2        +[illegible]V UNREG
                  D7                1        +5V REG

23 WAY FOR ZX81    - KEY AT POSITION 3
Fig. 2 Pin connections of the ZX81 expansion port
Fig. 2 Pin connections of the ZX81 expansion port

Table 1 Examples of eight bit PIO address words.

A7  A6  A5  A4  A3  A2  A1  A0  PORT
0   0   0   1   1   1   1   1   A data
0   1   0   1   1   1   1   1   A control
0   0   1   1   1   1   1   1   B data
0   1   1   1   1   1   1   1   B control

Table 2 Format of the operation control word.

BIT             7   6   5   4   3   2   1   0
output mode     0   0   1   1   1   1   1   1
input mode      0   1
bidirectional   1   0
control         1   1

Programming

The Z80 PIO has six control lines, three of which (MI, IORQ, and RD) can be connected directly to their counterparts on the ZX81 edge connector. The remaining three, B/A SEL (select port A or B), C/D SEL (select either control or data carried on bus), and CE (chip enable) must be connected to the address bus. The ZX81 address bus has the following characteristics: A0, A1, A2, A3, and A4 are all normally high (they are used to control printer, loudspeaker, etc.), so we can leave these high and connect B/A SEL, C/D SEL, and CE to the remaining three lines, A5, A6, and A7 respectively. A5 low selects port A, A5 high selects port B; A6 low selects data (input and output) and A6 high selects control (programming information). A7, the chip enable, is always held low. The resulting eight bit words are shown in Table 1 and their decimal values are 31, 93, 63, and 127 respectively.

We must next initialize the PIO by sending two control words to each port. The first defines which mode and, as we are using mode three, a second must be sent to define which of the eight lines are inputs and which outputs. The format of the operation control word is shown in Table 2, and it will be seen that the relevant control word for ports A and B is 11111111, that is, decimal 255.

The second control word also consists of eight bits, each one corresponding to the I/O line with the same number, i.e., bit 0 corresponds to PA (or PB) 0, bit 1 corresponds to PA1, etc. Setting the bit high defines the associated I/O line as an input, while setting it low defines it as an output. Since port A consists of the eight input lines from the various sensing switches, its control word will be 11111111, again, decimal 255. Port B has lines 0, 1, 2, 3 outputting data to the LED display and lines 4, 5, 6, and 7 accepting input data from the push button switches, so its control word will be 11110000, that is, decimal 240.

The first six instructions of the program therefore consist of loading the relevant control word into the accumulator and outputting it to either address 93 or address 127 (see Table 4 and the flow chart, Fig. 3).

Before the progam can be entered, you will need to reserve space for the 108 bytes of machine code by moving RAM-TOP. To do this type in:

POKE 16388,147
POKE 16389,67
NEW

and follow each statement with the Newline command. To check that RAMTOP has been moved, type in:

PRINT 256*PEEK 16389 + PEEK 16388

and you should get 17299.

Having reserved the 108 bytes after RAMTOP in the ZX81, type in:

10 FOR N=0 to 107
20 INPUT X
30 POKE 17299+N,X
40 NEXT N
50 PRINT USR 17299

then RUN. The computer will then wait for you to type in the 108 numbers given in Table 3.

When the programming has been completed, a 1 should appear on the LED display. This refers to the first line on port A, and you must now tell the computer whether this line is to be high or low, according to what type of sensing switch you plan to use on it. To do this you first press either switch SW3 (LOW) if the line is to be normally closed or SW4 (HIGH) if the line is to be normally open, and then press the ENTER switch SW2. The LED should now display a 2, and you repeat the procedure with this and each of the subsequent lines.

When all eight lines have been entered and the register is full, the microprocessor goes into a continuous loop, checking each line against its corresponding bit in the register. Should you wish to redefine the normal state of the lines, pressing SW1 empties the register and thus stops the program. If the alarm is triggered, it can be reset by pressing first SW2 and then SW1.

Fig. 3 Flow chart of the burglar alarm program.
Fig. 3 Flow chart of the burglar alarm program.
ZX Burglar Alarm PCB foil pattern (ETI)
ZX Burglar Alarm PCB foil pattern (ETI)
Fig. 4 The overlay diagram for the PCB. Note: the circuit was originally designed for a PC-mounting transformer which may not be available. An ordinary transformer can be mounted off the board with the leads soldered to the PC pads.
Fig. 4 The overlay diagram for the PCB. Note: the circuit was originally designed for a PC-mounting transformer which may not be available. An ordinary transformer can be mounted off the board with the leads soldered to the PC pads.

Table 3 The ZX81 data.

62, 255, 211, 93, 211, 93, 211, 127, 62, 240,
211, 127, 219, 63, 203, 111, 40, 250, 6, 8,
62, 0, 30, 0, 60, 211, 63, 219, 63, 203, 119,
40, 5, 203, 127, 32, 246, 28, 219, 63, 203,
111, 32, 250, 203, 11, 16, 232, 62, 0, 211,
63, 219, 63, 203, 103, 40, 210, 219, 31, 171,
6, 8, 203, 23, 56, 4, 16, 250, 24, 237, 197,
62, 9, 211, 63, 6, 10, 17, 1, 0, 33, 222, 57,
237, 82, 32, 252, 16, 247, 193, 120, 211, 63,
219, 63, 203, 111, 32, 250, 219, 63, 203, 103,
32, 250, 24, 160

PARTS LIST

Resistors (all 1/4 W, 5%)
R1                         4k7
R2, 4,5,6, 7,8,9,10        470R
R3                         330R
R11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24     10k

Capacitors
C1                         1000u 16V electrolytic
C2, 3                      100n
C4                         22u 16V electrolytic
C5, 6                      10n ceramic
C7                         100u 10V tantalum

Semiconductors
IC1                        7805
IC2                        7408
IC3                        7447
IC4                        Z80A PIO
Q1                         BD131
D1, 2                      1N4001
D3                         1N4148
DISP1                      Common anode 7-segment display

Miscellaneous
RLA1                       12V DC 400R miniature relay
T1                         9-0 9V 6VA transformer
SW1, 2, 3, 4               momentary action, push-to-make

PCB: edge connector to suit ZX81; 10-way 0.1" pitch PCB plug and socket — 2 off each; case, etc. to suit.
LD  A      255         62,   255
out A 93 211, 93
out A 93 211, 93
out A 127 211, 127
LD A 240 62, 240
out A 127 211, 127
IN 63 219, 63
BIT 5 203, 111
JRZ -6 40, -6
LD B 8 6, 8
LD A 0 62, 0
LD E 0 30, 0
INC A 60,
out A 211, 63
IN 63 219, 63
BIT 6 203, 119
JRZ 5 40, 5
BIT 7 203, 127
JRNZ -10 32, -10
INC E 28,
IN 63 219, 63
BIT 5A 203, 111
JRNZ -6 32, -6
RRE 203, 111
DJNZ -24 16, -24
LD A 0 62, 0
out 63 211, 63
IN 63 219, 63
BIT 203, 103
JRZ -46 40, -46
IN 31 219, 31
XOR E 171,
LD B 8 6, 8
RLA 203, 23
[illegible] C 4 56, 4
DJNZ 16, -6
JR -19 24, -19
PUSH B 197
LD A 9 62, 9
out 63 211, 63
LD B 10 6, 10
LD DE 1 17, 1, 0
LD HL 14814 33, 222, 57
SBC HL, DE 237, 82
JRNZ -4 32, -4
DJNZ -9 16, -9
POP B 193
LD A, B 120
out 63 211, 63
IN 63 219, 63
BIT 5 203, 111
JR NZ -6 32, -6
IN 63 219, 63
BIT 4 203, 103
JRNZ -6 32, -6
JR -96 24, -96
Table 4 Assembler listing of the burglar alarm program.
Table 4 Assembler listing of the burglar alarm program.

HOW IT WORKS

The various intruder detecting switches are connected between ground and the eight lines of port A on the PIO. Each of the eight lines is connected to the +5V line through a pull-up resistor, so that when the associated switch is open a logic high level will appear on the input, and when the switch is closed the line will be pulled down to logical low. The latter four lines on port B are similarly connected so that pushing any of switches 1 to 4 takes the associated line low. The first four lines on port B are used for the display output and carry a four bit binary code. This is fed directly to the decoder/driver 7447 and then to the seven segment display.

When the program is executed it puts out a ‘1’ and then waits for the line to be defined. Taking either line 6 or line 7 on port B low enters a 0 to 1 as desired into register E. Subsequently taking line 5 low initiates a rotate right instruction which moves the entered data one place to the right so that the register is ready to receive the next bit. The microprocessor then outputs a ‘2’ and the process is repeated until register E is full.

The microprocessor then goes into a continuous loop, using the XOR function to simultaneously compare each input line with the corresponding bit in register E. If both bits are at the same level, either both high or both low, the XOR function will produce a 0 output, but if the two bits are at different logic levels the XOR will give a 1. The RLA instruction is used to shift each bit into the carry flag and test for a 1 and if no carry is detected the microprocessor carries on testing the lines.

When a 1 is detected, a nine is briefly sent out via port B to the display. At the same time, a large number is loaded into registers H and L and 1 is successively subtracted until the result is zero. A total of 148 140 machine cycles are needed for this, and the nine is therefore displayed for a full second or so before the micro processor removes it and displays instead the number of the failed line. The AND gate IC2b has its inputs connected to the A and D lines from port B, and will therefore go high only when a nine is put out. Its output is connected to IC2a, another AND gate, which is wired as a latch. IC2a drives the transistor Q1 which turns on the relay. The other input of IC2a is connected to line 5 of port B, and if SW2 is pressed this line will go low, unlatching the gate and thus turning off the relay.

Also printed in LISTing January 1997.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.