Jump to page content

Matrix scan encoding

Contents

Overview

Matrix scan is an encoding technique where all the switches in the keyboard are wired into a grid, or matrix. Each keyswitch sits at a grid intersection and can be addressed by a grid co-ordinate pair, (row, column). Encoder circuitry in the keyboard examines each key in turn to check whether it is active. The diagram below shows part of a keyboard matrix with one key active, showing how each switch can be separately addressed:

Matrix scan has been the standard keyboard wiring and encoding arrangement for decades; it solves a number of problems with earlier keyboard implementations by allowing the sensing and encoding electronics to be centralised (factored) from the switches themselves. Circuitry previously repeated key-by-key only needs to exist once, within the encoder circuitry, which simplifies keyboard implementations and reduces production costs. Matrix scan makes reliable mechanical switching viable by allowing debounce logic to be applied to any key; the simplest approach to debounce is to pause the scanning process to allow time for the switch contacts to settle. Matrix scan also greatly simplifies capacitive keyboards by centralising the capacitive sensing and thus reducing each key to a simple variable capacitor.

Further, as every key has a unique coordinate pair within the matrix, it can be addressed separately, allowing for limitless keys to be held simultaneously without collisions.

The matrix can be formed from individual wires or printed circuit traces. In most keyboards, the matrix is formed from either a rigid printed circuit board (PCB), a flexible printed circuit (FPC, wiring printed onto a membrane sheet) or a combination of the two. Individual wires connecting the switches are commonly used by enthusiasts, but this approach is also found in some commercial products, in particular those from Maltron. Example circuits are shown below:

Part of the Cherry G84-4400 matrix; the integrated jumpers in ML switches allow the routing to be very clean, giving a fairly regular track layour
Part of the NTC KB-6251EA matrix, with a much more complex track structure
Part of the Silitek-made Dell KB216 matrix: the matrix arrangement is also used with rubber dome over membrane keyboards

Matrix scanning is suitable for virtually all non-encoding switch types, including conductive (metal contact, elastic contact, reed, membrane), photoelectric, capacitive, Hall effect and inductive. Keyboards using FPCs can also use the membrane sheets as switch contacts.

History

The earliest known occurrences of matrix scan keyboards are from 1970:

The Cherry and Clare-Pendar types were both described in the In key with computers side panel of the article Capacitive keys, simpler circuits add up to reliable keyboard, authored by Leon M. Magill in Electronics magazine in December 1970 (vol. 43 no. 25, 07/12/1970). Describing the Clare-Pendar approach, Leon Magill wrote:

Another keyboard that features a single-chip LSI/MOS encoding circuit is made by Clare-Pendar of Post Falls, Idaho. Here, a scanning technique searches for a closure of a dry reed switch key. Clock circuitry contained in the chip operates at 50 kilohertz, permitting a scan rate of 20 microseconds. A 2,000 bit read-only memory, also in the chip, generates an output code that is compatible with DTL, TTL and MOS circuitry.

The Cherry approach is described as though it is entirely different, but to some extent it is merely a TTL equivalent of the circuitry encompassed by the single chip of the Clare-Pendar design. The most significant difference is that the matrix co-ordinates provide the output codes directly, rather than using a look-up table in ROM. The article states:

A different encoding approach is taken by Cherry Electrical Products of Waukegan, Ill. Its scheme includes an eight-stage clocked, ripple-through counter; two 16-channel multiplexers; a 1-of-16 decoder; a monostable multivibrator; and assorted gates. The 1-megahertz clock scans all possible bit combinations every 256 µs. When a coded signal corresponding to a depressed dry reed switch key is sensed, the clock is inhibited, stopping the counter at a particular eight-bit code. The four most significant bits are fed to the multiplexer, where a strobe activates the circuit, while the four least significant bits go into the 1-of-16 decoder. A matrix connection between the multiplexer and the 1-of-16 decoder is established and a one-shot is triggered generating the encoded signal to the logic.

Adoption of matrix scan was not a given at this time; Micro Switch continued using their two-of-N static encoding, and the Bendix keyboard produced for NASA around 1974 also used two-of-N static encoding. Photoelectric encoder keyboards would still remain in production for some time to come. Some manufacturers continued to introduce exotic techniques following the turn of the 1970s, but these ideas did not last.

