ps2 mouse example, help understanding

Hello,

I'm confused how the ps2 mouse example works using only two pins. (Perhaps because I'm really a beginner..) I thought that a pin could only be used for input or output, not bidirectionally... As they obviously can, what do i need to do to use them this way (ex: make sure the pins are low before i try to read)

Any help anyone can give me would be great (especially cause it would cut in half pins for the project i'd like to work on)

Thanks, Cosis

the example: http://www.arduino.cc/playground/uploads/ComponentLib/mouse.txt

A mouse is a write-only device - the host doesn’t talk to the mouse, the mouse just sends data to the host. The only reason it needs two wires is that PS/2 has a clock line. One way communication is simpler that bidirectional.

It is possible to use a single wire for bidirectional communications (and power, too); see Dallas 1-Wire. It’s a fair amount of work, IMO, compared to methods that use more wires.

It’s also possible to put multiple devices on a bus, like I2C, SPI, RS485, etc. This saves pins/wires.

-j

How then does it include both mouse_read and mouse_write functions?

Also, my question wasn’t about the ps/2 protocol (there are very easy references for that. the ps/2 protocol IS bidirectional, as is i2c, and many other protocols.), but rather about how arduino was able to use one pin as both the input and the output.

Any help on that point is much appreciated.

just as proof that it does both read and write, here is a section of the mouse_init() function:

// Serial.print(“Sending reset to mouse\n”);
mouse_write(0xff);
mouse_read(); /* ack byte /
// Serial.print(“Read ack byte1\n”);
mouse_read(); /
blank /
mouse_read(); /
blank /
// Serial.print(“Sending remote mode code\n”);
mouse_write(0xf0); /
remote mode /
mouse_read(); /
ack */

in the file you linked:

void gohi(int pin)
{
  pinMode(pin, INPUT);
  digitalWrite(pin, HIGH);
}

void golo(int pin)
{
  pinMode(pin, OUTPUT);
  digitalWrite(pin, LOW);
}

it appears that gohi() and golo() change the mode of the pin. I would have thought they changed the state of an output judging by the name. :|

-j

So I can change the mode of the pin while in the program? (and after setup) thats nice to know, because then i can set pins to write for writing things, and then use them as inputs when needed

Oh well, i was really just trying to understand how that was possible (for now its filed under 'it just is')

Since i'd like to interface a ps/2 keyboard, i guess I really only need to modify the loop and setup functions

Thanks, Cosis

the ps/2 protocol IS bidirectional

Sure enough. That's what I get for relying on my memory.

So I can change the mode of the pin while in the program?

Sure, assuming the physical arrangement of the hardware allows it. With 1-Wire, for example, you [u]must[/u] change from output to input in mid-transaction (well, I guess you could probably add components and use 2 pins, but that sorta defeats the purpose of 1-wire :) ).

(and after setup)

The "special" thing about setup is that it only gets ran once after the system starts. Most of the time you'll use a pin in only one direction, so setup() is a logical place to stick such code.

-j

ok, many thanks.

Knowing I can change pin mode outside of setup clarifies much about how that code works. The rest is probably just knowing the protocol extremely well.

Again, thanks for your help,
Cosis

I still can't understand the following:

void gohi(int pin) { pinMode(pin, INPUT); digitalWrite(pin, HIGH); }

whenever "gohi" is called, it will set pin as an input, and without changing the state of pin it will write on it by executing digitalWrite. Isn't it writing on an imput??? PLEASE HELP,.

regards.

I believe this sequence activates th internal pull-up resistor.

-j

hi! im a beginner and im trying to emulate a mouse with the arduino could someone feed me with info about connecting arduino to the PC via PS2 (because my board is a freeduino maxserial) :\ i saw the code and i think i can use the setup and loop from it only to send the data to the PC...

bi-directionality is usually done using a "pull up" resistor, either external and explicit, or internal to the AVR. When the pin is set as an input, the pull-up resistor pulls the line "high." Set the pin to an output and send a zero, and the line goes "low."