Interfacing with a mouse

From Mech
Jump to navigationJump to search

Original Assignment

Interface the PIC with a PS/2 optical mouse and display (x,y) position data on either an LCD screen (see, e.g., C Example: Serial LCD or C Example: Parallel Interfacing with LCDs) or the PC (e.g., in Matlab or hyperterminal).

Overview

Ione Lynx-M9 mouse

The goal of the project is to interface the PIC184520 with a PS/2 mouse - specifically, an ione Lynx-M9 Wheel Optical mouse with a USB to PS/2 adapter. The mouse's XY coordinates will be displayed on an LCD screen and will be updated when the mouse moves.

The PS/2 connector is traditionally used to connect mice and keyboards to computer systems. The connector uses six pins: clock, data, 5V, GND and two that are not connected. The PS/2 connector is fully documented here: PS/2 connector. We used a Parallax PS/2 to Breadboard Adapter to connect the optical mouse to the PIC18. Documentation and ordering information for the adapter can be found here: Breadboard Adapter.

PS/2 to breadboard adapter

On this page you will find the steps to use a PS/2 mouse's output to meet your needs. The code, circuit diagram, and notes about the interface are included. This is currently a work in progress, but should be a good foundation for those looking to build on it and be able to use a PS/2 mouse in their own projects.

Mouse Interface












PS/2 Protocol

The PS/2 protocol uses two bidirectional communication lines between the system (in this case, the PIC) and the external device (the mouse). These lines are specified as DATA and Clock and allow for host-to-device transmission and device-to-host transmission. Data is sent as bytes with 11-12 bit frames (11 for device-to-host transmission and 12 for host-to-device). The frames are made up of: A start bit, eight data bits, a parity bit (odd), an end bit, and in the case of host-to-device transmission, an acknowledge bit. This data transmission takes place based on the clock signal generated by the external device.

This is a simplified description of the PS/2 protocol. More thorough/technical explanations can be found at the following links:

Computer Mouse Wiki

Documentation of PS/2 Protocol by Adam Chapweske and Roy Show

Description of PS/2 Protocol by Adam Chapweske

Circuit

The relevant part of the breadboard adapter data sheet can be seen below.

Breadboard adapter data sheet[1]










PS/2 circuit[2]

The mouse, as can be seen in the breadboard adapter schematic, now has four conduits indirectly attached to it. In order to operate, the mouse needs power, so the 5v and ground are used to power the mouse. The other two pins are actually used as both inputs and outputs. The mouse needs to first receive a signal before it can output a signal. The mouse also generates its own clock signal. More about this can be found in the PS2 protocol section.

Instead of using the resistors seen in the breadboard adapter schematic, it is actually preferable to use the PS/2 schematic shown at right because the PIC also needs to send data to the mouse.

The clock and data lines connect directly to the clock and data lines of the breadboard adapter. Note: there should be a resistor between the microcontroller outputs C/D and the base of the transistors. We also found that the buffers were unnecessary.

Basically, the microcontroller needs to be able to have two way communication with the mouse. So if the PIC wants to send data to the mouse, data will equal output D inverted, and Clock will equal output C inverted. Once the mouse starts sending data (and the outputs of C/D are set low), the clock and data lines are normally held at 5v, but are then set low if the mouse sends logic low. More information about this operation can be found here: PS2 Protocol

A great way to trouble shoot, debug, and see the final result is to use an LCD screen to display the information that the mouse is sending to the PIC. More information about LCDs and the flex code we used can be found here: C_Example:_Parallel_Interfacing_with_LCDs


A picture of our complete circuit can be found at right.

PS/2 to PIC circuit with LCD