SBC In-System Programming: Information | VisISP-52 ISP Application
SBCMON Monitor/Operating System: Main Page | Manual
SBCMON-based Software: SBCMON Monitor | Keypad Demo | LCD Echo Demo | Hardware Clock | Software Clock
SBCMON-based PS/2 Software: PS/2 Comm Monitor | PS/2 Keyboard | PS/2 Mouse
ORDER 8052.com SBC: PCB, KIT OR BUILT SBC AVAILABLE FOR PURCHASE
Download PS/2 Keyboard for SBCMON
You may download either the source code or the ready-to-use Intel-Hex version of the keyboard demonstration program. The Intel-Hex version may be loaded into SBCMON's memory using the L or Q commands. Download the source ASM program if you'd like to see the code that makes SBCMON work.
-
SBCMON PS/2 Keyboard Program Source (ps2key.asm)
SBCMON PS/2 Keyboard Intel HEX (ps2key.hex)
NOTE: This demo program requires SBCMON monitor program to run. Be sure to download SBCMON first, install on your SBC, and then run this demo from within SBCMON itself. This program may require additional modification in order to run without SBCMON.
This program allows a standard PS/2-style keyboard to be connected to the SBC. Keypresses on the keyboard are detected by the program, the keyboard scan code of 1-3 bytes is converted to a single-byte scancode, and the byte is displayed on the terminal.
You may press any key in the PC's terminal program in order to exit the program and return to SBCMON.
Concepts Demonstrated by Program
This program demonstrates the following concepts:
- Communicating with keyboard using clock and bidirectional data line.
- Utilizing the keyboard's communication protocol.
- Translating a multi-byte keyboard sequence to a single byte
This program requires a PS/2 keyboard or mouse to be connected to the SBC. The keyboard requires a +5V power supply, ground, and the program assumes that the keyboard data line is attached to P1.3 and the clock line is attached to P1.2.
I would recommend using an external board to mount a DIN-6 keyboard connector (Digikey part #275-1043-ND). On this board, mount two 8-pin SIP headers. The first SIP header will allow you to attach a ribbon cable from port 1 (J7 on the SBC) and the second SIP header will allow you to attach a ribbon cable from the EX connector (J11). The J7 connector will expose P1.2 and P1.3 while the J11 connector will provide +5V and ground.
Connecting the PS/2 Connector
The PS/2 connector is a female DIN-6 connector. The pinout is pictured to the right. The top graphic shows the pinout when looking at the PS/2 female connector. The bottom portion of the graphic shows the normal pinout of the pins that are connected at a right angle to the PCB. The pins use 0.1" spacing so are very appropriate for a thru-hole circuit board.
Theory of Operation
The routines used in this example program can best be summarized as follows:
-
PS2_Init: Initializes the keyboard, sets the repeat rate to 30 characters/second,
and turns off all keyboard LEDs.
PS2_SetLeds: Turns the three keyboard LEDs on or off. Bits 0, 1, and 2 control the LEDs accordingly.
PS2_GetByte: This reads a single byte from the keyboard. If the keyboard has no data pending, it clears the carry which indicates no data was read. If the keyboard has data pending, it is returned in the accumulator and the carry bit is set. Note that this will return one byte and that a single keypress may generate multiple bytes.
PS2_GetScanCode: Like PS2_GetByte, this routine will clear the carry if there is no data waiting and set the carry flag if data was returned by the keyboard. However, while PS2_GetByte will return a single byte (which may not be all the data required to process a keypress), PS2_GetScanCode will read 1, 2, or 3 bytes of data from the keyboard as necessary to determine the full keypress. It then translates the data received into a single byte to represent that keypress. For example, pressing the 'A' key will return the character 'A', pressing the 'C' key will return the character 'C'. Non-printable characters (escape, tab, function keys, etc.) are assigned a unique value--the values themselves can be viewed in the source code represented by the "KS_" equates. For example, KS_RIGHTCTRL is equated to 'b' so pressing the right control key would return the character 'b'.
Credits
This example program was based on Gabriel Lour's "keyboard routine" code which was contributed to the 8052.com code library. Modifications were made to his code to make the example program more closely conform to the style of the other SBC example programs. Additionally, the routines were modified so as not to require any specific addresses in internal RAM. Rather, "R" registers are used for temporary variable storage.
Additionally, Gabriel's code deals with the keyboard's scan codes. This example program, on the other hand, takes the 1-3 byte keyboard scan codes and converts them to a a single-byte unique scan code. This makes it much easier to deal with the resulting data in an assembly language program.
Information on the PS/2 connector pin-out and the keyboard scan codes returned by the keyboard were obtained from Adam Chapweske's site.