Keyboard Interfacing

Attaching and using a keyboard with the Altera UP1 board is actually quite simple. The following explains the basic operation of an AT style keyboard (the current standard for PC keyboards) as it sends data. This app note will not discuss the sending the keyboard commands, For more information on keyboards please see the excellent online resource, and the resource used in the making of this note, Beyond Logic Organization.

Keyboard Theory

The keyboard is actually a small computer of sorts. It is capable of sending and receiving commands, buffering data, and maintaining desired settings. This app note will only deal with the keyboard sending data.

Each key on a keyboard is assigned a scan code. This scan code, a two digit hexadecimal number for most keys, is transmitted by the keyboard when ever the key is pressed. If the key is held down for longer than it's typematic delay the code will be sent again and again. When the key is released the keyboard sends, F0 in hex, and then the scan code of the key that is released. This works for any and all keys on the keyboard.

An interesting note is about the "caps lock", "num lock" and "scroll lock" keys. These keys have an LED, on most keyboards, that is toggled on and off as the key is pressed. This LED is in fact controlled by the device controller the keyboard, your computer in most cases. When you press the "caps lock" key, for example, the keyboard tells you computer that the key has been pressed and the computer tells the keyboard to turn on or off, as the case my be, the corresponding LED.

Scan Codes and PS/2 Connection

The following are a diagrams of a keyboard with the scan code for each key written below the key name. This diagram is from the Beyond Logic Organization.

Below is a diagram of a PS/2 port:

1. Clock

2. GND

3. Data

4. NC

5. VCC (+5V)

6. NC

Notice that there is a clock and a data pin/line for the PS/2 connector. This is explained in the next Section

Data From the Keyboard

The keyboard is responsible for generating the clock signal. It is normally high, but when the keyboard wants to send data it creates a clock signal and sends it's data on the falling edge of the clock. This makes it very simple to read data from a keyboard. All you have to do is wait until you get a falling edge clock signals and then read the data on the data line. The following diagram from beyondlogic.org, shows the data

Notice that there are eight data bits, the least significant bit is sent first, and a parity bit. The parity is odd. The frequency of the keyboard clock is generally in the range of 20 to 30 KHz.

Using a Keyboard With the UP1 Board

Using a keyboard with the UP1 board can be very helpful. First is provides a large number of buttons, unlike the four push buttons included on the board, and second the buttons are debounced. In order to read data from the keyboard this code can be used. Note this code will get the raw data from the keyboard. I.E. each time a key is pressed three different scan codes are sent. This code will output the first scan code (two digit hex number) after a key has been released.

I hope this App note proves to be useful. If you have questions you can e-mail me at: ckowalski@iname.com. I will try to respond if I can.

Christopher Kowalski

E-Field Probe group. (CDU)