Init Mouse

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Init Mouse as PDF for free.

More details

  • Words: 455
  • Pages: 4
#include #include <delay.h> void portinit(); void uart_init(); void transmit(unsigned char x); void host_to_mouse(unsigned char packet); char ps2_init(); void setdataclock(unsigned char datastate, unsigned char clockstate); bit calparity(unsigned char data); #define bitset(var,bitno) ((var)|=1<<(bitno)) #define bitclr(var,bitno) ((var)&=~( 1<<(bitno))) #define #define #define #define

clk_low() rb6=0,bitclr(trisb,6) clk_release() bitset(trisb,6) data_low() rb7=0,bitclr(trisb,7) data_release() bitset(trisb,7)

char ps2_data_in;

bit calparity(unsigned char data) { bit parity=1; unsigned char i; for(i=0;i<8;i++) { parity=parity^(data<
void uart_init() { txsta=0x22;//baud low speed. spbrg=0x0c; //baud rate 19200 rcsta=0x90; //intcon=0xc0; option=0xc0; //pie1=0x20; } void portinit() { trisb=0xff; trisa=0xff; portb=0x00; trisd=0x00; trise=0x07; trisc=0x80; }

void transmit(unsigned char x) { while(txif==0); txreg=x; } void setdataclock(unsigned char datastate,unsigned char clockstate) { if(datastate) bitset(trisb,7);//set tris register bit associated with data line to input else { rb7=0; bitclr(trisb,7);//set tris register bit associated wtih data line to output } if(clockstate) bitset(trisb,6) ;//set tris register bit associated with clock line to input else { rb6=0; bitclr(trisb,6);//set tris register bit associated wtih clock line to output } }

void host_to_mouse(unsigned char packet) { unsigned char i=0; bit parity; ps2_data_in=0; do { setdataclock(1,0); //pull clock low arity = calcparity(packet); delayus(300); //must be greater than 100us setdataclock(0,0); //pull data low setdataclock(0,1); //release clock - device will start generating clock while(!rb6){} //wait for rising edge while(rb6){} //wait for falling edge for(i=0;i<8;i++) { senddataclock((packet<
transmit('<'); transmit(ascii[0]); transmit(ascii[1]); transmit('>'); }while(ps2_data_in!=0xfa); }

bit mouse_to_host(void) { unsigned char temp[8]={0},i; unsigned int a; setdataclock(1,0); //pull clock line low to inhibit communication delayus(100); //must be greater than 100us setdataclock(1,1); //release clock line, data line ready for input while(!rb6){} //wait for clock high for(a=0; a<100; a++) { if((!rb6)&&(!rb7)) //wait for data and clock to be low break; if(a==99) return 0; } for(i=0;i<8;i++) { while(!rb6){} //wait for clock high while(rb6){} //wait for clock low (falling edge); temp[i]=rb7; // store each bit } while(!rb6){} while(rb6){} while(!rb6){} while(rb6){} //wait for last 2 clock pulses (parity and stop) /* following statment combines the collected bits into byte*/ packet=128*temp[7]+64*temp[6]+32*temp[5]+16*temp[4]+8*temp[3]+4*temp[2]+2*temp[1]+ temp[0]; ps2_data_in=packet; return 1; } //function returns 0 if mouse initializes properly //returns 1 if mouse error (0xfc) is received //returns 2 if other code received char ps2_init() { char temp; //sr=4; //set default value host_to_mouse(0xff); //reset command while(!mouse_to_host()){} //mouse init results temp=ps2_data_in; while(!mouse_to_host()){} //mouse id if(temp==0xaa) //0xaa = mouse initialized ok (self test) { host_to_mouse(0xf3); host_to_mouse(10); host_to_mouse(0xf4);//enable data reporting, begin operation

}

} else if(temp==0xfc) return 1; else return 2; return 0;

//0xfc = mouse had init error //code other than 0xaa or 0xfc returned by mouse

void main() { portinit(); uart_init(); while(ps2_init()!=0){} //function returns 0 when init goes ok }

Related Documents

Init Mouse
October 2019 5
Mouse
April 2020 20
Mouse
May 2020 17
Mouse
November 2019 34
Mouse
April 2020 12
Mouse
April 2020 23