Interfacing PC keyboard to AVR microcontroller
-->Properties:
- 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
Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post | TrackBack URI for this post
New on WinAVR Tutorial
Running TX433 and RX433 RF modules with AVR microcontrollers,Sometimes in embedded design you may want to go wireless. Might be you will want to log various readi …Programming AVR ADC module with WinAVR,Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solut … |
New on WinARM Tutorial
What are differences between WinARM and WinAVR,Everyone who is working with AVR microcontrollers knows this powerful tool – WinAVR (http://win …LPC2000 watchdog timer,As in all microcontrollers watchdog timers purpose isto reset microcontroller after reasonable amount … |

September 18th, 2006 at 10:53 am
hello.i made a led message board for showing words in a led matrix by avr atmega 32 I want program it without pc only by a keyboard.can you help me please?
thanks.
nader
September 19th, 2006 at 8:27 am
First of all try to read scan codes from keyboard. when you will have scan codes you can decode them to char’s and perform your tasks according to them.
The scan code reading procedure is same – just copy it. It’s hard to say what experience you have with embedded programming, but changing one AVR to another AVR micro-controller shouldn’t hurt. If there are some particular parts you are concerned about, it would be easier to help.
October 10th, 2006 at 7:39 pm
Thanks for the article!
I want to do precisely this, but with an ARM microcontroller.
Could you describe the hardward modifications needed to interface the keyboard to an ARM7 microcontroller (such as the LPC2119 which is 3.3V, “5V tolerant”)?
Yours,
Murray
(BTW, the comment interface below your article does not work properly with the FIrefox browser — 1.5.0.7. It looks OK, but the “please add” field doesn’t accept input correctly. The input gets referred to the Web page input field.)
October 11th, 2006 at 4:27 pm
I guess Keyboard should be powered through separate 5V regulator. For data lines you may leave as they are because ARM tolerate 5V signals. But to be safer you may use like resistor dividers from incoming signals..
KB—-/\/\/\———–ARM
…………….1,8k |
……………………/
……………………\ 3.3k
……………………/
……………………|
…………………—
About comments…I dont know I work with firefox and didnt notice such problems. Anyway thanks for comment.
July 29th, 2007 at 2:56 am
hi
i need to know how can i make a serial conection (rs232)between pc & avr atmega16. i must have a program in my o.s.(win xp) that can convert every charecter we typed in a text box to ascii code and send that to atmega16 & micro. convert that to it’s charecter and display that on a lcd(16*2).
pleas help me .thanks
October 24th, 2007 at 1:24 am
omg, i r total noob. can u pls help me do my projekt? pleas help me.
Seriously though, nice write up. I’m looking to do something similar on my Arduino. This will definitely be a source I’m coming back to. Thanks for taking the time to present this.
February 23rd, 2008 at 8:52 pm
[...] It turns out that only four of the pins on the PS/2 connector are used by the keyboard, and two of those are power and ground. The other two are data outputs generated by the keyboard when a key is pressed. No data needs to be sent to the keyboard, so the interface is very simple. The two data outputs from the keyboard are clock and data, both normally high. When a key is pressed, the clock signal pulses low 11 times, and the data signal sends 11 bits of data in sync with the clock. Bits 1-8 are the scan code data byte. Bits 0, 9, and 10 are start, parity, and stop bits, and can be ignored. More data details can be found in this write-up of interfacing a PC keyboard to a microcontroller. [...]
April 4th, 2008 at 5:37 am
Hi how to use this code in ave studio 4, and is it possible to use a usb to ps/2 converter (ie usb keyboard instead ps/2)
thanks
April 6th, 2008 at 12:55 am
Very good job.Im impressed
I would like to help me connecting my ps2 keyboard in stk500(AVR atmega16 mcu) and make my leds flash when i stroke a key . The board has 8 leds but i think i’ll need 11 to present the 11 bits. Any idea? Or i will make except portD and portB as output? In assembly would be nice. Ihave avr studio to connect with stk500.
Thanks
June 7th, 2008 at 7:33 pm
dear scienceprog..
this is a good article,thank’s a lot.
i’m using AVR ATMEGA8535 for displayed character on dot matrix.
since i try to running C language program that you’ve made. it seems i found error. i cannot find header called ‘pgmspace.h’.where i can find that header?
please help me scienceprog,thank’s for your attention.
July 9th, 2008 at 10:44 am
Dear Mr. or Mrs..
i have tried this program but i couldn’t compile or make it. An errors said that there’s no pgmspace.h and ina90.h header in this program. And at scancodes header, it said that it needs array.. i don’t know where’s the problems, and i ask you to give me advice to solve this problems..
thank you..
September 20th, 2008 at 5:49 pm
hi! im trying to do the same project but in assembly. If anyone have it done and would like to share it , it would be helpful!
Thanks in advance. Any offer is welcome!