The keyboard is the most common way for humans to input information into a computer. It has been around since before computers were mainstream and everyone was still using typewriters. Because of this prevalence in society, it's important that we understand how to interface to the basic PS/2 keyboard.
This article will describe and show you an example of how to create a system capable of interacting with a keyboard in order to understand what keys have been pressed. The example system is built on the LPC1343 Development Board, but the PS/2 Library Code is generic and can be ported to any other microcontroller.
When 'B' is pressed, 0x32 is transmitted by the keyboard.
When 'B' is released, Break(0xF0) and 0x32 are transmitted by the keyboard.
Important Points:
This article will describe and show you an example of how to create a system capable of interacting with a keyboard in order to understand what keys have been pressed. The example system is built on the LPC1343 Development Board, but the PS/2 Library Code is generic and can be ported to any other microcontroller.
PS/2 Male Connector |
PS/2 Female Connector |
So let's start from the basics. PS/2 is a serial protocol and only uses 2 pins to communicate information. The data pin is actually bi-directional, but we will only consider the data output from the PS/2 device.
The standard pinout for a mini-din or PS/2 port can be seen above for both the male and female connectors. Typically the male connector is on the keyboard/mouse or whatever PS/2 device you have and the female connector will be on the receiving device (laptop/desktop) which is the LPC1343 in our case.
The standard pinout for a mini-din or PS/2 port can be seen above for both the male and female connectors. Typically the male connector is on the keyboard/mouse or whatever PS/2 device you have and the female connector will be on the receiving device (laptop/desktop) which is the LPC1343 in our case.
PS/2 Data Output |
So initially, we connected PS/2 Keyboard with the LPC1343 development board and then connected the Logic Analyzer to view the pluses. The following images are from the Logic Analyzer Application and show the pulses for keypress 'A' and 'B', whenever a key is pressed then a scan code is sent by the keyboard and when the key has released the break and keyboard scan code is sent, which is shown below in the images.
One more interesting thing is that, when a key on the keyboard is pressed, suppose 'A' is pressed, the keyboard will not send the ASCII character of 'A', in spite it will send the scan code, by this scan code we will determine which key is pressed.
When 'A' is pressed, 0x1C is transmitted by the keyboard.
One more interesting thing is that, when a key on the keyboard is pressed, suppose 'A' is pressed, the keyboard will not send the ASCII character of 'A', in spite it will send the scan code, by this scan code we will determine which key is pressed.
When 'A' is pressed, 0x1C is transmitted by the keyboard.
When 'A' is released, Break(0xF0) and 0x1C is transmitted by the keyboard.
When 'B' is pressed, 0x32 is transmitted by the keyboard.
When 'B' is released, Break(0xF0) and 0x32 are transmitted by the keyboard.
Important Points:
- The host provides Power Supply
- Data & Clock lines are open collectors with a pull-up resistor to allow two-way communication.
- In the Normal Mode of operation, it is a keyboard that derives both lines to send data to the PC/Host.
- When it is necessary the Host can take control to configure the keyboard and change the status LEDs(CAPS/NUM/Scroll).
- In Idle, both Clock and Data Lines are held high, by the pull-up.
- For every 8 bits of data output, there is a start and a stop bit.
- All data output is valid only on a falling clock edge.
Now based on the information above we can write the code to determine the scan code and then based on the scan code, we can determine which key is pressed.
Algorithm
- Configure Clock Pin to Interrupt on Falling Edge.
- When Interrupt occurs, read Data line values.
- Decode bits and store Data in a variable.
- Check Parity, if parity is correct, move data in a queue else reject data.
- Extract Scan Code from Queue and compare the scan code with look-up data to determine which key is pressed.
Have a look at this video for a demonstration of the project.
Schematic Diagram
PS2_DATA is connected to PIO3.2 and PS2_CLK is connected to PIO3.3
Code is written in IAR for ARM version 7.60
No comments:
Post a Comment