Understanding the Switch Matrix
Originally published on May 14th, 1996 in the Usenet newsgroup: Rec.Games.Pinball
Switch Matrix Basics
Switches fall into two catagories -- 'electro-mechanical' and 'solid state'. Most switches used in pinball machines are electro-mechanical. Microswitches, blade switches, tilt bobs etc. These switches have moving mechanical parts and are subject to the laws of physics governed by the materials they are made of -- steel, copper, brass etc.
There is one inherent problem with interfacing microprocessor based systems and mechanical switches and it's called 'bounce' (yes the thing that goes 'boing, boing'). When a mechanical switch closes, the contacts do not settle immediately, instead they bounce a number of times before they come to rest in their final closed position. A microprocessor (MPU) running at high clock speeds (typically executing a million plus instructions per second) is fast enough to detect each of these bounces as a valid 'make and break' action, and each will be acted upon independantly. However, this was not always the case. Early Bally solid state electronics could not process some switch closures reliably because the switch would close and open very quickly producing a fast 'switch edge'. To overcome this, a capacitor was installed across the switch to time stretch the switch closure and to give the MPU enough time to reliably sample the switch closure.
To overcome the multiple contact problem, a switch 'debounce time' is introduced.
What's debounce timing? Well, when the MPU detects a valid switch closure, lets say, a stand-up target worth 1000 points, the MPU makes a note of it, then waits for a period of time (which is usually governed by 'debounce' software timer or something equivalent) and then checks the switch again. If the MPU finds the switch is still closed, it processes the switch action (it adds the target value to the score). If you removed the debounce timing and shot the 1000 point target - you may well rack up more than 3000 points for a single hit!
Solid state switches are switches with no moving mechanical parts. Opto (short for 'optical' ) sensors are a prime example and are used commonly on current tables. They have their plusses and their minuses. They are more expensive than mechanical switches due to the additional circuitry required to decode them but do not suffer the contact wearing problems of mechanical switches. Also, because they have no moving parts they do not require 'debouncing' which makes for less house keeping software and more overall switch processing speed.
I should explain what an Opto-sensor is. An opto sensor 'sees' or 'detects' light. To be more specific, the sensor has two parts - the emitter which is the source of light and the receiver which detects light. The emitter usually takes the form of a light-emitting diode (an LED) and the receiver, -- a light-sensitive transistor. Together they form an optical switch, performing exactly the same function as a mechanical switch except that they use a beam of light rather than a mechanical actuator. Most (but not all) opto sensors use infra-red light which is not visable to the human eye. Most opto's have a transmitter and a reciever which is seperated by air.
I've knocked up a little circuit here to explain how the opto-sensors works:
The beam of infra-red light (signalled by the arrow between the LED transmitter and NPN transistor receiver - sorry I couldn't draw the emitter arrow) is unbroken -- uninterrupted. The current can flow through the transistor because it's switched on via the infra-red sensitive base which is receiving light from the LED. This deprives point 'A' of enough voltage to actually trigger the Schmitt trigger inverter. The invertor output is high (the inverse of it's input). If the beam of light is broken, the voltage at point 'A' rises as it is no longer flowing through the transistor, which is now off. This triggers the inverter into action forcing the output to change state. The output now goes low. This will be detected by any electronics scanning the circuits output. This is commonly known as an 'Opto-interrupter' -- where the beam of light is interrupted and causes a state change (don't get this confused with MPU interrupts - they are not related). A schmitt trigger is used to provide a 'clean' pulse.
The Switch Matrix: What is it? Why use it? How does it work?
Well, we need our targets, ball troughs, ball locks, tilt and other switches to be read by the MPU so we can have them acted upon when they close (their exact function is determined in software). If we have 64 switches in a pinball machine, then we need 64 seperate inputs that can be looked at. The first problem arises: this costs us a lot in hardware and a lot in PCB space. A compromise is reached: read the switches on an 8 x 8 matrix which reduces the overall cost but gives us the minor problems of a more complex switch system.
There is nothing wrong with a matrix (be it switch or lamp). It solves the problem very elegantly and is fairly easy to troubleshoot. However, if we loose row or column due to a broken switch wire or IC failure, it could mean that up to 8 switches will no longer respond.
The switches are arranged in a series of 8 x 8 as we've already mentioned - there are 8 rows and 8 columns. If you draw 8 horizontal lines on a peice of paper -- these are the rows. If you then draw 8 verticle lines down the page intersecting the row lines (thereby forming a grid),- these are the columns (like the columns that hold buildings up!). If you now count the number of times the lines intersect - you will get 64. Each of these intersections represents one switch in the matrix.
col2 col3 col4 col5 col6 col7
Each area identified SW 'nn' - would look like this:
You will notice a blocking
diode in the switch path between the row and the switch column this
is extremely important as we'll see. The arrangement for the
switch and the diode is common throughout the whole matrix
(the diode is not always connected to the switch -- Gottlieb
used 'diode strips' which were located under the playfield). Now
then, a couple more points to clear up before we push on. You
may have noticed the signal input arrows (>) at the row insert
points this is where the inputs to the matrix are and there are
obviously 8 of them. They are often refered to as strobes
or sends. personally I like the term 'send' as the other end
has 'returns' (<).True the signals do 'strobe' on and off sequentially
(much the same as a strobe light) but whatever you call the signal
it is purely subjective (you could call it the 'row on pulse'
Also, only 16 different coloured wires are required, again, 8 for the rows and 8 for the columns. Simple eh? Now all we have to do is drive some power through the rows and check the switch returns the other end to see if we get anything back for the microprocessor to read (indicating a closed switch).
Interfacing the Matrix with the Microprocessor
The switch matrix cannot be directly connected to the microprocessor -- it doesn't have the power to drive the matrix. We need a suitable interface between the microprocessor and the matrix to do the work, enter the most common interface chip for pinball machines on the planet - the 6820/1 PIA (Peripheral Interface Adaptor).
The 6820/1 has two 8-bit bidirectional ports which can have any of it's 16 pins configured as an input or an output. What a coincindence! We need one 8-way output port ('sends' or 'strobes') and one 8 way input port ('returns' - to read).
One port is known as 'Port A' and the other 'Port B'. The microprocessor can write to the output port which in turn will drive a voltage through the rows, into the switch matrix and back into the inut port of the PIA. Our microprocessor can safely read and write the matrix without burning up!
The PIA doesn't usually drive the matrix directly from it's output pin's either. Instead, a driver of some form is used. A pull-up resistor or driver transistor is common. The PIA 'B' port can actually output more current than the 'A' port (2.5mA) which would probably be enough but would offer no protection to the PIA if the matrix shorted and would significantly shorten the life of the IC (you would be 'loading' the output). So, it would be good pratice to protect the output stages using drivers as it also adds more power to push through the matrix which gives us a nice healthy signal.
If higher current drivers are used, current limiting resistors need to be added to the input pins to reduce the current to a safe level for the PIA to read, otherwise the PIA port input stage would burn up.
Another use of drivers involves not sending 5 volts through the matrix but higher voltages -- 12 or 24 volts springs instantly to mind. A driver transistor can be switched on with a 'pre-driver' transistor which, in turn is switched on by our PIA. The pre-driver runs from the 5 volt rail whilst the driver runs volt from the 12 or 24 volt rail which then pushes the higher voltage through the matrix. This method is known as level shifting. We are'shifting up to higher voltage/current.
Similarily, our PIA cannot read 12 volts or there abouts (don't try this at home), so the input has to be shifted back down again to a safe level -- 5 volts or less. You could use zener diodes, potential dividers or comparators (as with the WPC system).
So when the microprocessor writes data
to the output port of the PIA it is effectively driving by proxy the
row8 row7 row6 row5 row4 row3 row2 row1
('sends' or 'strobes') |bit7|bit6|bit5|bit4|bit3|bit2|bit1|bit0|
- the PIA output port (byte)
The microprocessor then reads the input
port to look for an active return line which will appear
as an active bit in the input port register, which then
would indicate a closed (or 'jammed' if in self test) switch. The microprocessor
would then initiate the debounce time, after which it would
then check the input port register again to see if it was
ret8 ret7 ret6 ret5 ret4 ret3 ret2 ret1
Finally (if the switch is closed) the microprocessor
executes the process associated with the switch -- scoring, tilt,
ball in lock or whatever.
The diodes remain in the matrix to prevent the voltage leaking back onto another line and giving a false switch registration when a switch closes.
The microprocessor via the PIA/ driver electronics sends a voltage down row 1. It then checks the returns for an active bit -- this would indicate that a switch or switches (1-8) are closed. It would then turn row 1 off and row 2 on and check the input port again for closures (switches 9-16 this time) and so on until all 8 rows and been turned on an all 8 returns had been read.
The microprocessor has to obviously perform this scanning routine 'n' number of times per second checking for new switches closing and previously closed switches opening again.
Well, I think that's it. There are a couple of loose ends -- not all matrix's use PIA's, some use transistors, comparators, pull-up resistors to push a higher current through the matrix as well as other interface methods to the microprocessor etc.
Hopefully, this was an insight into how the switch matrix functions.