- Interface AVR to standard PC AT keyboard;
- Only two I/O lines used. One line is also connected to external interrupt pin of AVR;
- No external components is needed for interface;
- Included C source reads from keyboard interface and converts to serial
In many situations you need some kind of human interface to 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 open collector with pull-up resistors.
Keyboard cable connectors can be DIN or Mini DIN (We are not talking about USB interface):
According to keyboard timing diagram in bellow picture the keyboard transfers data to host AVR microcontroller. The protocol is: one start bit (always 0), eight data bits, one odd parity bit and one stop bit (always 1). The data is validated during the low period of clock pulse. Clock signal is generated by keyboard and pulses are about 30-50us low and high.
The keyboard has a scan code associated with each key. When key is presse â€“ the code is transmitted. If key is is hol down for a while the code is transmitted repeatedly (about 10 times per s). After 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 codes from two to five bytes. The first bytes is 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 example is a simple keyboard to RS232 interface. Scanned codes are translated to ASCII characters and transmitted by UART. The code included in 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 include menu under XLINKâ€“Options. The
linker file applies to AT90S8515 only.
Algorithm is working in that way: Keyboard actions are handled by INT0 interrupt. The algorithm is quite simple: Store the value of the data line at the leading edge of the of the clock pulse. Clock line is connected to one of interrupt pins (INT0 or INT1). The interrupt wil be executed every clock cycle. And data will be stored at the faling edge. After bits are received the data is decoded by 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 are done by lookup table stored in flash memory.
Read more at: More About project
The source code: Source files