Difference between revisions of "Interfacing with a mouse"

From Mech
Jump to navigationJump to search
Line 28: Line 28:
[[Image:Mi table.PNG|thumb|374px|center|Mouse Interface Table [http://www.allpinouts.org/index.php/Mouse/Keyboard_(PS/2)]]]
[[Image:Mi table.PNG|thumb|374px|center|Mouse Interface Table [http://www.allpinouts.org/index.php/Mouse/Keyboard_(PS/2)]]]
YV and XV are overflow flags for the motion in Y and X directions respectively where 0 means negative motion and 1 means positive motion. R is for the right button and L is for the left button where 1 equals pressed and 0 equal unpressed. X0-X7 and Y0-Y7 are binary outputs of 0 to 255, which represent how far the mouse has moved in its respective directions since the last sample (the least significant digit is on the right). Optical mice with wheels and additional buttons generally send a fourth data packet, but for this lab, that data was not needed.
YV and XV are overflow flags for the motion in Y and X directions respectively where 0 means negative motion and 1 means positive motion. R is for the right button and L is for the left button where 1 equals pressed and 0 equal unpressed. X0-X7 and Y0-Y7 are binary outputs of 0 to 255, which represent how far the mouse has moved in its respective directions since the last sample (the least significant digit is on the right). Optical mice with wheels and additional buttons generally send a fourth data packet, but for this lab, that data was not needed.

The mouse sends the data within the frames described in the PS/2 protocol section. This is a visual representation of a byte of information being sent from the mouse to the host:
[[Image:2 Clock data pic.PNG|thumb|374px|center|Byte of Data[http://www.computer-engineering.org/ps2protocol/]]]

When the clock is set low, the host will wait 10-25us before reading the data stream. More information on the mouse interface may be found at the following links:
[http://www.computer-engineering.org/ps2protocol/ Description of PS/2 Protocol by Adam Chapweske]
[http://www.allpinouts.org/index.php/Mouse/Keyboard_(PS/2) Description of Connector pinout for PS/2 Mouse/Keyboard]
== Circuit ==
== Circuit ==
The relevant part of the breadboard adapter data sheet can be seen below.
The relevant part of the breadboard adapter data sheet can be seen below.

Revision as of 01:24, 12 February 2009

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.

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

Mouse Interface

Normally, when a mouse first powers on, it will send its device ID information and set default values for its sample rate, resolution and scaling. The mouse also by default disables its ability to report data until it is sent an “Enable Data Reporting” command from the host (in this case the PIC). Once the “Enable Data Reporting” bits are sent, data reporting is enabled, the mouse can send data as long as the clock and data line are not held low by the host. Typically, a PS/2 mouse will send 3 bytes of data in three consecutive data packets as shown in the diagram below:

Mouse Interface Table [1]

YV and XV are overflow flags for the motion in Y and X directions respectively where 0 means negative motion and 1 means positive motion. R is for the right button and L is for the left button where 1 equals pressed and 0 equal unpressed. X0-X7 and Y0-Y7 are binary outputs of 0 to 255, which represent how far the mouse has moved in its respective directions since the last sample (the least significant digit is on the right). Optical mice with wheels and additional buttons generally send a fourth data packet, but for this lab, that data was not needed.

The mouse sends the data within the frames described in the PS/2 protocol section. This is a visual representation of a byte of information being sent from the mouse to the host:

Byte of Data[2]

When the clock is set low, the host will wait 10-25us before reading the data stream. More information on the mouse interface may be found at the following links: Description of PS/2 Protocol by Adam Chapweske Description of Connector pinout for PS/2 Mouse/Keyboard

Circuit

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

Breadboard adapter data sheet[3]










PS/2 circuit[4]

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