Have fun with PS/2: Keyboard and Mouse

Introduction

Have you heard PS/2 before? Many might be thinking about the PS2 controller when you all see the title of this project. Actually the PS/2 here is the purple color and green color socket at the back of the older desktop computer, for mouse and keyboard connection. Anyway I am using bread board friendly breakout board MiniDIN-6 connector. It breaks out all 6 pins to a 0.1″ spaced connector.

In this tutorial we will be showing that interfacing to PS/2 mouse or keyboard is not as difficult as you imaging. Today I will show you example solution to use the ps/2 keyboard and mouse using the PS/2 protocol and make the program simple so that everyone can understand and use the PS/2 easily.

PS/2 Interface and Protocol

A diagram of the PS2 connector is below.

1

The serial communication typically runs at a baud rate of 10 KHz to 16.7 KHz. Each byte sent by a device to a controller is transmitted as a packet containing 11 bits. The “device to host” packet has the following bits:

  • 1 start bit (always 0)
  • 8 data bits (least significant bit first)
  • 1 parity bit (odd parity)
  • 1 stop bit (always 1)

For more info please look at the reference slot below .

please note that the LSB and MSB is the same as the normal UART protocol, in fact it is very similar. This is 9-bit transmission with the 9th bit as odd parity bit. But the problem here, the baudrate might change and UART do not have clock line 🙂

2

Hardware required:

1. SK40C + PIC16F877A

DSCF7662

2. BREAKOUT BOARD PS/2 SOCKET

BBmdin6p

3. KEYBOARD AND MOUSE(which use PS/2 socket)

km

Software required:

MPLAB IDE & HITECH C PRO Installation

PICKIT 2

Related References:

• How to set up MPLAB IDE

How to Create a project

• How to use PicKit2

PS/2 with mBed

PS/2 Mouse Interface with Arduino

The PS/2 Keyboard Interface

•PS/2 Cytron website

 

Project:

Use the BB-mDIN-6P, interface with PS/2 keyboard and PS/2 mouse. Grab the information and do simple demo. Use PIC16F877A

EXAMPLE

We will start with PS/2 keyboard and continue with PS/2 mouse.

CONNECTION

For the connection of the both keyboard and the mouse are the same. The different is we either plug in keyboard or plug in mouse.

This is the picture of the connection I made for test.

DSCF7664

This is the connection picture from Fritzing

Untitled Sketch_bb

KEYBOARD

Plug in the keyboard to the BB-mDIN-6P

DSCF7668

SAMPLE CODE

The sample code contains the main body of the programming code. Some function can be referred below.

keyboardmain

This is the part where we indicate the result and display at the LCD

keyboardchkkey

MOUSE

Plug in the mouse to the BB-mDIN-6P

DSCF7671

SAMPLE CODE

This sample code contains the main body of the mouse ps/2 programming code.

mousemain

This is the part where indicate the result the display at the LCD

mousdisplay

SAMPLE CODE FUNCTION REFERENCE

Function of the conversion. This means that we swap the 8-bit data making the LSB become MSB and vice versa

convlsbmsb

Since the serial communication contain the parity bit. This is the part where to check the parity bits. Parity bit is being use to check whether the 8-bit data received are correct.  Some sort of calculation is involve and I will explain later.

paritychk

TUTORIAL EXPLAINATION ABOUT PS/2 MOUSE AND KEYBOARD

The PS/2 mouse and keyboard are widely use in the pass year before USB come out. Is good for us to know how to receive or transmit data from the controller to the mouse.

The basic protocol

For the keyboard, there are not much protocol in it, it is just transmit signal from the keyboard when a button is press follow with clock to indicate the signal like a SPI transmit data. But in this tutorial we are not using SPI to receive the signal since the protocol transmit from the keyboard is 11 bits.

Normally, when we press the button it will transmit a 11bits signal out and when we release it, it will transmit a break signal to notify the key is released.

The hex code for the command is here.

For PS/2 mouse, it is more tricky. When we start up the mouse, it need to be initialize at a certain time limit else it won’t give any signal. Moreover there are setting for the mouse before we could use it to send the signal to controller.

If I am not mistaken, we need to transmit reset, 0xFF to the mouse once receive a package to acknowledgement from the mouse which also contain 11 bits. Then, you can send the setting to the mouse anytime.

Below are some lesson about the mouse protocol,

The default protocol

snap

Mouse Commands

Every command or data byte sent to the mouse (except for the resend command fe) is ACKed with fa. If the command or data is invalid, it is NACKed with fe. If the next byte is again invalid, the reply is ERROR: fc.