In modern keyboards, the matrix positions do not provide the ASCII codes of the keys, but the co-ordinates can be used to create internal identities that can be converted to ASCII codes or standard scancodes using a look-up table in ROM.

Rollover

The matrix arrangement has the significant advantage of allowing two or more keys to be held simultaneously. Because each key position can be examined independently, the state of each key is not inherently affected by the state of any other key. This allows reliable interleaved press and release detection, from two or more overlapping keystrokes, all the way up to an unlimited number of keys held down: rapid typing and holding keys concurrenly does not inherently jam the key detection process.

The limit on the number of keys that can be held without interfering with the scanning process does depend on the type of keyboard in question. Electromagnetic sensing, including ferrite core and capacitive keyboards, provides isolated key examination by nature. Conductive keyboards (such as mechanical keyboards) require a diode at each matrix position to prevent wrong-direction current flow from causing “ghost” or “phantom” keypresses to be detected where no key was actually active; without these diodes present, the number of keys that can be detected concurrently depends on which keys are involved, but a minimum of two can be guaranteed. Unlimited concurrent key detection is the current definition of N-key rollover. Although N-key rollover on a conductive keyboard does still require diodes, only one diode is required per switch, and for ease of assembly these diodes were sometimes fitted into switch in the factory (as is, or was, the case with RAFI, Cherry, Siemens, Clare and others). In most cases, membrane keyboards cannot provide unlimited concurrent keys, as there is no way to solder diodes onto plastic sheets; workarounds do exist, but little is known about most of them. However, N-key rollover is not a necessity and keyboards function fine with a limit of two concurrent non-modifier keys. (Modifier keys are wired specially to avoid problems.)

The following illustration shows a portion of a switch matrix wired with mechanical contact switches, with diodes fitted to ensure N-key rollover:

In the diagram above, you will see that the matrix layout does not correspond to the key layout on the keyboard. In most cases, the circuit routing is chosen for ease and convenience, since the look-up table in the encoder takes care of the mapping between key position and output code. Some pure TTL keyboards do require the row and column numbers to collectively form the output code of the key.

Implementation

From the inception of matrix scanning around 1970, both discrete logic (DTL and TTL) and large-scale integration (LSI) encoders were used. DTL and TTL encoders use the key positions within the matrix to define the output codes, necessitating a redesign of the PCB to change the identity of keys. A DTL/TTL encoder would use around 12–16 discrete chips. The alternative method was the single-chip encoder, implemented as LSI in MOS circuitry; single-chip encoding arrived around the same time as matrix scan, around 1969. LSI (MOS) encoders provide a look-up table in ROM (or externally in PROM/EPROM) to abstract the output codes of the keys from the matrix positions. For example, changing the first key on the number row from QWERTY (1/!) to AZERTY (&/1) would require a circuit redesign in both the matrix wiring and shift logic in a DTL/TTL keyboard, while in an LSI-encoded keyboard only the look-up table entry would need to be changed. By the late 1970s it became practical to implement encoders as microcontroller firmware.

TTL

Some keyboards provided the matrix encoding entirely in transistor–transistor logic (TTL), without a dedicated encoder or controller chip. The output code from each key was derived from the key’s matrix co-ordinates. A binary counter IC addresses a multiplexer IC in order to iterate over the matrix rows, and a decoder IC or similar circuit detects the active columns. Where two or more keys are pressed at once, a single key can be filtered using a priority encoder IC, a portion of the counter value (or a second counter), or equivalent logic in discrete TTL chips. The value of the counter plus the value of the active column are combined to form a single binary number representing the output code, that is then presented to the host. The output can be parallel, or fed into a shift register for serial transmission.

TTL-based encoding has the advantage of using entirely off-the-shelf components to form the encoder. This was the basis of Cherry’s keyboard designs described in New Cherry electronic data entry keyboards from 1971 (document scanned by Bitsavers). Combined with simple mechanical switches, this likely allowed Cherry to produce an affordable but reliable product. Cherry’s block diagram in their brochure was depicted as follows:

