/************************************************ Le mouse PS/2 LabProc 2009-1 Marcos Augusto Stemmer *************************************************/ #include #include "uart.h" #define LCD_E 1 #define LCD_RS 2 #define PS2_CP 0x40000000 #define PS2_D 0x80000000 int tempo; void barra(int x); void PrDig(char c) { c = '0' + (c & 0xf); if(c > '9') c+='A'-'9'-1; UART0putchar(c); } /* Escreve um byte como 2 digitos hexadecimais */ void PrByte(char c) { PrDig(c >> 4); PrDig(c); } int lescan(void) { int k, scan; do { while(!(FIO4PIN3 & 0x40)); /* Espera enquanto KB_CP==0 */ while(FIO4PIN3 & 0x40){ /* Espera enquanto KB_CP==1 */ } } while(FIO4PIN3 & 0x80); /* O start bit deve ser 0 */ scan=0; k=8; do { /* 8 bits de dados */ while(!(FIO4PIN3 & 0x40)); /* Espera subir CP */ while(FIO4PIN3 & 0x40); /* Espera descer CP */ scan = (scan >> 1)+(FIO4PIN3 & 0x80); }while(--k); while(!(FIO4PIN3 & 0x40)); /* Le o bit de paridade */ while(FIO4PIN3 & 0x40); while(!(FIO4PIN3 & 0x40)); /* Le o stop bit */ while(FIO4PIN3 & 0x40); return scan; } void escreve_bit(int b) { while(!(FIO4PIN3 & 0x40)); /* Espera subir CP */ while(FIO4PIN3 & 0x40); /* Espera descer CP */ FIO4DIR &= ~(PS2_CP | PS2_D); if(!(b & 1)) { FIO4DIR |= PS2_D; FIO4CLR3 = 0x80; } } void escreve_ps2(int c) { int par, k; while(!(FIO4PIN3 & 0x40)); /* Le o stop bit */ FIO4DIR |= PS2_CP; /* CP como saida */ FIO4CLR = PS2_CP; /* CP=0 */ k=T0TC+2; while(k!=T0TC); FIO4DIR |= PS2_D; FIO4CLR = PS2_D; /* DATA=0 */ FIO4DIR &= ~PS2_CP; k=8; par=1; do { escreve_bit(c); par ^= c; c >>= 1; } while(--k); escreve_bit(par); escreve_bit(1); while((FIO4PIN3 & 0xc0)!=0xc0); /* Espera subir CP */ } /* Escreve um numero decimal */ void escrevenum(int num) { char digs[16]; int nd=0; if(num<0) { LCDputchar('-'); num=-num; } do { digs[nd++]= (num % 10) + '0'; num /= 10; } while(num); while(nd) LCDputchar(digs[--nd]); } int main (void) { int a, x, y, dx, dy; LCDinit(); UART0_Init(); LCDcomando(0x80); T0TCR = 0; T0PR = 12000000/10000 - 1; T0TCR = 2; T0TCR = 1; escreve_ps2(0xff); /* Reset */ a=lescan(); dx=lescan(); dy=lescan(); PrByte(a);PrByte(dx);PrByte(dy); UART0putchar('\n'); escreve_ps2(0xf6); /* Inicializa com defaults */ a=lescan(); PrByte(a); UART0putchar('\n'); escreve_ps2(0xf3); /* Config. Amostras/segundo */ a=lescan(); PrByte(a); UART0putchar('\n'); escreve_ps2(10); /* 10 amostras/seg */ a=lescan(); PrByte(a); UART0putchar('\n'); escreve_ps2(0xe8); /* Resolucao */ a=lescan(); PrByte(a); UART0putchar('\n'); escreve_ps2(0); /* 1 contagem/mm */ a=lescan(); PrByte(a); FIO4DIR |= 0xff; FIO4SET = 0xff; escreve_ps2(0xf4); /* Inicia o funcionamento do mouse */ a=lescan(); PrByte(a); UART0putchar('\n'); x=y=0; FIO4DIR |= 1; do { a=lescan(); dx=lescan(); dy=lescan(); if(a & 0x10) dx |= -256; if(a & 0x20) dy |= -256; x += dx; y += dy; LCDcomando(0xc0); LCDputchar(' '); UART0putchar('\r'); escrevenum(x); LCDputchar(' '); escrevenum(y); LCDputchar(' '); barra((x >> 2)); FIO4PIN0 = (~128) >> ((y>>4) & 7); } while(1); return 0; } /* Estas rotinas sao chamados pelo crt.S. Devem existir, mas ainda nao estao sendo usadas */ void UNDEF_Routine(){} void SWI_Routine(){} void FIQ_Routine(){}