我一直在使用此消息底部的鼠标草图(由其他人编写)来尝试从 PS/2 鼠标中获取运动数据。我检查了这款鼠标的规格,上面说它是 PS/2 兼容的。但是,当我运行它时,它似乎停在 mouse_init 的第一行,上面写着“mouse.write(0xff); // reset”。这是对 ps2.h 中函数的调用。ps2.h 自 2008 年以来一直存在,并已在许多项目中使用,所以我认为它没问题,但我想知道 USB 鼠标连接为 PS/2 鼠标时是否有一些特殊的功能,而这个库从来没有旨在应对。有没有人有任何经验可以说明这一点?
我已经能够确定 mouse.write 正在改变我的 Genius 鼠标的状态,但它卡在了鼠标应该将时钟状态拉低以便主机可以继续传输数据的点。在 mouse.write 开始之前,时钟状态是低的,但是它被主机推高了几行到 mouse.write 并停留在那里。鼠标再也不会把它拉低了。任何关于问题可能是什么的想法将不胜感激。
#include <ps2.h>
/*
* an arduino sketch to interface with a ps/2 mouse.
* Also uses serial protocol to talk back to the host
* and report what it finds.
*/
/*
* Pin 5 is the mouse data pin, pin 6 is the clock pin
* Feel free to use whatever pins are convenient.
*/
PS2 mouse(6, 5);
/*
* initialize the mouse. Reset it, and place it into remote
* mode, so we can get the encoder data on demand.
*/
void mouse_init()
{
mouse.write(0xff); // reset
mouse.read(); // ack byte
mouse.read(); // blank */
mouse.read(); // blank */
mouse.write(0xf0); // remote mode
mouse.read(); // ack
delayMicroseconds(100);
}
void setup()
{
Serial.begin(9600);
mouse_init();
}
/*
* get a reading from the mouse and report it back to the
* host via the serial line.
*/
void loop()
{
char mstat;
char mx;
char my;
/* get a reading from the mouse */
mouse.write(0xeb); // give me data!
mouse.read(); // ignore ack
mstat = mouse.read();
mx = mouse.read();
my = mouse.read();
/* send the data back up */
Serial.print(mstat, BIN);
Serial.print("\tX=");
Serial.print(mx, DEC);
Serial.print("\tY=");
Serial.print(my, DEC);
Serial.println();
// delay(20); /* twiddle */
}