Cherry’s “PRO” keyboard (B70-05AB) utilised fourteen to sixteen integrated circuits, the part numbers of which are given in the documentation:

Part number Function Quantity
7400/MC3000 Quad 2-input NAND gate 1
MC846 Quad 2-input NAND gate 1
7402 Quad 2-input NOR gate 1
7408 Quad 2-input AND gate 3
7474 Dual positive-edge-triggered D-type flip-flops 2
7486 Quad 2-input XOR gate 1
7493 4-bit binary counter 2
74123 Dual retriggerable monostable multivibrator 1
74150 16-line to 1-line data selector/multiplexer 1
74154 4-line to 16-line decoder/demultiplexer, inverting outputs 1
74123 Dual retriggerable monostable multivibrator (optional) 1
74180 9-bit odd/even parity generator/checker (optional) 1

A second 74123 multivibrator is one of the optional components that together provide auto-repeat using the repeat key; in addition to the 74123, three extra resistors and capacitors are required, as well as PCB alterations (cutting of a jumper and connecting two locations in the circuit). The 74180 parity generator is the other optional chip, which when installed provides a parity bit in the output; this feature, too, requires alterations to the PCB. Various other changes are possible in the “PRO” keyboard with circuit alterations or changes to resistor and capacitor values, such as timing alterations and changes to the key assignments. The “PRO” keyboard was designed with considerable flexibility, but only by way of altering the circuitry.

The term “bit-shift logic” in the diagram does not refer to the use of a shift register to convert between parallel and serial data (and no such chip exists in the B70-05AB parts list). Rather, it refers to the process of transforming ASCII codes when the control or shift key is held, which is achieved by bit manipulation.

TTL-based encoding also has some significant disadvantages. Not least, the output is controlled by the matrix, and switching output from ASCII to EBCDIC or any other code set requires a whole new matrix. Changing from QWERTY to AZERTY requires not only a change in the PCB layout (so that the tracks to Q and A and to W and Z are exchanged, to swap their matrix co-ordinates and thus their output codes), but the shifted numerals behaviour requires different logic for the shift key. The unhelpful “QWERTY” ordering of keys on a keyboard leads to an unnecessarily complicated matrix. Such keyboards also require a considerable amount of chips: 15 in the example in Cherry’s 1971 brochure, and their PRO keyboard has been found with 14 and provision for 16 in total. Adding features such as serial output require circuit changes. Moving as much of the encoding and output processing into a single chip would go on to greatly simplify keyboard design and production.

Some example chip choices include:

Manufacturer Matrix access Timer Device
Acorn 74251 8-line to 1-line data selector/multiplexer
7445 BCD to decimal decoder/driver
74163 synchronous presettable 4-bit binary counter BBC Microcomputer (1981)
Cherry 74150 16-line to 1-line data selector/multiplexer (select)
74154 4-line to 16-line decoder/demultiplexer (detect)
7493 4-bit binary counter × 2 B70-05AB “PRO”
Datanetics SN74151N 8-line to 1-line data selector/multiplexer
MC8311P one-of-sixteen decoder
MC7493P × 2 Model 01-02-638 (1970)

RS 277-117

Radio Shack produced a TTL dual-mode ASCII keyboard kit for hobbyists. Part 277-117 comprised the PCB, one of the eighteen total ICs required (IC position Z1, RS74H103) and the documentation, with the remainder of the parts to be purchased separately. The documentation for this project provides good insight into how such keyboards were made, although it is quite a simplistic and limited implementation; available for download at Bryan’s Old Computers under Digital Group documentation, specifically the section DG Systems, the documentation includes the assembly instructions, complete theory of operation, output codes, circuit diagrams and troubleshooting. The kit was intended to be paired with part 275-1422, which appears to be a Hi-Tek High Profile array.

The age of the product is not known. The only known dated reference is in the Radio Shack 1977 catalogue, and the switch assemblies are likely to be surplus parts.

