sábado, 18 de enero de 2014

Teclado PS2


#include <16F877A.h>
#use delay(clock=20M)
#fuses HS,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP
#zero_ram
#include <lcd.c>

unsigned char edge, bitcount;
char got_interrupt;
char interrupt_count;
char status_b3;
#bit INTF_BIT = 0x0B.1

unsigned char const unshifted[68][2] = {
0x0d,9,
0x0e,'º', 0x15,'q', 0x16,'1', 0x1a,'z', 0x1b,'s', 0x1c,'a', 0x1d,'w',
0x1e,'2', 0x21,'c', 0x22,'x', 0x23,'d', 0x24,'e', 0x25,'4', 0x26,'3',
0x29,' ', 0x2a,'v', 0x2b,'f', 0x2c,'t', 0x2d,'r', 0x2e,'5', 0x31,'n',
0x32,'b', 0x33,'h', 0x34,'g', 0x35,'y', 0x36,'6', 0x39,',', 0x3a,'m',
0x3b,'j', 0x3c,'u', 0x3d,'7', 0x3e,'8', 0x41,',', 0x42,'k', 0x43,'i',
0x44,'o', 0x45,'0', 0x46,'9', 0x49,'.', 0x4a,'-', 0x4b,'l', 0x4c,'ñ',
0x4d,'p', 0x4e,''', 0x52,'´', 0x54,'`', 0x55,'¡', 0x5a,13, 0x5b,'+',
0x5d,'ç', 0x61,'<', 0x66,8, 0x69,'1', 0x6b,'4', 0x6c,'7', 0x70,'0',
0x71,'.', 0x72,'2', 0x73,'5', 0x74,'6', 0x75,'8', 0x79,'+', 0x7a,'3',
0x7b,'-', 0x7c,'*', 0x7d,'9',
0,0
};

unsigned char const shifted[68][2] = {
0x0d,9,
0x0e,'ª', 0x15,'Q', 0x16,'!', 0x1a,'Z', 0x1b,'S', 0x1c,'A', 0x1d,'W',
0x1e,'"', 0x21,'C', 0x22,'X', 0x23,'D', 0x24,'E', 0x25,'$', 0x26,'·',
0x29,' ', 0x2a,'V', 0x2b,'F', 0x2c,'T', 0x2d,'R', 0x2e,'%', 0x31,'N',
0x32,'B', 0x33,'H', 0x34,'G', 0x35,'Y', 0x36,'&', 0x39,'L', 0x3a,'M',
0x3b,'J', 0x3c,'U', 0x3d,'/', 0x3e,'(', 0x41,';', 0x42,'K', 0x43,'I',
0x44,'O', 0x45,'=', 0x46,')', 0x49,':', 0x4a,'_', 0x4b,'L', 0x4c,'Ñ',
0x4d,'P', 0x4e,'?', 0x52,'¨', 0x54,'^', 0x55,'¿', 0x5a,13, 0x5b,'*',
0x5d,'Ç', 0x61,'>', 0x66,8, 0x69,'1', 0x6b,'4', 0x6c,'7', 0x70,'0',
0x71,'.', 0x72,'2', 0x73,'5', 0x74,'6', 0x75,'8', 0x79,'+', 0x7a,'3',
0x7b,'-', 0x7c,'*', 0x7d,'9',
0,0
};

void init_kb(void);
void decode(unsigned char sc);

#int_ext
void int_ext_isr(void){
  unsigned char data;
  if(bitcount < 11 && bitcount > 2){
    data = (data >> 1);
    status_b3 = input(PIN_B3);
    if((status_b3) == 1){
      data = data | 0x80;
   }
  }
  if(--bitcount == 0){
    decode(data);
    data = 0;
    bitcount = 11;
    got_interrupt = TRUE;
  }
  got_interrupt = TRUE;
  interrupt_count++;
  disable_interrupts(INT_EXT);
}


void main(void)
{

  lcd_init();
  delay_ms(100);
  init_kb();
 
  output_float(PIN_B0);//clock
  output_float(PIN_B3);//data
  port_b_pullups(TRUE);
  delay_us(5);
  interrupt_count = 0;
  got_interrupt = FALSE;
  status_b3 = 0;
  ext_int_edge(H_TO_L);
  INTF_BIT = 0;
  enable_interrupts(INT_EXT);
  enable_interrupts(GLOBAL);

  while(true){
    if(got_interrupt == TRUE){
      got_interrupt = FALSE;
      INTF_BIT = 0;
      enable_interrupts(INT_EXT);
    }
  }
}

void init_kb(void){
  bitcount = 11;
  lcd_gotoxy(1,1);
  printf(lcd_putc,"connected ..." );
}

void decode(unsigned char sc){
  static unsigned char is_up=0, shift = 0, mode = 0;
  unsigned char i;
 
   lcd_gotoxy(1,1);
   printf(lcd_putc,"               " );
   lcd_gotoxy(1,1);
   printf(lcd_putc,"Num [%X]",sc);

  if (!is_up){
    switch (sc){
      case 0xF0 :
        is_up = 1;
        break;
      case 0x12 :
        shift = 1;
        break;
      case 0x59 :
        shift = 1;
        break;
      case 0x5A :
        shift = 0;
        //printf("\n\r" );
        break;
      default:
        if(!shift){
          for(i = 0; unshifted[ i ][ 0 ]!=sc && unshifted[ i ][ 0 ]; i++);
          if (unshifted[ i ][ 0 ] == sc){
            lcd_gotoxy(1,2);
            printf(lcd_putc,"               " );
            lcd_gotoxy(1,2);
            printf(lcd_putc,"Car <%c>", unshifted[i][1]);          
          }
        } else {
         for(i = 0; shifted[ i ][ 0 ]!=sc && shifted[ i ][ 0 ]; i++);
         if (shifted[ i ][ 0 ] == sc){
            lcd_gotoxy(1,2);
            printf(lcd_putc,"               " );
            lcd_gotoxy(1,2);
            printf(lcd_putc,"Car <%c>", shifted[i][1]);          
         
         }
       }
       break;
     }
   } else {
      is_up = 0;
      switch (sc){
        case 0x12 :
          shift = 0;
          break;
        case 0x59 :
          shift = 0;
          break;
      }
    }
}

No hay comentarios:

Publicar un comentario