PC mouse information

This documents tries to tell how PC mouse hardware works and how to read it at the lowest level. If you are programming under some ready made operating systems, then it would be better to use the routines supplied by the mouse driver (a good description of DOS mouse driver programming is available at ftp://x2ftp.oulu.fi/pub/msdos/programming/docs/gmouse.doc in ascii format). There is also a good tutorial Programming the Microsoft Mouse in the PC Game Programmer's Encyclopedia.

How mouse system works

Typical PC mouse controlling system has the following parts:

sensors -> mouse controller -> communication link -> data interface -> driver -> software
Sensors are the movement detectors (typically optomechanical) which sense the mouse movement and button swiches which sense the button states. Mouse controller reads the state of those sensors and takes acount of current mouse position. When this information changes the mouse controller sends a packet of data to the computer data interface controller.

The mouse driver in the computer received that data packet and decodes the information from it and does actions based on the information. Typically mouse driver has the information of the current mouse state (position and button states) and tells them to the application or operating when it asks them. Typically the mouse drive calls mouse cursor moving routines when mouse is moved and sends messages to the software when buttons are pressed.

In typical modern PC mouse driver the actual cursor movement is not linearly related to the mouse movement. This might sound a bit strange but it has been found that there are better ways to change the mouse movement to cursor muvement than just simply causing one mouse step to move the cursor one pixel. During the pioneering research done at Apple Computer in the devellopment of the graphical user interface (GUI), it became apparent that no particular ratio between mouse movement and cursor movement was best suited for all tasks.

Early work detected that there are two basic movements in the use of pointing devices: move cursor to desired area and then exactly to desired target. Those two movements have contradictory requirements, so Apple solved the problem by monitoring the mouse movements and change it's CPI (counts per inch) characteristics. When the mouse was moved slowly it remained 100 CPI and when the mouse was moved fast it appears to be 400 CPI mouse. This method of adjusting CPI based on its usage has now been adopted by the default driver in Windows 95 and is now the most commonly accepted way of translating mouse movements to cursor screen movement


Mouse hardware information

Serial mouse

Voltage levels:

Mouse takes standard RS-232C output signals (+-12V) as its input signals. Those outputs are in +12V when mouse is operated. Mouse takes some current from each of the RS-232C port output lines it is connected (about 10mA). Mouse send data to computer in levels that RS-232C receiver chip in the computer can uderstand as RS-232C input levels. Mouse outputs are normally something like +-5V, 0..5V or sometimes +-12V. Mouse electronics normally use +5V voltage.

Hardware implementation

PC serial mouse uses typically DRT and RTS lines for generating +5V power for microcontroller circuit in the mouse. Because typical optomechanical mouse also needs power for 4 leds in the optocoupler movevement detectors, there is not much power to loose. A typical approach is to use diodes to take current fron DTR and RTS lines and then feed it through resistor to all of the (infrared) leds in the movement detectors. All four (infrared) leds are connected in series, which gives about +5V voltage drop over all leds (typical to leds used in moused). This +5V is adequate power for low power mouse microcontroller. The serial data transimitting circuit consists of simple discrete transistor circuir to make it consume as little power as possible. The positive power supply usually taken from RTS and DRT lines (just after the diodes and before the resistor going to leds). The negative supply for transmitter is taken from TD pin. Typical PC serial port mouse takes 10 mA total current and operates at voltage range of 6-15V. The data itself in sent using standard asuncronous RS-232C serial format:

              Start D0  D1  D2  D3  D4  D5  D6  D7  Stop
   Logic 0      ___ ___ ___ ___ ___ ___ ___ ___ ___
  +3..+15V     |   |   |   |   |   |   |   |   |   |
               |   |   |   |   |   |   |   |   |   |
               |   |   |   |   |   |   |   |   |   |
   Logic 1     |   |   |   |   |   |   |   |   |   |
  -3..-15V  ___|   |___|___|___|___|___|___|___|___|____

Microsoft serial mouse

Microsoft serial mouse is the most popular 2 button serial mouse type. Typically that cheap tho button mouse which comes with the computer is Microsoft mouse system. Microsoft mouse is supported in all major operating systems.

Mouse resolution and tracking rate

Maximum tracking rate for Microsoft mouse is 40 reports/second * 127 counts per report = 5080 counts per second. The most common range for typical mouses is 100 to 400 CPI (count per inch) but can be up to 1000 CPI (cheap ones typically are 100 CPI or 200 CPI models). This means that you can move 100 CPI mouse up to speed of 50.8 inches per second and 400 CPI mouse maximally at 12.7 inches per second. The actual accuracy of movement the software sees is detemined by the settings of the mouse driver (many mouse drivers have option to adjust mouse sensitivity).

Pinout

9 pin    25 pin     Wire Name            Comments
shell     1         Protective Ground
3         2         TD                  Serial data from host to mouse (only for power)
2         3         RD                  Serial data from mouse to host
7         4         RTS                 Positive voltage to mouse
8         5         CTS
6         6         DSR
5         7         Signal Ground
4         20        DTR                 Positive voltage to mouse and reset/detection

RTS = Request to Send   CTS = Clear to Send
DSR = Data Set Ready    DTR = Data Terminal Ready
To function correctly, both the RTS and DTR lines must be positive. The lines DTR-DSR and RTS-CTS must NOT be shorted. Implement the RTS toggle function by setting the RTS line negative and positive again. The negative pulse width is at least 100ms. After a cold boot, the RTS line is usually set to a negative level. In this case, setting the RTS line to a positive level is also considered an RTS toggle.

Serial data parameters:

1200bps, 7 databits, 1 stop-bit

Data packet format:

Data packet is 3 byte packet. It is send to the computer every time mouse state changes (mouse moves or keys are pressed/released).
        D7      D6      D5      D4      D3      D2      D1      D0
 
1.      X       1       LB      RB      Y7      Y6      X7      X6
2.      X       0       X5      X4      X3      X2      X1      X0      
3.      X       0       Y5      Y4      Y3      Y2      Y1      Y0
Note: The bit marked with X is 0 if the mouse received with 7 databits and 2 stop bits format. It is also possible to use 8 databits and 1 stop bit format for receiving. In this case X gets value 1. The safest thing to get everything working is to use 7 databits and 1 stopbit when receiving mouse information (and if you are making mouse then send out 7 databits and 2 stop bits).

The byte marked with 1. is send first, then the others. The bit D6 in the first byte is used for syncronizing the software to mouse packets if it goes out of sync.

LB is the state of the left button (1 means pressed down)
RB is the state of the right button (1 means pressed down)
X7-X0 movement in X direction since last packet (signed byte)
Y7-Y0 movement in Y direction since last packet (signed byte)

Graphical description how the data is contained in the packet

              1st byte        2nd byte         3rd byte
          ================  ===============  ================
           - 1 ? ? Y Y X X  - 0 X X X X X X  - 0 Y Y Y Y Y Y
          ================  ===============  ================
               | | \ / \ /      \---------/      \---------/
               | |  |   |            |                |
               | |  |   \----\       |                |
               | |  \--------|-------|--------\       |
               | |          / \ /---------\  / \ /---------\
               | |         ================ =================
               | |          0 0 0 0 0 0 0 0  0 0 0 0 0 0 0 0
 Left Button --/ |         ================ =================
Right Button ----/            X increment      Y increment

Mouse identification

When DTR line is toggled, mouse should send one data byte containing letter 'M' (ascii 77).

Logitech extension to protocol

Logitech uses this same protocol in their mouses (for example Logitech Pilot mouse and others). The origianal protocol supports only two buttons, but logitech as added third button to some of their mouse models. To make this possible logitech has made one extension to the protocol.

I have not seen any documentation about the exact documents, but here is what I have found out: The information of the third button state is sent using one extra byte which is send after the normal packet when needed. Value 32 (dec) is sent every time when the center button is pressed down. It is also sent every time with the data packet when center button is kept down and the mouse data packet is sent for other reasons. When center button is released, the mouse sends the normal data packet followed by data bythe which has value 0 (dec). As you can see the extra data byte is sent only when you mess with the center button.

Mouse systems mouse

Serial data parameters:

1200bps, 8 databits, 1 stop-bit

The data is sent in 5 byte packets in following format:

        D7      D6      D5      D4      D3      D2      D1      D0

1.      1       0       0       0       0       LB      CB      RB
2.      X7      X6      X5      X4      X3      X2      X1      X0
3.      Y7      Y6      Y5      Y4      Y3      Y4      Y1      Y0
4.      X7'     X6'     X5'     X4'     X3'     X2'     X1'     X0'
5.      Y7'     Y6'     Y5'     Y4'     Y3'     Y4'     Y1'     Y0'

LB is left button state (0=pressed, 1=released)
CB is center button state (0=pressed, 1=released)
RB is right button state (0=pressed, 1=released)
X7-X0 movement in X direction since last packet in signed byte 
      format (-128..+127), positive direction right
Y7-Y0 movement in Y direction since last packet in signed byte 
      format (-128..+127), positive direction up
X7'-X0' movement in X direction since sending of X7-X0 packet in signed byte 
      format (-128..+127), positive direction right
Y7'-Y0' movement in Y direction since sending of Y7-Y0 in signed byte 
      format (-128..+127), positive direction up
The last two bytes in the packet (bytes 4 and 5) contains information about movement data changes which have occured after data butes 2 and 3 have been sent.


Bus mouse

Bus mouse is connected to the PC using a mouse card put to ISA bus. The card has the intelligence in it and the mouse has only the detectors for mouse movement and mosue button switches. The mouse is connected to card using 9 pin Hosiden connector.

Connector pinout

(the pin umbering might be wrong in this)
        1 2 3

       4 5 6 7

         8 9


1.      SW2
2.      SW3
3.      Signal Ground
4.      XB
5.      YA
6.      YB
7.      SW1
8.      +5V
9.      XA
SW1, SW2 and SW3 are signals from mouse buttons.
XA, XB, YA and YB are quadriture signals from mouse movement detectors.

Another connector numbering scheme


         7 8 9

        3 4 5 6

          1 2

 1.      +5v
 2.      XA  (H-Pulse)
 3.      XB  (HQ-Pulse)
 4.      YA  (V-Pulse)
 5.      YB  (VQ-Pulse)
 6.      SW1 (Left)
 7.      SW2 (Middle)
 8.      SW3 (Right)
 9.      GND
 SW1, SW2 and SW3 are signals from mouse buttons.XA, XB, YA and YB are
 quadriture signals from mouse movement detectors.

Wiring according logitech documentation

Pinout:

         7 8 9

        3 4 5 6

          1 2
Pin functions:
Wire     Mini-DIN       Logitech             Microsoft
Color      Pin       P-Series Signal       InPort Signal
Black       1             +5V                  +5V
Brown       2              X2                   XA
Red         3              X1                   XB
Orange      4              Y1                   YA
Yellow      5              Y2                   YB
Green       6              Left                 SW1
Violet      7              Middle               SW2
Gray        8              Right                SW3
White       9              GND                  Logic GND
SHIELD    shell            chassis              chassis

PS/2 mouse

PS/2 mouse is connected to computer motherboard using same type of 6 pin connector as PS/2 keyboard. The data is sent using syncronous serial protocol similar to protocol used by PS/2 keyboard. The mouse data is handled using keyboard controller. PS/2 type mouse is becoming more and more common because many new computers (nearly all big brands and new no brand computers made using ATX standard motherboards) have PS/2 mouse port integrated it in it and using PS/2 mouse connected to that port also frees one serial port for other uses.

Mouse resolution and tracking rate

The standard PS/2 mouse (with Logitech mouse) defaults to 160 CPI and can be switched to 40, 80, 160 or 320 CPI with software. Microsoft mouse driver for Windows 3.x and Windows 95 defauls to 160 couns per inch. The maximum tracking rate for PS/2 mouse is 40 report/second * 255 counts per report = 10200 counts per second. For 100 CPI mouse this would indicate maximum trackign rate of 102 inches per second and for 400 CPI mouse only 25.2 inches per second.

Connector pinout

Pin Wire Name 1 DATA 2 Reserved 3 Ground 4 +5V Supply 5 CLK 6 Reserved Shield Chassis

Data packet format

        D7      D6      D5      D4      D3      D2      D1      D0
1.      XV      XV      YS      XS      1       0       R       L
2.      X7      X6      X5      X4      X3      X2      X1      X0
3.      Y7      Y6      Y5      Y4      Y3      Y2      Y1      Y0

L       Left button state (1 = pressed down)
R       Right button state (1 = pressed down)
X0-X7   Movement in X direction
Y0-Y7   Movement in Y direction
XS,YS   Movement data sign bits (1 = negative)
XV,YV   Movement data overflow bits (1 = overflow has occured)

Physical connector

The PS/2 mouse connector has the following pinout when looking at the connector on the back of the computer:

6 Pin PS2

1   DATA
2   No connection
3   GND
4   +5V
5   CLK
6   No connection

The PS2 is numbered as follows:
        
      6   5
     4     3
       2 1

Bi-directional transmission is controlled by the CLK and DATA lines. Both are fed by an open collector device which lets either host or mouse force the line to "0". During non-transmission, CLK is at "1" and DATA can be at "0" or "1".

The host can inhibit mouse transmission by forcing CLK to "0". If the host inhibits the mouse while it is transmitting, the byte must be retransmitted (if the inhibit state arrived before the 11th clock).

There is a simple description in my old IBM PS/2 model 50/60 technical reference. The mouse interface is the same as the keyboard interface. The Intel 8042 supports two channels, one for keyboard and one for the auxiliary device (mouse, trackball, touchpad). Pin 1 is the data, pin3 ground, pin 4 +5V and pin 5 clock.

Receiving data: Check 'clock'. If inactive, there is a bit on the 'data' line. Each transmission unit is one start bit, eight data bits, odd parity and one stop bit. Start bits are low, stop bits high. Each clock active or inactive period is 30 to 50 microseconds. Data transition to falling edge of clock is 5 to 25 microseconds.

Sending: Check that both clock and data are high. Pull down data for start bit, and start clocking.

NOTE: Logitech has made their own extension also to this protocol to allow three buttons.


Mouse movement detectors

The most tyical construction of mouse movement detectors are optomechanical detectors. The system works so that X and Y movement of the mouse rotates slotted disks. The movement info is then picked from those slotted disks using optical sensors. There are two sensors for each slotted disk and those sensors give out two singals which are 90 degreed out of phase compared to each other.


sensor #0 ----    ----    ----    ----   light
              ----    ----    ----       no light

sensor #1   ----    ----    ----    --   light
          --    ----    ----    ----    no light
So if we call the getting light phase '1', and the not getting light phase (when the light is blocked by the wheel) '0' then it looks like this:
sensor #0 - 1111000011110000111100001111
sensor #1 - 0011110000111100001111000011
            ^^^^^^^^
            full cycle of pulses
This can be decoded into four discrete bit pairs for the two sensors:
         Forward direction     Backwards direction
            0 1 2 3                3 2 1 0
sensor #0 - 1 1 0 0                0 0 1 1
sensor #1 - 0 1 1 0                0 1 1 0
This information can be easily decoded usign small microcontroller and then sent to computer in correct format.

Information sources


Tomi Engdahl <[email protected]>