The output is split dual-mode. With Shift released, the alphanumeric keys output numbers and uppercase letters. With Shift held or Shift Lock engaged, the number keys produce symbols, but the letter keys produce control characters as though Control was held (the separate Control key has no influence on character output). The Shift keys themselves do not generate output of their own. Shift lock is electronic secretarial: press Shift Lock to engage, and either Shift key to release. The shift lock state is held in a flip-flop. The Control key does not affect the ASCII codes and must be detected specially; the same applies to the Break, Clear, Here Is keys and the two unmarked keys. These six keys produce bit patterns 1000 0000 to 1000 0101, with the eighth bit (called “E”) indicating that a special key was pressed; this is instead of giving the keys dedicated control lines on the edge connector. Two approaches are offered for dealing with these special keys.

The complete assembly required six capacitors, eleven resistors and eighteen ICs; the complete list of ICs is as follows:

Part number Function Quantity
RS74H103 Dual, edge-triggered J-K flip-flop 1
RS7473 Dual, J-K flip-flop 2
RS74193 Synchronous 4-bit up/down counter 2
RS7413 Dual, 4-input Schmitt trigger 1
RS74154 16-line demultiplexer 1
RS7475 4-bit bistable latch 2
RS7404 Hex inverter 2
RS7402 Quad, 2-input NOR gate 1
RS7400 Quad, 2-input NAND gate 2
RS7410 Triple, 3-input NAND gate 2
RS7420 Dual, 4-input NAND gate 2

Both positive and negative logic output is offered, on alternative lines on the edge connector (a complete set of lines for each of positive and negative logic, with the customer expected to determine which is required and use the correct set of lines). A Repeat key is provided to enable auto-repeat.

No explicit debouncing seems to be performed. The product documentation states:

The keyboard scan principle, which has been used on all recent calculators, has the advantage of minimized effects of key bounce. (Key bounce is that tendency of a key to chatter, or double-entry, when pressed or when released.)

It’s interesting to note that this design requires more ICs than Cherry’s “PRO” keyboard, yet offers fewer features.

MOS/LSI

Various manufacturers produced off-the-shelf large-scale integration (LSI) matrix-based encoder integrated circuits. These are nearly complete keyboard encoder circuits contained in a single chip. Proprietary static encoders existed as early as 1969 at Micro Switch, while off-the-shelf scanning encoders were on the market as early as 1971, the earliest confirmed date for General Instrument’s AY-5-2376. Before long, numerous semiconductor manufacturers put out their own single-chip encoders, both static and scanning.

Certain aspects of the process such as controlling scan and debounce timing may need to be provided using additional components such as capacitors. These encoder chips often included a few thousand bits of ROM capacity for all the output codes for the keys; separate output codes would be defined for each of the supported modes. Alternatively, the chip could be designed to provide look-up offsets into an external ROM containing the character codes. The use of an external ROM (typically a PROM or EPROM) allowed the keyboard manufacturer to cheaply and conveniently customise the keyboard output without the expense of full mask programming of the encoder chip ROM.

Many characteristics of the encoding and output process are intrinsic to the encoder IC, and are impossible to change. These include the matrix dimensions, the rollover or lockout behaviour, the number of modes supported and the behaviour of the modes, the means by which modes are selected, and the number of bits stored per character. Many encoders connected directly to the matrix, with dedicated pins for each row and column. This reduced the overall cost by removing the need for external multiplexers or decoders, but disqualified those models if a higher key count was needed. National Semiconductor implemented a compromise with the MM57499: the physical wiring is for a 12×8 matrix (utilising 20 pins on the IC), allowing up to 96 keys, but by grounding the fifth strobe line, strobe lines 0–3 instead address a multiplexer to extend the matrix to 12×12 for a total of 144 keys.

The mode implementation also varied. National Semiconductor’s MM5740 was a quad-mode encoder with 9-bit output, but bits 0–4 and 8 of each key were common to all modes, thus saving on ROM size and chip complexity and cost. Typically, though, each key could generate three or four fully-independent codes, one for each of the available modes. These codes could be set in the factory (typically to ASCII), or the customer could provide the manufacturer with the desired matrix map and have custom encoder chips fabricated to suit their equipment. As noted, it was not uncommon to provide the output tables in a separate chip and use an encoder with no internal tables.

