PS/2 Keyboard Unit

This library unit implements several procedures to read a PS/2 keyboard and control its LEDs. Note that the extended keys (cursor and Windows keys) have not been implemented yet, but there is a stub in place for them should you be so inclined.

Also bundled with the library unit in the download package is an include file containing the key codes. This is pretty extensive, which is why I put it in a separate file.

Compilation Constants


Your program should define three compiler conditionals  which indicate the pins to which the keyboard is  connected. Here's an example for the PIC16F88:

{$DEFINE PS2PORT 'PortA'}    // keyboard on Port A
{$DEFINE PS2Clock 0}         // clock on A.0
{$DEFINE PS2Data 1}          // data on A.1


Optionally, you can change the delay between keystrokes which otherwise defaults to 10 mS.

{$DEFINE PS2Delay 5}         // R/W delay in mS

The data direction ports (TRIS) are taken care of automatically in the initialization routine.

Both keyboard pins should have 10k pull-up resistors attached.

Commands


There are five commands, and their use is pretty self-evident. The last one listed here is the one you're most likely to use frequently.

function PS2_Read : byte;
read a keyboard byte

procedure PS2_Write(PS2Byte : byte);
write a keyboard byte

procedure PS2_LEDs(Kbd_LEDs : byte);
set the LEDS on or off,
Bit 0 = the Scroll Lock, Bit 1 = Num Lock and Bit 2 = Caps Lock

procedure PS2_UpdateLEDs;
synchronize the LEDs to the actual key status,
(Scroll Lock, Caps Lock, Num Lock)

function PS2_Key : char;
return ASCII value

Add It to Your Library Collection


As mentioned, there are two files here. First is the actual library unit, "PS2_KBD.pas." And second is the include file containing all the keycodes, "PS2_KBD_Codes.pas." They are zipped together in the download package.


Be sure to read over the source code for additional details on how to use it.

Try It Out


If you'd like to see it in operation, then head over to the exercises to find the PS/2 keyboard exercise. Click here to go to it.

No comments:

Post a Comment