

The MCU can then process the information and act accordingly. In this mode, the keyboard can supply the needed serial data and clock signals. Many MCUs include a serial-peripheral interface (SPI) port that can operate as a slave device and receive serial data. You might think a UART could handle this serial information, but keyboard clock frequencies can vary between 10 kHz and about 16 kHz, so a UART set for a fixed bit rate might not work in all cases ( Note 1). A PS-2 keyboard transmits data and clock signals when I press the W key, which has the assigned code 0x1D (MSB first).

The least-significant bit (LSB) of a key code gets shifted out first.įIGURE 1. A PS-2 keyboard does, however, transmit key codes in a UART-type format: a start bit eight data bits an odd-parity bit and a stop bit ( Figure 1). These codes don’t directly translate into ASCII values 0x57 and 0x58, respectively. So, for example, the W key produces 0x1D and the X key produces 0x22 (all values given in hexadecimal). The PS-2 keyboard engineers assigned a unique code to each key.
#Ascii key codes how to
I'll show you how to use a system-on-a-chip and a bit of software to convert PS-2 key codes to corresponding ASCII values. To further complicate things, these keyboards have their own code-transmission protocol. You can buy inexpensive PS-2 keyboards, but they don't produce ASCII values many applications need.