The rollover and lockout behaviour was also encoder-specific. N-key rollover was widely implemented. A common alternative was N-key lockout: as soon as the first key in the matrix is detected, the scanning process stops until that key is released. National Semiconductor’s MM5740 offered two-key rollover or N-key rollover, but this had to be set with the mask programming. General Instrument’s AY-5-3600 and SMC’s corresponding KR3600 as well as their KR9600 and KR9601 all permit a choice of rollover or lockout using the option pins. General Instrument’s AY-3-4592 has a dedicated lockout/rollover pin. The literature for encoders indicates that, in N-key rollover operation, matrix scanning is suspended upon key detection, until the debounce time has completed for that key. That is, if multiple keys are pressed, they are not all debounced simultaneously, as that would introduce unnecessary complexity. Rather, the first key to be detected is debounced and output, then matrix scanning resumes. An area of RAM tracks the keys that were detected, so that they are not reported again on the next matrix pass. This way, keys are reported as they are struck, but holding them will not interfere with future keystrokes. Conductive keyboards require a diode at each position for this; electromagnetically-sensed keyboards (which some encoders such as General Instrument’s AY-3-4592 support) do not.

Encoders were typically designed with parallel output, but certain models provided serial output instead.

See the keyboard encoder ICs page for details of some of the encoder models that were on the market.

A keyboard encoder would not necessarily provide the complete functionality of a keyboard, and additional functionality—beyond that provided by the encoder—had to be provided with additional logic circuitry. A good example of this is Jameco’s JE610 kit: Jameco desired Repeat and Caps Lock functionality, and neither facility is included in the AY-5-2376 encoder that it uses. As a result, additional logic chips are required for these features.

Microcontroller

As the cost of integrated circuits fell, it finally became cost-effective to use a microcontroller as the keyboard encoder. On visual inspection, keyboards using a controller appear the same as those with an encoder (with both types having one or two large chips depending on whether the ROM is stored separately or integrated into the controller or encoder); some keyboards described as having a “controller” instead have an encoder logic chip, as described above. Sometimes the chip type can be determined readily from the part number (e.g. a model number from Intel’s MCS-48 series or National Semiconductor COPS family) but other times the identity of the chip is not readily apparent.

Micro Switch claimed in October 1977, in an advertisment in Computer Design, to have introduced the world’s first “intelligent keyboard”, based on a microcontroller:

Designing a full-function keyboard for your system used to have a major problem. A full-function price.

MICRO SWITCH has changed all that with the intelligent keyboard. It’s the first microcomputer-based keyboard. Ever.

Which means when you equip it with virtually every function you can think of, it still comes in for the price of a simple encoded keyboard. Because all the functions that used to require extra components are now available on a single microcomputer chip.

Besides traditional encoder functions, the chip can handle many others, such as parallel or serial data, multi-character storage and tri-stated outputs for direct data bus compatibility without using expensive I/O ports.

The intelligent keyboard can perform more functions more efficiently because less hardware is needed. You get lower total system cost. Pin for pin compatible EPROM for faster design turnaround. Plus greater reliability since there are fewer components.

More details were provided in the December issue:

Combining 103 Hall-effect logic scan key modules with a single-chip microcomputer results in a full-function keyboard for intelligent and distributed processing terminal applications. The microcomputer integrates 8-bit CPU, ROM, RAM, I/O lines, and a time/event counter on a single chip. A 40-pin EPROM, pin-compatible with the ROM, is available for prototyping. Features of the 19.38 x 16.34 x 1.44″ (49.23 x 16.1 x 3.66-cm) model 103SD24-1 keyboard include 4-mode, 8-bit ASCII code assignment; choice of serial or parallel data outputs; 14 relegendable keytops for programming keys; 8-deep FIFO character storage; N-key rollover; and timed/auto repeat for selected keys.

This technique, of writing the encoder as a software application for a microcontroller, was adopted wholesale across the industry and remains the means by which keyboards today are built. Compared to dedicated encoder chips, there is considerably more flexibility available: altering the behaviour of the keyboard is achieved simply by changing the firmware mask, and this permits signficant changes in logic to be achieved without having to redesign the entire encoder.

