Nov 20, 2004 The "Seeing-Eye Mouse" v1.0 PS/2 Mouse-Reading PIC Design by Don Papp http://www.imakeprojects.com theprojectmaker@imakeprojects.com PIC Programming done by Evan Dudzik (evandude@gmail.com) OVERVIEW -------- The purpose of this PIC is to allow the use of a generic PS/2 mouse(*) as a general purpose input device or simple navigation aid. It reads the data from the mouse, interprets it, and presents the data on the PIC's outputs in one of several formats depending on the user's selection. One of the points of this PIC is the ability to turn the raw mouse data (which transmits a +/- X/Y change from last transmit) into something with a little more bearing on the real world. (*) NOTE: USB mice wired as PS/2 mostly work, but at least one I have used (Microsoft optical USB mouse with PS/2 adapter) does not. Also note: The mouse can be either optical or a ball mouse. However, do not use a ball mouse if the mouse will be moving over about 6"/second. (The ball will skip.) BASIC OPERATION --------- A simple use of the PIC would be to read a mouse's motion and make HIGH one (or more) of four outputs: FORWARD, BACK, LEFT, RIGHT. So moving the mouse Forward would make FORWARD = HIGH. Moving it to the Rear and Left would result in BACK = HIGH and LEFT = HIGH. Leaving the mouse motionless results in all four outputs LOW. The exact behaviours of the outputs depends on the output mode selected, but this example should give you the general idea. In addition to the outputs FORWARD, BACK, LEFT, RIGHT (F,B,L,R), there is one RS232 output. Its exact function also depends on the mode selected by the user, but typically it outputs a repeating packet of data (ASCII numbers) followed by a comma (',') at 9600 baud. This packet represents the F,B,L,R data in some way (for use as an alternate means for a host to read the output) and also includes the mouse button states (M0, M1, M2 - not all 3 of which are always physically present on a mouse). It would be typical for the PIC to output the following in a simple mode of operation: Mouse movement: FORWARD and RIGHT Button states: M0 (button 0) depressed F = HIGH B = LOW L = HIGH R = LOW RS232 format: , RS232 Output = "1010100," (quotes are not transmitted) The outputs are updated synchronously and as often as possible - preferably at least every 100ms. (Digital outputs reflect changes in mouse movement, and RS232 output repeats constantly with changes appearing in the serial packet as soon as they are available.) INPUTS / OUTPUTS ---------------- RA0 = PS/2 DATA (pic has internal pullups enabled) RA1 = PS/2 CLK (pic has internal pullups enabled) RA2 = threshold0 (input) (pic has internal pullups enabled) RA3 = threshold1 (input) (pic has internal pullups enabled) RA4 = /resolution_doubler (active low) input (pic has internal pullups enabled) BA0 = F (Forward) output BA1 = B (Backward) output BA2 = L (Left) output BA3 = R (Right) output BA4 = RS232_Out (output) TTL level positive polarity (need inverter to talk to PC) BA5 = mode0 (input) no internal pullup BA6 = mode1 (input) no internal pullup BA7 = /rs232_transmit_enable (input) no internal pullup SOFTWARE -------- The software turns mouse movement into velocities (000-127) which roughly represents 0"/sec to 12"/sec. From Evan: technically 4 counts per mm but it never actually comes out to that. The mouse really is not made for absolute distance measurement - it's for relative motion/speed detection. 'counts' * 9 / 15 = velocity scaled to 000-127 to represent 0-12"/sec Top end of detection is 12"/sec (ideally). PIN DESCRIPTIONS ---------------- thresh0, thresh1 Sets the detection threshold - velocities under this do not trigger the FBLR outputs 00 = 0 01 = 5 10 = 10 11 = 15 (Recall velocities are 000-127, equal roughly to 0-12"/sec) /rs232_transmit_enable When 0, this allows the PIC to transmit regular ASCII packets **************** WHEN A LOT OF RS232 IS DONE, SOME PACKETS MIGHT BE LOST FROM THE MOUSE IN OTHER WORDS, A BUTTON HIT/RELEASE MAY BE UNNOTICED BY THE PIC. **************** FBLR Digital outputs. They output binary out, or PWM /resolution_doubler Experimental -- normally this pin is unconnected. Good candidate for reprogramming to something else. When it is active (LOW), it halves all detected velocities. (So techically /resolution_halver). It is most useful (if at all useful) for effectively halving the sensitivity selected by thresh0 and thresh1. So if you are in MODE 00, tying this LOW will mean the sensitivity is halved, meaning the mouse needs to move twice as fast in order to light up corresponding FBLR pin(s). TX RS232 out. Requires inverter to talk to a PC. TTL levels. DATA Data in from a mouse. (or D+ from a USB mouse) CLK Clock in from a mouse. (or D- in from a USB mouse) mode0, mode1 These pins set the operation mode of the pic. All outputs are continually updated to reflect current mouse status, and in the case of RS232 outputs, the output packet is continually repeated with the current information. This allows polling the RS232 data by waiting for a "," then reading values until the next "," and interpreting them based on the current output mode. RS232 output packets can be suppressed by making /rs232_transmit_enable HIGH. (Or, to think of it another way, packets can be requested by making this pin LOW). MODE 00 FBLR Outputs: Each of F,B,L,R are HIGH for as long as there is movement in that direction(s) with a speed in excess of THRESHOLD (see below). RS232 Output: "," where 0 or 1 for each direction, or 0/1 for button pushes. RS232 output (no quotes): "1001100," for forward and right, button 0 pushed. MODE 01 FBLR Outputs: PWM out for servo movement. FB and LR output the same signal (pairs). For example, servo to L or R results in move left/right moving servo left/right in proportion to the mouse velocity. RS232_Out outputs an ASCII representation of each direction's VELOCITY (000-127), as well as the state of any mouse buttons (1 = pressed, 0 = not pressed). RS232_Out format: "," The RS232 information is only semi-accurate, and is supposed to zero back after movement is ceased. MODE 10 This mode outputs a cumulative count of each direction's VELOCITY every 200ms over RS232_Out (along with button states). F,B,L,R outputs are the same as for MODE 00 (HIGH on movement in excess of THRESHOLD). The purpose of this mode is to provide numbers suitable for simple estimations of distances and directions travelled. Every 200ms, each direction's current VELOCITY is added to one of four respective 16-bit (0-65535) counters (FORWARD_COUNT, BACKWARD_COUNT, LEFT_COUNT, RIGHT_COUNT) and a data packet is sent out the RS232_Out pin reflecting this information. These counters are reset to 0 upon this mode being made active. The counters need only be incremented with direction velocities when this mode is active. The host may enter this mode, travel a short distance forward (verifying the direction being moved in by looking at the "F" output, subject to the threshold value), then read the FORWARD_COUNT portion of the RS232 output to get a GENERAL idea of how far it has moved. Alternately, the host may enter this mode, travel around randomly, then compare the COUNTS of all four directions to get a VERY general idea of how much raw actual movement was made. THIS IS NOT DEAD-RECKONING. IT IS ONLY A COUNT OF THE PAST VELOCITIES. NUMBERS WILL NOT NECESSARILY BE THE SAME BETWEEN IDENTICAL DISTANCES. When any one of the 16-bit counters is full, it rolls ALL counters back to 00000 and outputs the next RS232 packet with "99999" for all the COUNTER values to indicate a rollover has occured. It is the host's responsibility to notice this rollover has happened. (The counters are still incremented normally internally when the "99999" is output to indicate a rollover. It's just that the output is overridden to "99999" for each counter after resetting each to 00000.) The counter may be intentionally reset smoothly by entering MODE 11 (see below). F,B,L,R outputs: Same as MODE 00 (HIGH if the VELOCITY in that direction EXCEEDS the value in THRESHOLD). RS232_Out format: 5-digit (padded leading zeroes) for each direction's 16-bit counter, followed by button states. "," MODE 11 This is the same as mode 10. Provides a way to reset the counters while still staying in the same mode. ---------- This work is licensed under the Creative Commons Attribution-NonCommercial2.5 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/2.5/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.