Subject: TECH: Basics of Switches and the Switch Matrix
From: Clive Jones <[email-masked]>
Date: Tue, 14 May 96 09:39:00 PDT
Newsgroups: rec.games.pinball

Hi Chaps.

Okay, so last time I *may* have been a little technical with my WPC   
switch matrix description (or so I was told in private mail) but, most of   
us probably understand the basics, but for those newbies who don't - I've   
put together a beginners guide to understanding the switches and the   
matrix.

(oh yeah, the wasps seem to have left my workshop alone now - many thanks   
to that curry!).

*Understanding Switches*

Switches fall into two catagories - Electro-mechanical and Solid State.   
Most switches used in Pinball machines are Electro-mechanical -   
Microswitches, Blade switches, Tilt bobs etcetera. These switches have   
*moving* mechanical parts and are subject to the laws of physics governed   
by the materials they are made of - steel, copper, whatever.

There is one inherent problem with interfacing microprocessor based   
systems and mechanical switches - it's called 'bounce' (yes the thing   
that goes 'boing, boing').

 Everytime a mechanical switch closes, the contacts do not settle   
immediately, they 'bounce' a number of times before they come to rest in   
their final closed position. A microprocessor (uP) running at high speeds   
as they do (typically executing a million plus instructions per second)   
is fast enough to detect each of these bounces as a real (valid) 'make   
and break' action, and, each will be acted upon independantly, to   
overcome this a switch 'debounce time' is introduced.

What's debounce timing? - Well, when the uP detects a valid switch   
closure, lets say, a stand-up target worth 1000 points, the uP makes a   
note of it, then waits for a period of time (which is usually governed by   
'debounce' software timer) and then checks the switch again. If the uP   
finds the switch is *still* closed, it processes the switches action (it   
adds the targets value to the score).

The length of debounce time required is usually given by the switch   
manufacturer in the switch data sheet, which the software engineer can   
then base his software timing on (in some cases slightly longer to get a   
definate 'clean' switch register).

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 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 and require additional circuitry, 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' code and more overall switch processing speed.

I should explain what an Opto-sensor is. 'Opto' means 'light', *most*   
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.   
The transmitter is *usually* an infra-red LED (light emmiting diode) and   
on the recieving end - a transistor who's base region is sensitive to   
infra-red light.

I've knocked up a little circuit here to explain how the opto-sensors   
works;

      +5 volts

          ^
          |
          +------[180]-------------.             |\
          |                        |          (a)|  \
          '------[270]--.          +----[2k2]----|[]  >O------ on (low)
                        |          |             |  /
                       === --.   |/              |/
                  LED  _v_    -> |\. NPN Tran.
                 Trans  |          | receiver     Schmitt Invertor
                        |          |            (74LS14 or similar)
                        '----------+
                                   |
                                   |
                                   |
                                   v

                                0 volts

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 - not interrupted, the current can flow   
through the transistor because it's been switched on by 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.   
If the beam of light is broken the voltage at point 'A' goes up as it's   
no longer flowing through the transistor which is now 'off' - this   
triggers the inverter into action forcing the output to change state (go   
low) which with be detected by the electronics 'scanning' the circuits   
output. This is commonly known as an 'Opto-interrupter' - were the beam   
of light is interrupted and causes a state change (don't get this   
confused with uP interrupts which we discussed in previous posts - they   
are not related). A schmitt trigger is used to provide a 'clean' pulse   
(well in my design anyway).

*The matrix*

What is it? why? and how does it work?

Well, we need our targets, ball troughs, ball locks, tilt and other   
switches to be read by the uP 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 that need to be read individualy, then we   
need 64 seperate inputs that can be looked at - this costs us *alot* in   
hardware and alot in PCB space and ultimately alot out of our pockets, so   
a compromise is reached - read the switches on an 8 x 8 matrix, which   
reduces cost but gives us the problems of a more complex switch system.

There's nothing wrong with a matrix, it solves the problem very   
elegantly, and is fairly easy to troubleshoot, until we loose row or   
column which means upto 8 switches are no longer responding.

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 - these are the columns   
(like the columns that hold buildings up!). If you now count the number   
of times the lines intersect - you will have 64, each of these represents   
*one* switch in the matrix.

         col1  col2  col3  col4  col5  col6  col7  col8

row1>-----+-----+-----+-----+-----+-----+-----+-----.
      sw1 | sw2 | sw3 | sw4 | sw5 | sw6 | sw7 | sw8 |
          |     |     |     |     |     |     |     |
