- Interface AVR to standard PC AT keyboard;
- Only two I/O lines were used. One line is also connected to the external interrupt pin of AVR;
- No external components are needed for the interface;
- Included C source reads from keyboard interface and converts to serial
In many situations, you need some human interface for your microcontroller project. In this example is interfacing AVR microcontroller to standard PC AT keyboard described.
Physically interface looks as in picture bellow:
In a keyboard interface, signal lines are an open collector with pull-up resistors.
Keyboard cable connectors can be DIN or Mini-DIN (We are not talking about USB interface):
According to the keyboard timing diagram below picture the keyboard transfers data to the host AVR microcontroller. The protocol is one start bit (always 0), eight data bits, one odd parity bit, and a one-stop bit (always 1). The data is validated during the low period of the clock pulse. The keyboard generates the clock signal, and pulses are about 30-50us low and high.
The keyboard has a scan code associated with each key. When the key is pressed – the code is transmitted. If the key is is hold down for a while, the code is transmitted repeatedly (about 10 times per second). After the key is released, the brake code is transmitted ($F0). Usually, all keys have 8-bit length codes, except some keys like Home, Insert, and Delay have an extended code from two to five bytes. The first bytes are always $E0. This is also true for the break sequence, e.g., E0 F0 xx…
The keyboard can handle three sets of scan codes. Default is set Two, which is used in this example.
The code in the example is a simple keyboard to the RS232 interface. Scanned codes are translated to ASCII characters and transmitted by UART. The code included in the example can be adapted to any AVR microcontroller with SRAM.
Note: The linker file(AVR313.xcl) included in the software archive has to be included instead of
The standard linker file. This is done from the included menu under XLINK Options. The linker file applies to AT90S8515 only.
The algorithm is working in that way: Keyboard actions are handled by INT0 interrupt. The algorithm is quite simple: Store the data line’s value at the leading edge of the clock pulse. The clock line is connected to one of the interrupt pins (INT0 or INT1). The interrupt will be executed every clock cycle. And data will be stored at the falling edge. After bits are received, the data is decoded by the decode function, and characters are stored in a buffer. Other special keys (arrows, pagination keys, etc.) are ignored.
The mapping from scan codes to ASCII characters is done by lookup table stored in flash memory.
Read more at: More About project
The source code: Source files