#include "ps2mouse.c"
// D7 D6 D5 D4 D3 D2 D1 D0
//1st YV XV YS XS 1 0 R L
//2nd X7 X6 X5 X4 X3 X2 X1 X0
//3rd Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0
//synchronous
//L Left Button State (1 = pressed down)
//R Right Button State (1 = pressed down)
//X0-X7 Movement in the X direction
//Y0-Y7 Movement in the Y direction
//XS Direction of the movement in the X axis (1 = UP)
//YS Direction of the movement in the Y axis (1 = LEFT)
//XV,YV Overflow of the movement data bits (1 = overflow has occured)
//only send if state changes
unsigned short byte0,byte1,byte2;
void main ()
{
//0=lb,1=dt,2=ck,3=rb,4=X+,5=X-,6=y+,7=y-
cmcon=7;//turn off comparators
trisb=0xff;
PORTB=0;
txsta.brgh =1;
spbrg=0x33;//need to set baud rate
init9();
do{
byte0.f0=PORTB.f0; //L
byte0.f1=PORTB.f3; //R
if(PORTB.f4==PORTB.F5)byte1=0;
else
{
byte1=20;
if(PORTB.f4)byte0.f4=1; //XS=up
if(PORTB.f5)byte0.f4=0; //XS=down
}
if(PORTB.f6==PORTB.F7)byte2=0;
else
{
byte2=20;
if(PORTB.f6)byte0.f5=1; //YS=left
if(PORTB.f7)byte0.f5=0; //YS=right
}
byte0.f2=0; //D2 unused
byte0.f3=1; //D3
byte0.f6=0; //no overflow
byte0.f7=0; //no overflow
if(byte1||byte2||byte0.f0||byte0.f1)
{
send9(byte0);
send9(byte1);
send9(byte2);
}
}while(1);
}
//ps2mouse.c
void parity(unsigned short byte);
void init9()
{
//ports
// baud rate
txsta.sync=1;//synchrous mode
rcsta.spen=1;//set clk and dt lines Serial Port ENable
txsta.csrc=1;//master mode
txsta.tx9=1;//select 9 bit
}
void send9(unsigned short byte)
{
parity(byte);
txsta.txen=1;
txreg=byte;
asm{
btfss PIR1,4 //check transmit bit txif
goto $-1
}
//while(!pir1.txif);//wait for transmission
//while(!txsta.trmt);//wait for transmission
txsta.txen=0; //turn off transmission
}
unsigned short rcv9(void)
{
}
void parity(unsigned short byte)
{
asm{
swapf FARG_parity, w
xorwf FARG_parity, f
rrf FARG_parity, w
xorwf FARG_parity, f
btfsc FARG_parity, 2
incf FARG_parity, f
}
txsta.tx9d=!byte.f0; //put bit in 9th bit register
}