With their IKB-1 Intelligent Keyboard, also introduced in 1977 or 1978, IMSAI opted for an Intel 8035 microcontroller with a separate 4751 ROM chip for the program. The IKB-1 took advantage of having keyboard firmware to allow the keyboard’s output to be reconfigured on the fly, primarily from the keyboard itself, but in certain cases also from the host computer. This included the ability to remap keys, although like all the other settings this was lost when the keyboard lost power.

C. P. Clare would also advertise intelligent keyboards in 1978, in both reed and capacitive forms. The type of microcontroller used is not presently known.

In the years that followed, a number of microcontroller families were used as keyboard controllers. Intel’s MCS-48 family was a popular choice, with models 8039, 8048, 8049 being found in keyboards. Examples include Kaypro 2000 (8748), Apple Desktop Bus Keyboard (8048), Epson Q103A, Micronorth MCL-5165 and Ortek MCK-101 (8049), and Cherry KFN3-8358 (8039). The 8039 in KFN3-8358 is ROM-less, and is paired with an AMD 2716 EPROM for the firmware storage. Similarly, Cherry’s UB88-0008 used a National Semiconductor COP402N ROM-less microcontroller paired with a National Semiconductor MM2758Q-A 8 kilobit EPROM. Fujitsu FKB4700 and Zenith 163-73 are examples of keyboards using an 8051 chip from Intel’s MCS-51 family, the successor family to MCS-48. Sony’s OA-S3400 word processor used a Fujitsu MB8841. The Commodore Amiga 1000 keyboard used a MOS Technology 6500/1-based controller; the 6500/1 is a microcontroller derivative of the better-known 6502 CPU.

Microcontroller-based keyboards have become the standard approach that is still used today. Microcontroller-based keyboards often communicate using a dedicated protocol. The “AT” keyboard protocol introduced with the IBM Personal Computer/AT (now the PS/2 keyboard protocol, with a smaller plug) introduced the ability for the computer to control the lock key status. Apple designed the Apple Desktop Bus (ADB), a universal daisy-chained peripheral bus used by their keyboards and mice, with automatic address negotiation provided by the firmware within each device. PS/2 and ADB were in turn superseded by USB. A single keyboard can communicate on multiple protocols. Some USB keyboards can be connected to a PS/2 port using an adapter, and they automatically detect what connection is in use. Some older keyboards had DIP switches to select between protocols such as XT, AT or PS/2, ADB and Sun. Having all the keyboard behaviour contained within firmware makes such designs far simpler and cheaper than logic-based encoding and dedicated communication circuitry.

Memory-mapped I/O

A rather intriguing take on this method was described in the article Microprocessor keyboard encoding in Microprocessors volume 2 number 2 from April 1978. The article was penned by Roger Munt of Terminal Display Systems Limited in Lancashire, England. Rather than use a microcontroller (even a ROM-less microcontroller), Munt’s organisation decided to use a regular microprocessor, specifically a Signetics 2650. The 13 true address lines from the CPU (A0–A12) are fed into an 8T98 octal buffer, an HEF4515 4-to-16 binary decoder and the program and optional substitution PROMs. The eight data lines D0–7 connect in parallel to four hex inverting buffers (each HEF40098) and one or two PROMs. (Although the buffers are listed as HEF40098, which is a hex buffer, the schematic indicates that they are octal buffers.) The binary decoder selects where the data lines are connected: one of the four buffers, or one of the two PROMs. Thus, four of the address bits select which of the following is mapped into the address space:

Thus, the entire keyboard matrix is memory-mapped, using four address lines to enable the relevant buffer (for the desired half of the matrix) combined with eight address lines to isolate a specific matrix row. It does mean of course that the modifier keys and DIP options will span multiple addresses in memory. Debouncing is simply provided by waiting 10 milliseconds for the key to settle; no RAM or shift register is used for this purpose.

The use of a microprocessor means that a separate RAM chip is required, but in this case it appears that the firmware runs entirely from the CPU registers, with no dedicated RAM. An “option board” if fitted would contain RAM, to allow for more advanced functionality.

The article was written in 1978, and the keyboard was designed in late 1976, making it contemporary with the Micro Switch and IMSAI offerings described previously.

Host-based