Command d0: Read extended ID

Read up to 256 bytes.

Commands d1df: Vendor unique commands

Command d1: Logitech PS/2++ command

This command was to be used, followed by an arbitrary data sequence. Now replaced by the sliced commands using e8.

Command e1: Read secondary ID

Replies with two bytes. An IBM TrackPoint returns 01 as first byte, and a second byte depending on the model.

Command e2: IBM TrackPoint command

Followed by several parameter bytes. For details, see ykt3dext.pdf.

Command e6: Set mouse scaling to 1:1

Often ingredient in magic sequences.

Command e7: Set mouse scaling to 2:1

Often ingredient in magic sequences.

Command e8: Set mouse resolution

This command is followed by a byte indicating the resolution (0, 1, 2, 3: 1, 2, 4, 8 units per mm, respectively). It is used in magic sequences to transport two bits, so that four of these are needed to send a byte to the mouse. See below.

Command e9: Status request

This command returns three bytes:

First a status byte: Bit 7: unused, 0. Bit 6: 0: stream mode, 1: remote mode. Bit 5: 0: disabled, 1: enabled. Bit 4: 0: scaling set to 1:1, 1: scaling set to 2:1. Bit 3: unused, 0. Bit 2: 1: left button pressed. Bit 1: 1: middle button pressed. Bit 0: 1: right button pressed.

Then a resolution byte: 0, 1, 2, 3: 1, 2, 4, 8 units per mm, respectively.

Finally a sample rate (in Hz).

See below for special Synaptics Touchpad handling.

Command ea: Set stream mode

Command eb: Read data

Read a mouse packet. Needed in remote mode to ask the mouse for data. Also functions in stream mode.

Command ec: Clear echo mode

Command ee: Set echo mode

Command f0: Set remote mode

Command f2: Read mouse ID

(Only supported on some systems.) This command reads a 1-byte mouse ID. The reply is a single byte 00. Wait at least 10ms after issuing this command.

For the keyboard reply, see above.

BallPoint (trackball) devices return a single byte 02, Intellimouse returns 03, Explorer Mouse returns 04, 4d Mouse returns 06, 4dplus Mouse returns 08,as does the Typhoon mouse.

Command f3: Set mouse sample rate

(Only supported on some systems.) Set mouse sample rate in Hz. If the given sampling rate is acceptable the ACK is fa. Otherwise the NACK is fe, and the host can correct. If it is incorrect again fc is sent. Correct values are, e.g., 10, 20, 40, 60, 80, 100, 200.

Command f4: Mouse enable

The stream mode mouse data reporting is disabled after a reset and after the disable command. This command enables it again.

Command f5: Mouse disable

This stops mouse data reporting in stream mode. In stream mode, this command should be sent before sending any other commands.

Command f6: Set defaults

If this command is recognized, a reset is done (set sampling rate 100 Hz, resolution 4 counts/mm, stream mode, disabled, scaling 1:1), but no diagnostics are performed. For some enhanced mice that require a magic sequence to get into enhanced mode, this command will reset them to default PS/2 mode.

Command fe: Resend

If this command is recognized, the last mouse packet (possibly several bytes) is resent. There is no ACK to this command, but if the last reply was ACK, it is sent.

Command ff: Mouse reset

A self-test is performed. When OK, the response is aa 00. On error, the response is fc 00. The mouse is reset to default PS/2 mode.

 

Transmit and receive data

Here is the link which will let use know clearly how to transmit and receive with a good detail provided.

http://www.student.montefiore.ulg.ac.be/~pierards/spe/documentation/PS2-VGA.pdf

Still meet problem??

If there are still some parts which don’t understand, please kindly to post in our forum.

PROBLEM MAY OCCUR

· Mouse configuration is set up when it is starting up. Please don’t use reset button if the mouse are not responding. Use the on/off to start up the mouse

· Mouse and keyboard are quite different, keyboard send data once is connected and button is press, mouse would have same function but need to be initialized and the desire timing once it is connected.

· Please note that 5v and gnd wire miss plug will lead to short circuit which will damage the keyboard, mouse and SK40C.

 

THERE WHERE SAMPLE CODE AND SOME VIDEO AT BELOW SECTION:

video:

source code:

Buy

, , , ,

Related Post

Control and Monitor via SMS Part 2

Simple steps to control Stepper Motor using 2Amp Motor Driver Shield and CIKU

PIC16F VS PIC18F:Analysis of PIC Mid-Range 8 bit family and Advanced 8-bits Family

SK40C vs CT-UNO

Leave a Reply

Your email address will not be published. Required fields are marked *