The majority of today made keyboards is using the same communication port for communication with the computer. This port is called the PS2 port (also known as a "mouse port"). Some keyboards (mice or pointing devices) are using the USB connection, but it is not as widely used as the PS2 port. The PS2 port was developed by IBM to interface keyboards and pointing devices such as mice, trackballs etc. Before the PS2 port was put in usage, another port was used, this port is almost completely (electrically) similar to the PS2. This former port (named AT/XT) used 5-pin DIN connector.
The PS2 port uses 6-pin mini-DIN connector:
Because of electrical similarity between 5-pin DIN connector and 6-pin mini-DIN connector, these two connectors can be easily changed with simple hard-wired adaptor.
Signals CLK and DATA serves for bidirectional synchronous serial communication between the computer and keyboard or mouse. The buses of both signals (DATA and CLK) in the computer and also in the device (keyboard) are containing a resistor (10k?) connected to +5V and an open-collector transistor on the output. This design protects it from reciprocal damage of outputs by collision during communication.
Signal RESET is used to initialize the connected device (keyboard). This operation is being run right after the computer is turned on. The keyboard is supplied by a voltage of +5V, connected to pin number 4 (according to the picture of the 6-pin mini-DIN connector). Ground for supply and signals is on the pin number 3. All levels of signals are compatible with level of CMOS circuits
In further text more general term host will be used to refer to computer (or whatever the keyboard/mouse is connected to) and term device will be used for keyboard (or mouse). The basic idea is, that the device is always generating the clock signal, but the host has always ultimate control over the communication on the PS2 port.
If the host wants to send data, it must first inhibit communication from the device to the host by pulling Clock low. In order to restore the communication from the device to the host, the host must pull Data low and releases Clock. This is called the "Request-to-Send" state and it signals the device to start generating clock pulses.
There exists three basics states of bus:
- Data = high, Clock = high: Available for use
- Data = high, Clock = low: Communication Inhibited
- Data = low, Clock = high: Host Request-to-Send
Information which is transmitted is of size one byte. Each information byte is sent in a frame consisting of 11 bits. These bits are:
- 1 start bit
- 8 information bits (LSB is sent as first)
- 1 parity bit (odd parity)
- 1 stop bit
The frequency of clock signal is usually 10 kHz (it can be up to 16.7 kHz). Data sent from the device to the host is read on the falling edge of the clock signal. Data sent from the host to the device is read on the rising edge. The minimal space between two different followed codes is 1,2 ms.
If the device (keyboard or mouse) wants to send information, it first checks the clock signal to check if it's at a high logic level. In case, that the clock signal is in low, it means, that the host is inhibiting the communication and therefore the device must wait until the host releases the clock signal. The clock signal must be continuously in high level for at least 50 microseconds before the device can start transmitting the data.
The device writes a bit on the Data line when clock signal is high, and it is read by the host when clock signal is low. The host can inhibit the transmission before the 11th clock pulse. If the transmission is stopped, the device must abort current transmission and prepare the data to be retransmit when the host releases the clock signal. Most keyboards have for this purpose 16-byte buffer.Mice store only the most current movement packet for transmission. In following is described typical sequence of events when the host is receiving the data from device:
1. The device checks the clock signal. If the line is low, transmission from the device is inhibited
2. The device checks the data line. If the line is low, the controller receives data from the host.
3. The device checks the clock signal during the transmission at intervals not exceeding 100 microseconds. If the device finds the host holding the clock signal low, the transmission is terminated. The host can terminate transmission anytime during the first 10 clock cycles.
4. A final check for terminated transmission is performed at least 5 microseconds after the 10th clock.
5. The host can hold the clock signal low to inhibit the next transmission.
6. The host can set the data line low if it has a byte to transmit to the device. The data line is set low when the start bit (always 0) is placed on the data line.
7. The host raises the clock signal to allow the next transmission
The packet is sent a little differently in host-to-device communication... First of all, the PS/2 device always generates the clock signal. If the host wants to send data, it must first put the Clock and Data lines in a "Request-to-send" state as follows:
- Inhibit communication by pulling Clock low for at least 100 microseconds.
- Apply "Request-to-send" by pulling Data low, then release Clock.
The device should check for this state at intervals not to exceed 10 milliseconds. When the device detects this state, it will begin generating Clock signals and clock in eight data bits and one stop bit.
The host changes the Data line only when the Clock line is low, and data is read by the device when Clock is high. This is opposite of what occurs in device-to-host communication. The host may abort transmission at time before the 11th clock pulse by holding the clock signal low for at least 100 microseconds. To make this process a little easier to understand, here's the steps the host must follow to send data to a PS/2 device:
1) Bring the Clock line low for at least 100 microseconds.
2) Bring the Data line low.
3) Release the Clock line.
4) Wait for the device to bring the Clock line low.
5) Set/reset the Data line to send the first data bit
6) Wait for the device to bring Clock high.
7) Wait for the device to bring Clock low.
8) Repeat steps 5-7 for the other seven data bits and the parity bit
9) Release the Data line.
10) Wait for the device to bring Data low.
11) Wait for the device to bring Clock low.
12) Wait for the device to release Data and Clock
Coding of keys of keyboard
From the point of view of coding keys it is possible to split keys into there groups. These groups are :
- basic group
- extended group
- special group
The basic group:
To basic group pertain 83 keys. These keys with their names and scan codes are listed in the table (which is placed further in the text) on positions from 0 up to 99. If some of these keys is pressed, appropriate scan code of this key is sent and after this key is released, the break code ($07E0) is sent followed by scan code of released key. If the key is being pressed for longer period of time, the appropriate scan code is being sent until the key is released.
The sequence of scan codes sent by the keyboard
The scan code in square brackets is being sent while the key is pressed.
The extended group:
The extended group consist of 14 keys. The scan codes and names of the key from extended group are in the table on positions 100-113. These key have the same scan codes as keys on the numerical keyboard, which pertain to basic group, but before these scan codes is put code ($05C0). The purpose of this code is to distinct the scan codes of these keys from the scan codes of keys from basic group.
The sequence of scan codes from extended group sent by the keyboard
The two scan codes in square brackets are being send while the key is pressed
In case that the numerical keyboard is turned on (numerical keys representing the numbers), are together with the scan code from extended group send additional scan codes. These additional scan codes are working in the same way like if the key SHIFT would be pressed in parallel. The code ($05C0) is put in front of the scan code of SHIFT key ($0624). This is so called doubling of the code ($0624). This new code is named ESH code. The ESH code is listed in the table of codes together with codes from extended group and is placed on position number 114.
The special group:
The special group of keys includes all the rest of the keys on the keyboard. This group contains of five keys: PAUSE, PRINT SCREEN, CAPS LOCK, SCROLL LOCK and NUM LOCK. The first two keys (PAUSE and PRINT SCREEN) are sending different combinations of scan codes according to another key, that is pressed in parallel.
The other three keys (, CAPS LOCK, SCROLL LOCK and NUM LOCK.) cause the change of status of appropriate LED indicator. In this case the communication is being run in both directions.
If two or more keys are pressed in parallel (key from basic and extended group), the scan codes of according keys are send in the same order as they where pressed and these scan codes do not influence each other. The only exception is, if the cyclic sending of scan code is interrupted. This happens when one key is pressed for a long period of time and then is pressed some other key. In this case the keyboard stops sending scan code of key that was pressed before
On the following picture is a keyboard with names of scan codes. Those key, that are doubled have prefix N (for numerical keyboard) and prefix R (for the rest of the keyboard). Each key has its own combination of scan codes, which can not be changed.
The process of sending the scan code ($0438)hex which belong to key representing letter 'A'
In table below are listed all scan codes. The first column indicates the position, the second indicates the scan code and the last one shows the name of the code, which is derived from the meaning of the key. All codes, that are sent by the keyboard have 0.bit in low logical level and 10. in high, therefore the information consists always out of 9 bits. Altogether there is only 93 scan codes. The scan codes from basic group are on positions 0-99 and codes from extended group on positions 100-113.
On 114th position is placed the ESH code (already mentioned before) which is being put together with codes from extended group. The codes, that are sent when the PAUSE and PRINT SCREEN keys are pressed are on positions 97,115 and 116. The break code is on position 0. The codes used for controlling the LED are on positions 40 and 41, but these codes are send by the computer.