A PS/2 Keyboard can extend the functionality of a humble PIC microcontrollerby 101 Keys with 4 simple connections. There's no need for external components - the PIC Micro has more than enough on-board to handle the entire operation.
Before I press on, its important to note that I am using an AT (Advanced Technology) type keyboard. They aren't very advanced anymore, most have a 6 Mini-DIN connector and cost less than $10 from your local PC store or eBay.
Also, this is a explanatory guide as to how the Swordfish swKBD module can be used in the real world. More information about PS/2 Keyboard protocol other issues will be covered in a future article.

Almost everyone who's written a PIC program has relied at some-stage to use an array of switches or a matrix keypad for data entry. More often than not, I'd imagine the cost of the hardware, not to mention the I/Os required from the PIC would exceed that of what's required for a $5-10 keyboard.
If there's one more feature that might help cog application uses, perhaps its plug-and-play. There's no need to surrender a whole keyboard to one project - unplug it and use it on something else. The user module handles such scenarios quite well. (caveat: hot swapping can potentially cause damage on older devices - we're talking fairly old there, newer keyboards with PTC Fuses and robust port implementation will do fine).
The conventional AT PS/2 Keyboard either uses a 5 pin DIN connector, or a 6 pin Mini-DIN. As I've found Mini-DIN to be more popular these days, I'll focus on that. If you have a variation, then google the pinout and use the schematic below.
The picture on the right is what a 6 pin Mini-DIN looks like. There's a pinout for both male and female types. The wire color is also listed, but as many have found in the past with this sort of thing, wire colors are not a good indication to go by.
Typically, pull-up resistors are required for clock and data. Almost every PIC has pull-up resistors on PORTB, so I will be making use of those in software. If you plan on following suit, here is the schematic for interfacing with a PS/2 Keyboard:
PORTB not an option? Then connect a 1K-10K resistor for both clock and data to +5V.
The code is well commented, so I will keep the introduction short.. Typical use of the module:
Device = 18F2520 // 18F2520 PIC in use, could be any 18F PIC Clock = 32 // clock speed is 32Mhz (8MIPS) Config MCLRE = Off // disable MCLR Include "InternalOscillator.bas" // search for "User Module Pack" at www.digital-diy.com Include "USART.bas" // used for displaying content on a uart terminal Include "swKBD.bas" // PS2 Keyboard module. URL http://digital-diy.com/home/swordfish/user-modules/242-ps2-keyboard-module-swkbdbas.html SetBaudrate(br38400) // initialise USART for 38400 baud USART.Write("Power On",13,10) // send a message to the terminal While True // main program loop If swKBD.NewKey Then // checks the device for new information If KBD.ValidChar Then // ensure the key is a valid non-white space character USART.Write(KBD.KeyChar) // yes, display it via USART ElseIf KBD.KeyCode = KBD_ENTER Then // check if the 'Enter' key was pressed USART.Write(13,10) // yes, send a line feed and carriage return EndIf // EndIf // High(PORTB.7) // toggle PORTB.7 high then low, Low(PORTB.7) // to measure the time it takes between loops Wend //
The module options are well commented, but if you want more information on any of them, have a read of the info that follows.
// default module options - user options can override these values... #option KBD_CLK = PORTB.4 // clock pin (connects to pin 5 of Mini-Din 6 pin) #option KBD_DATA = PORTB.5 // data pin (connects to pin 1 of Mini-Din 6 pin) #option KBD_PULLUPS = true // a note: internal pullups can be used instead of external resistors #option KBD_UpdateLEDs = true // monitor and handle Caps/Scroll/Num lock LEDs #option KBD_DefaultASCII = LCASE // select UCASE or LCASE mode if LEDs are disabled
Save the following program to the Swordfish UserLibrary folder, ensure it is named "swKBD.bas".
This is a basic sequence of events for the module. Click on the image to enlarge it.