Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to interface ps/2 mouse wih atmel 8051 microcontroller

Status
Not open for further replies.

user1111

Member level 3
Joined
Mar 24, 2007
Messages
55
Helped
7
Reputation
14
Reaction score
2
Trophy points
1,288
Activity points
1,576
psclock psdata

also is it possible to communicate ps/2 mouse in serial mode?
 

Re: how to interface ps/2 mouse wih atmel 8051 microcontroll

hi,
about the serial mode. I think its not possible... the ps2 use an extrange 10-11 bit shincronous data packet, where the clock is generated by the mouse!!!

I used RC51 for this routines... hope you find them useful...


----------------------------------------------------
sbit psDATA = P1^0;
sbit psCLOCK = P1^1;


void ps2Inhibit()
{
psDATA = 1;
psCLOCK=0;
}

bit ps2Rb()
{
bit c;
while(psCLOCK);
c=psDATA;
while(!psCLOCK);
return c;
}
char ps2Rx()
{
char i=8,a=0;
psCLOCK=1;
ps2Rb();
do {
a >>= 1;
if (ps2Rb()) a |= 0x80;
} while (--i);

ps2Rb(); //bit: parity...
ps2Rb(); //bit: stop...
return a;
}

void ps2Tb (bit c)
{
while(psCLOCK);
psDATA=c;
while(!psCLOCK);
}

void ps2Tx (char dato)
{
char i=50,pari=0;
psCLOCK=0;
while(--i); //aprox 100us
psDATA=0; //bit: start. (RTS)
i=8;
psCLOCK=1; // RTS...
do {
if (dato & 1) {ps2Tb(1); pari++;}
else ps2Tb(0);
dato >>=1;
} while(--i);

(pari & 1)? ps2Tb(0) : ps2Tb(1); //bit par
ps2Tb(1);
while(psCLOCK);
while(!psCLOCK);
}

char Tipo;
signed char mouse_X=0;
signed char mouse_Y=0;
signed char mouse_Z=0;

void init_MOUSE()
{
ps2Tx(0xFF); //reset
ps2Rx(); //Acknoledge
ps2Rx(); //self Test = AAh
ps2Rx(); //mouse ID = 00h
ps2Tx(0xF3); //sampling...
ps2Rx(); //-Acknoledge
ps2Tx(0xC8); // 200
ps2Rx(); // Acknoledge

ps2Tx(0xF3); //sampling...
ps2Rx(); // Acknoledge
ps2Tx(0x64); // 100
ps2Rx(); // Acknoledge

ps2Tx(0xF3); //sampling...
ps2Rx(); //Acknoledge
ps2Tx(0x50); //80
ps2Rx(); //Acknoledge

ps2Tx(0xF2); //ID
ps2Rx(); //Acknoledge
Tipo = ps2Rx(); //mouse ID =00 normal 03 intelli

ps2Tx(0xF3); //final sampling...
ps2Rx(); // Acknoledge
ps2Tx(0x0A); // 10 (je je je I'm slow...)
ps2Rx(); // Acknoledge

ps2Tx(0xE8); //Res...
ps2Rx(); // Acknoledge
ps2Tx(0x00); //1 count/mm
ps2Rx(); // Acknoledge

}

char mouse_STAT;
signed char mouse_dX;
signed char mouse_dY;
signed char mouse_dZ;

void read_mouse ()
{
ps2Tx(0xEB); // Data
ps2Rx(); // Acknoledge
mouse_STAT=ps2Rx();
mouse_dX = ps2Rx();
mouse_dY = ps2Rx();
if (Tipo) mouse_dZ = ps2Rx();
//abosolute pos...
mouse_X +=mouse_dX;
mouse_Y +=mouse_dY;
if (Tipo) mouse_Z +=mouse_dX;
}

---------------------------------------------------


now first you should call

init_MOUSE();

then

read_mouse();

to read values in mouse_X, mouse_Y And maybe mouse_Z (also the mouse_dX, and so.. are useful...)

hope you already searched for the PS/2 format and the PS/2 mouse commands...


cheers!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top