Host-based arrangements use the host equipment to identify the keys pressed. For example, the host could be a computer where the operating system is partially or wholly responsible for the keyboard operation. Where the keyboard is tied to the host hardware (such as a computer or terminal with an integrated keyboard), this offers a cost saving. The keyboard will need far more wires joining it to the host, but for an internal keyboard this does not inconvenience the user in the way that a thick and heavy external cable would do.

Acorn BBC Microcomputer

Acorn produced a hybrid software–hardware solution for the BBC Microcomputer in 1981. Because the keyboard is integrated into the case, placing a dedicated controller onto the keyboard would be wasteful. However, it would also be wasteful for the 2 MHz 6502 CPU to be continually used to check the keyboard. Additionally, there is a limit on the number of I/O lines available from the I/O controller to which the keyboard circuitry is connected. A compromise was reached where the complex processing is handled by the operating system, but the keyboard itself was responsible for detecting keyboard activity and multiplexing the matrix lines. A total of seventeen lines connect the keyboard to the motherboard, including the matrix row and column numbers (four and three bits respectively), the software-level reset line, the multiplexing clock, and the LEDs. The complete circuit diagram can be found on the BBC/Master Circuit Diagrams at MDFS, reproduced (uncredited) from page 490 of the Advanced User Guide (note that what appears to be “.5V” is really “+5V”). A simplified diagram is shown on the Arduino forum, but it mistakenly omits all the diodes and incorrectly shows all eight columns connected to the NAND gate.

Wong’s–made (“type 1”) BBC Micro keyboard with Futaba ML switches; the diodes can be seen at the bottom right, below the mounting plate
PCB of type 1 keyboard showing the matrix arrangement

The matrix itself comprises ten rows of eight columns. The rows and columns are both multiplexed by chips on the keyboard PCB (the row multiplexer is a BCD-to-decimal decoder). When no keys are pressed, the keyboard uses a binary counter to continually scan the matrix rows, fed by a 1 MHz clock signal from the motherboard. This counter controls the row multiplexer, and the columns are ignored. The matrix scanning is thus independent of the operating system.

Seven of the eight columns connect to an eight-input NAND gate. When a key is pressed, it brings that input to the NAND gate low, causing the NAND gate’s output to go high: the gate output indicates whether any key in the row being scanned is pressed, without being aware of which key, as the column multiplexer is not used at this stage. The NAND gate output is used to signal an interrupt, allowing the operating system to become aware of keyboard activity. From this point onwards, the key-pressed interrupt is shut off, and the operating system scans the matrix directly by taking control of both multiplexer chips, giving it the ability to read each key position on demand.

Each key is assigned an internal scan code; these codes are held in a 70-byte look-up table in the operating system ROM, in 10-byte groups spaced apart at 16-byte intervals, interspersed with 6-byte blocks of data and code to make use of the spare bytes. This look-up table converts from the scan codes to ASCII codes. The scan codes themselves are binary-coded decimal (BCD), where the high nybble is the column number and the low nybble is the row number, taken directly from the values of the two multiplexer chips.

According to Professor Steve Furber, one of the engineers responsible for the design, the operating system handles de-bouncing by checking every millisecond to see that the keys remain held; the 1 ms timer interrupt is used by the operating system for this purpose. Once a fixed number of check cycles have elapsed, the key is deemed to be pressed, and if it remains pressed, auto-repeat is triggered. He also notes that once the last key is released, the operating system re-establishes the key-pressed interrupt and relinquishes the scanning duty back to the keyboard’s on-board logic circuitry. Those who understand 6502 assembly language can determine this from the full MOS 1.2 disassembly at MDFS; do note however that some of the look-up table blocks mistakenly have the least-significant nybble of their address given as &A instead of &B.

Rollover is fixed at two keys, as two addresses in the 6502’s zero page (used for the most frequently-accessed operating system data in Acorn MOS) contain the two keys currently detected, not counting control or shift. If the number of keys held simultaneously increases incrementally, the first two keys will be detected. Pressing a ghosting combination simultaneously may result in a ghost being one of the two reported keys, and games and other software that request specific matrix positions be checked will be subject to ghosting.

