Para mouse con conector PS2
pin 5 (CLOCK) -> Arduino digital 6
pin 1 (DATA) -> Arduino digital 5
pin 4 (VCC) -> Arduino VCC
pin 3 (GND) -> Arduino GND
Codigo Arduino:
#include <ps2.h>
PS2 mouse(6, 5);
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();
}
void loop()
{
char mstat;
int mx;
int my;
mouse.write(0xeb); // give me data!
mouse.read(); // ignore ack
mstat = mouse.read();
mx = mouse.read();
my = mouse.read();
if (mx != 0)
{
if (mx > 200)
Serial.println("L");
if (mx < 200)
Serial.println("R");
}
if (my != 0)
{
if (my > 200)
{
Serial.println("D");
}
if (my < 200)
Serial.println("U");
}
}
Codigo C#
*** Agregar un timer con intervalo 1 ***
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace Mouse3
{
public partial class MainForm : Form
{
SerialPort port = null;
bool U, D, L, R = false;
static int inc = 10;
public MainForm()
{
InitializeComponent();
}
void MainFormLoad(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
foreach (string p in ports)
{
port = new SerialPort(p, 9600);
if (port.PortName == "COM15")
break;
}
port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
}
void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int n = 0;
int count = port.BytesToRead;
while (count > 0)
{
n = port.ReadByte();
var letra = Convert.ToChar(n);
switch (letra)
{
case 'U': U = true; break;
case 'D': D = true; break;
case 'L': L = true; break;
case 'R': R = true; break;
}
count--;
}
}
void Timer1Tick(object sender, EventArgs e)
{
if (U)
{
pic1.Top-=inc;
U = false;
}
if (D)
{
pic1.Top+=inc;
D = false;
}
if (L)
{
pic1.Left-=inc;
L = false;
}
if (R)
{
pic1.Left+=inc;
R = false;
}
}
}
}
Para mouse con conector USB
Verde (CLOCK) -> Arduino digital 6
Blanco (DATA) -> Arduino digital 5
Rojo (VCC) -> Arduino VCC
Negro (GND) -> Arduino GND
El codigo para arduino es el mismo.
-------------------------------------------------------------------------------------------------------------
Fuentes:
http://playground.arduino.cc/ComponentLib/Ps2mouse
http://sohilpatel.org/arduino-ps2-mouse/
PARA MOUSE USB:
http://www.msx.org/forum/msx-talk/hardware/use-10eu-connect-modern-mouse-msx


No hay comentarios:
Publicar un comentario