- Published: Sunday, 26 September 2010
- Written by Graham Mitchell
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.
Keyboard - PIC
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:
In the above code, calling swKBD.NewKey will initiate the Host (PIC) to check if there is new data on the Device (keyboard). If the device has nothing to send, it could take up to 4mS to return "False" and continue on in the program. With no keys being pressed, PORTB.5 would be toggled at 250Hz (I used this as an example of how they main program loop is not restricted to wait for a key to be pressed..)
In the above example, the additional task is toggling a pin, but what if you had a really long task, one that took seconds? Well, the keyboard will store all key-presses, and send them out, one by one each time swKBD.NewKey is called. How you handle this is up to you.
Caps Lock = Shift
Upon power-up, the keyboard is configured to 'Make Only' mode. That means data is ONLY sent when keys are pressed, nothing is sent when they are released. Other characters that would normally need 'Shift' to work (such as !@#$%^&*) are now accessed by pressing Caps lock (once) and then the keyboard buttons 12345678. Don't want to operate in 'Shift' mode? Then turn Caps Lock off by pressing it a second time.
The above method greatly reduce the code overhead required to parse key codes. If you're familiar with AT Keyboard Make/Break codes then you'll know what I mean.
Other non-white character codes can be found in the Public Const list, within the module.
The module options are well commented, but if you want more information on any of them, have a read of the info that follows.
#Option KBD_CLK / KBD_DATA
These are two of the four wires that connect to the keyboard. More info in the interfacing section of this article.
#Option KBD_PULLUPS = True
This option ensures the internal pullups on PORTB are used. That way, no external components are required when interfacing with the keyboard. Handy.
Everyone likes flashing LEDs, so there's no point in letting the three on the keyboard go to waste! This option will allow the module to monitor Caps, Scroll and Num lock presses. From there, the module will send the appropriate command to the keyboard to toggle the LEDs.
Also, when this option is enabled, if you press "A" with Caps off then the key will be encoded too "a". Alternatively, if Caps was on the it would return "A". Put simply, caps lock replaces shift (for reasons explained in "Important Points" above.)
If you disable KBD_UpdateLEDs then the keyboard will either work in uppercase or lowercase mode. It's the equivalent of either holding shift, or leaving it released. "A" would translate to "a" in LCASE mode, and "1" would translate to "!" in UCASE mode.
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.
External PS/2 AT Keyboard Links
- Interfacing the PC's Keyboard.
- AT Keyboard Interface V1.04 for Microchip PIC 16F84 Microcontroller
- The PS/2 Keyboard Interface
- The PS/2 Mouse/Keyboard Protocol