The keyboard design contains a couple of other curiosities. The final column of the matrix is isolated from the NAND gate, which instead receives a continual 5 V feed for its eighth input. Of this column, eight rows collectively form a single-byte configuration word in the manner of NVRAM (curiously, this is not done with a single eight-column row). The ability of the keyboard to scan any matrix position on demand allows it to read this configuration word during start-up. The eight bits can be set by wire links or by a DIP switch block, either option being fitted to two rows of eight holes on the PCB. This entire column is diode protected, as otherwise any start-up options set would result in ghosting. The remaining two row positions for this column are Control and Shift, also diode protected, and also isolated from the NAND gate. This indicates that neither Control nor Shift can alert the operating system; pressing for example Control+A would only generate an interrupt as A is struck, after which the operating system’s controlled scan would detect that Control is held. These two keys will also not generate ghosts if they are held in conjuction with two other keys.

The operating system provides a call (OSBYTE 121) to allow user code to scan the matrix for a specific key or for any key above a particular scan code; a second call (OSBYTE 122) scans the matrix to locate any single key pressed above scan code 16, which ignores Shift, Control and the eight start-up options. Steve Furber mentioned that games are able to bypass the operating system and read the keyboard directly, which may improve performance by removing all the interrupts relating to keyboard handling. Note that one key is not present on the matrix at all, specifically the Break key which requests a restart of the operating system. The Break key is wired to its own reset line and functions independently of the rest of the keyboard. (Having a single key to restart the operating system proved troublesome and in the later BBC Master Series machines it could be locked out with a screwdriver.) Nonetheless, the user could still bind an action to that key, as it was a software-level restart rather than a hardware-level reset.

Radio Shack TRS-80 Model I

The earlier TRS-80 computer (retrospectively the Model I) used a simpler arrangement, as detailed in the TRS-80 Technical Manual (1978) on pages 23 (description) and 36–37 (schematic). The keyboard matrix is connected into lines A0–A7 of the CPU’s address bus, and all eight lines (D0–D7) of the CPU’s data bus, forming an 8×8 matrix. When line KYBD is signalled, the keyboard is made accessible via tri-state buffers. With all bits A0–A7 of the address bus set to 1, all matrix rows are activated simultaneously, and the data bus then indicates whether any key is pressed. If there are no keys pressed, the matrix does not need scanning, conserving CPU time. If a 1 appears on the data bus, then the process is repeated one row at a time in order to locate the key. The conversion from matrix position to ASCII code is performed by the computer’s firmware. The KYBD line originates in a 74LS156 decoder/demultiplexer, driven from several lines of the CPU’s address bus. Included in a brief list of problems with the computer on Wikipedia’s TRS-80 entry is “keyboard bounce issues”; it’s not stated whether the switch bounce time exceeded that of the firmware’s debounce period, or whether debouncing was simply omitted by the keyboard scanning routine.

Ladder matrix

The “ladder” matrix is a proposition put forth by Acer in a US patent application filed in November 1989. The details can be seen in European patent 427364 “An encoding circuit for a keyboard”, filed two months later. This design takes advantage of the fact that the data lines on a microcontroller or microprocessor are largely bidirectional. Instead of using each data line as only output (scan) or input (sense), the direction of lines can be reversed as required. The result is a triangular matrix that uses fewer lines for the same number of keys, or more keys for a particular limit on matrix lines. A matrix of this type can be seen in the diagram below:

The example in the diagram uses eleven I/O lines for a total of 55 keys (1 + 2 + 3 + … + 10). Eleven lines would provide at most a 5 × 6 matrix, or 30 keys.

This arrangement was reached as a cost-saving measure; the patent notes that the Intel 8051 (MCS-51 family) microcontroller has more data lines than the 8049 (MCS-48 family), but deems the 8051 to be too costly for use as a keyboard encoder. Using a decoder to expand the number of data lines was also held to be too costly.

The patent does not make any mention of the implications that this design has on rollover. Using the example circuit diagrams in the patent, ghosting is extremely easy to trigger in a manner that cannot be prevented by diodes, but as the same key can be detected in two directions, it may be possible to take advantage of this to ignore at least some ghosts.