row2>-----+-----+-----+-----+-----+-----+-----+-----+
      sw9 | ... |     |     |     |     |     |     |
          |     |     |     |     |     |     |     |
row3>-----+-----+-----+-----+-----+-----+-----+-----+
          |     |     |     |     |     |     |     |
          |     |     |     |     |     |     |     |
row4>-----+-----+-----+-----+-----+-----+-----+-----+
          |     |     |     |     |     |     |     |
          |     |     |     |     |     |     |     |
row5>-----+-----+-----+-----+-----+-----+-----+-----+
          |     |     |     |     |     |     |     |
          |     |     |     |     |     |     |     |
row6>-----+-----+-----+-----+-----+-----+-----+-----+
          |     |     |     |     |     |     |     |
          |     |     |     |     |     |     |     |
row7>-----+-----+-----+-----+-----+-----+-----+-----+
          |     |     |     |     |     |     |     |
          |     |     |     |     |     |     |     |
row8>-----+-----+-----+-----+-----+-----+-----+-----+
     sw57 |sw58 |sw59 |sw60 |sw61 |sw62 |sw63 |sw64 |
          |     |     |     |     |     |     |     |
          |     |     |     |     |     |     |     |
ret1<-----'     |     |     |     |     |     |     |
ret2<-----------'     |     |     |     |     |     |
ret3<-----------------'     |     |     |     |     |
ret4<-----------------------'     |     |     |     |
ret5<-----------------------------'     |     |     |
ret6<-----------------------------------'     |     |
ret7<-----------------------------------------'     |
ret8<-----------------------------------------------'

(just for the tech's - the switch numbers are 'physical' not 'logical'   
(11-88))

Each area identified SW'nn' - would look like this;

         

                              col'n'              col'n'+1
                                |                   |
                                |                   |
row'n'   >----+-----------------|-+-----------------|-+---
              |                 | |                 | |
              |   sw1           | |   sw2           | |
              |    __           | |    __           | |    _
              '---O  O---[>|--. | '---O  O---[>|--. | '---O
                               '+                  '+
                        diode   |           diode   |   

                                |                   |
row'n'+1 >----+-----------------|-------------------|---
              |                 |                   |
              |                 |                   |
              |    __           |                   |
              '---O             |                   |
                                |                   |
                            (return'n')      (return 'n'+1)

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 matrix   
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 - 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' for example).

Also, only 16 different coloured wires are required, again - 8 for the   
rows (or 'sends' or 'strobes') and 8 for the columns (or returns). 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   
uP to read (indicating a closed switch).

*Interfacing the Matrix with the Microprocessor*

The switch matrix cannot be directly connected to the uP - it doesn't   
have the power to 'drive' the matrix (and if the matrix shorted - you'd   
probably be picking the CPU out of the back of your head), so, we have to   
provide a suitable interface between the uP and the matrix - 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 ports which can have any of the 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 called 'port a' and the other 'port b'. The uP can write to   
the output port which inturn will drive a higher voltage through the rows   
('sends' or 'strobes') which will return into the input port of the PIA   
which our uP can safely read 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 chip (you'd be 'loading' the   
output), so it would be good pratice to protect the output pins using   
drivers as it also adds more power to push through the matrix too which   
gives us a 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 (port 'b' only - port 'a' does not have   
enough output current), 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're 'stepping' or 'shifting' up to higher   
voltage/current.

Similarily our PIA cannot read 12 volts or there abouts (don't try this   
at home, otherwise you may be picking another chip out of the back of   
your head!)- so the input has to be shifted back down again to a safe   
level (5 volts or less). You could use zener diodes or comparators (as   
with the WPC).

So when the uP 'writes' data to the output port of the PIA it is   
effectively 'driving' via proxy the switch matrix;

 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 uP 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 uP   
would then initiate the debounce time, after which it would then check   
the input port register again to see if it was still closed.

 ret8 ret7 ret6 ret5 ret4 ret3 ret2 ret1  - ('returns')

|bit7|bit6|bit5|bit4|bit3|bit2|bit1|bit0| - the PIA input port (byte)

Finally (if it is closed) the uP 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.

*Scanning Method*

The uP via the PIA + driver electronics sends a voltage down row1 - 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 row1 off and row2   
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 checked.

The uP has to obviously perform this scanning routine a 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 uP.

Have fun.

Clive
<[email-masked]>