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
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