The BBC Model B Microcomputer keyboard – How it works
As part of my BBC Micro PC project (more about this later) I need to turn the keyboard in a BBC Micro in to a USB hid device. I could just rip the insides out of a PC USB keyboard and solder wires on to the back off all the key switches but that seems somewhat messy. So I decided to design a micro-controller circuit to read the keyboard and to appear as a standard USB HID device. The neatest solution is to have the micro-controller drivng the keyboard circuitry the same was as the BBC Micro main board does. This post explains how the keyboard circuit works.The BBC Model B keyboard circuit board is more than just a grid of key-switches. It comprises some circuitry to reduce the number of connections between it and the main circuit board to 8 lines and to generate interrupts on key presses. Its a very neat design. Below is the keyboard circuit diagram taken from the BBC Model B service manual. The diagram also includes a bit of circuitry for the speech ROM expansion which I will ignore.
|IC1||74xx161/163||Synchronous 4 bit binary counter|
|IC2||74xx45||BCD to decimal decoder|
|IC3||74xx251||3 state 1 of 8 line data selector|
|IC4||74xx30||Octal NAND gate|
At the core is the traditional key matrix. It is a grid of rows and columns with momentary push switches that connect a row to a column when pressed. All of the rows are tied high by resistors to the 5v supply rail. The rows are inputs to an octal NAND gate (IC4), which, in this case is better thought of as an OR gate with inverting inputs. I.e. but default the output of the NAND gate is low but if one of the rows is pulled low the NAND output goes high. The rows are also inputs in to the line data selector (IC2).
The synchronous 4 bit binary counter (IC1) is driven by the BBC Micro’s 1MHz clock and when the KB EN is high is in free running mode. The 4 bit outputs (outputs QA-QD) are decoded by the BCD to decimal decoder (IC3) such that the decoder’s outputs go low in turn. The outputs of the decoder form the columns of the grid. I.e. column 0 is low on the first clock pulse, column 1 on the second and so on. If a key is pressed the row the key is on will go low when the column goes low. This in turn makes the output of the NAND gate go high indicating a key is pressed. The output of the NAND gate is used to generate an interrupt in the BBC Micro’s CPU.
So normally the keyboard is being scanned by the hardware without any software influence. This is a good thing as it takes a lot of loading off of the BBC Micro’s CPU. When the key press interrupt is triggered the key scanning software takes over. As I said normally the 4 bit binary counter (IC1) is free running however if the LOAD pin (pin 9) is taken low the 4 bit input value (inputs A-D) are latched through to the outputs (QA-QD) and hence control the columns provided by the decoder (IC3). IC1′s load pin is connected to KB EN.
The S line of the data line selector (IC2) is the select line and is also connected to KB EN. The output of IC2 is 3 state meaning it either reflects the appropriate data input line when select is low or is high impedance. The data input lines are the row lines of the keyboard matrix. The input line is chosen using the three bit ‘address’ value on A-C. The row state is output via pin W.
So the keyboard is normally hardware scanning. If the user presses a key, the interrupt is triggered. The CPU enables the keyboard by setting KB EN low and then manually scans the key board by setting the columns in turn via IC1 and checking the state of the rows in turn via IC2. Once the scan is complete KB EN is set high and the hardware scanning continues.
There are three keys on the keyboard which are exceptions. The two SHIFT and CTRL keys are on a row that does not trigger an interrupt. This means that shift is only scanned manually or when another key is pressed. The BREAK key (labled RST) is completely separate from the matrix and triggers the CPU’s reset line. On boot the CPU scans the keyboard manually to see if any of the modifiers are held down to do a cold reset or mount the disk for example.
The BBC Micro keyboard can be easily interfaced with say an ATMEL ATMega328 (like in an Arduino). The micro-controller can drive a pin directly from one of the timers generating a square wave output to scan the keyboard. The interrupt can be connected to one of interrupt inputs and the rest connected to general purpose IO pins set as outputs or inputs as appropriate. Dick Streefland’s USBtiny code provides the functionality to support the USB protocol in software utilising three IO pins on the micro-controller and a few discrete components. All that is required by me is to write the keyboard scanning code and provide the missing bits of the HID device profile.
Tags: BBC Micro PC