Autor Tema: Codigo para leer teclado 4x4  (Leído 3323 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado PerriPic

  • PIC10
  • *
  • Mensajes: 24
Codigo para leer teclado 4x4
« en: 05 de Diciembre de 2007, 08:00:42 »
A ver si alguien me puede ayudar con este código q no consigo q funcione correctamente para leer un tecla de un teclado 4x4:

#include <p18f2550.h>
#include <delays.h>            // Rutinas de retardo

char Lee_Tecla(void)
{
   char tecla,Temp,Temp1,Temp2;
   TRISB=0xF0;
   INTCON2=INTCON2&0x7F; // bit INTCON2<7>=#RBPU habilita pull-ups
   PORTB=0x00;
   Temp=0x00;
   Temp1=PORTB; // Comprueba la fila
   while ((Temp1 & 0x10) && (Temp<4)) //cuenta hasta llegar a un bit a '1'
   {
      Temp1>>=1;
      Temp++;
   }
   if (Temp<4) //Fila correcta; se ha pulsado tecla
   {
      Temp2=Temp; //Guardo Fila
      TRISB=0x0F;
      INTCON2=INTCON2&0x7F;
      PORTB=0x00;
      Temp=0x00;
      Temp1=PORTB; // Comprueba la columna
      while ((Temp1 & 0x01) && (Temp<4)) // cuenta hasta llegar a ‘1’
      {
         Temp1>>=1;
         Temp++;
      }
      if (Temp<4) //Columna correcta; cálculo la tecla…
      {
         Temp1=Temp;
         Temp2=(Temp2<<2)+Temp1;
         tecla=Temp2;
         return(tecla);
      }
      return(0xFF);
   }
   return(0xFF);
}

void main(void)
{
   //ADCON1=0x0F;
   //TRISB=0x00;
   while(1)
   {
      PORTB=Lee_Tecla();
      Delay1KTCYx(10);      //retraso 10ms;
   }
}

Desconectado gilbert

  • PIC10
  • *
  • Mensajes: 4
Re: Codigo para leer teclado 4x4
« Respuesta #1 en: 14 de Diciembre de 2007, 11:00:07 »
hola amigo soy nuevo en  este foro y espero aportar en algo...
buano aca ise un codigo para leer un teclado de 4x4 es una libreria (KBD_calc.c) la cual funciona correctamente
para que funciona se debera poner en el programa principal:
kbd_init();
tecla=kbd_getc(); este te debolvera la tecla pulsada en caracter.

una ves pulsada la tecla se esperara a que se suelte y asi evita rebotes...

codigo:

//este es el codigo de lka libreria.

#byte PORTB = 6
char tecla_get(void);
char conprueva(void);
char tecla,key2,tempo1;
int i,key1,tecla1,tecla2=0b01111111;
void kbd_init()
{
set_tris_b(0x0f);
}
char kbd_getc()   //devuelve el valor de la tecla
{
  tecla=tecla_get();
  tecla=conprueva();
  i=0;
 
 switch(tecla)
 {
   case 0xeb: return ('1');
              break;
   case 0xdb: return ('2');
              break;
   case 0xbb: return ('3');
              break; 
   case 0xed: return ('4');
              break;
   case 0xdd: return ('5');
              break;
   case 0xbd: return ('6');
              break;
   case 0xee: return ('7');
              break;
   case 0xde: return ('8');
              break;
   case 0xbe: return ('9');
              break;
   case 0xd7: return ('0');
              break;
   case 0x7d: return ('x');
              break;
   case 0x7e: return ('/');
              break;
   case 0x7b: return ('-');
              break;
   case 0x77: return ('+');
              break;
   case 0xb7: return ('=');
              break;
   case 0xe7: return ('@');
              break;
   case 0x80: return (0x80);
              break;
   default:   return ('E');
              break ;
 }
}

char tecla_get()   //captura elk valor de la tcla pulsada
{
tecla2=0b01111111;
  for(i=0;i<4;i++)
  {
     #asm
      movf tecla2,w
      movwf PORTB
     #endasm
     delay_us(3);
     #asm
      movf PORTB,w
      movwf key2
     #endasm
 
    tecla1=tecla2-key2;
     
     if(tecla1!=0)
     {
       delay_ms(20);
       #asm
        movf tecla2,w
        movwf PORTB
        #endasm
       delay_us(3);
       #asm
        movf PORTB,w
        movwf key2
       #endasm
       
       tecla1=tecla2-key2;
       
        if(tecla1!=0)
        {
         return (key2);
        }
    }
   rotate_right(&tecla2,1);
 } 
 return (0x80);
}

char conprueva()   //espera hasta que la teclas se suelte
{
  tecla1=tecla-0x80;
  if(tecla1!=0)
  {
   tempo1=tecla;
   do
   {
     tecla=tecla_get();
     i=0;
     tecla1=tecla-0x80;
     if(tecla1==0)
      {
       return (tempo1);
      }
   }while(TRUE);
  }
  return (0x80);


//fin de la libreria.
« Última modificación: 14 de Diciembre de 2007, 11:14:29 por gilbert »

Desconectado gilbert

  • PIC10
  • *
  • Mensajes: 4
Re: Codigo para leer teclado 4x4
« Respuesta #2 en: 14 de Diciembre de 2007, 11:10:41 »


PB0 = fil1
PB1 = fil2
PB2 = fil3
PB3 = fil4
PB4 = col1
PB5 = col2
PB6 = col3
PB7 = col4
« Última modificación: 14 de Diciembre de 2007, 11:16:49 por gilbert »

Desconectado PerriPic

  • PIC10
  • *
  • Mensajes: 24
Re: Codigo para leer teclado 4x4
« Respuesta #3 en: 17 de Diciembre de 2007, 13:59:00 »
X lo q veo estás trabajando en ccs(creo) mientras q yo lo hago en c18. De todas formas voy a exarle un vistazo a ver si puedo cambiar algunas cosas y utilizarlo xq mira q llevo tiempo con esto, q pensaba q iba a ser una tontería y no me sale ni para atrás!!!Muxas gracias de todas formas!!!!

Desconectado LABmouse

  • Moderadores
  • DsPIC30
  • *****
  • Mensajes: 3575
    • Juntos es mejor
Re: Codigo para leer teclado 4x4
« Respuesta #4 en: 17 de Diciembre de 2007, 17:51:37 »
A ver si te doy una mano, aprovechando que estoy probando el Entrenador Universal...

Perdonen la mala calidad del Video, pero la calidad del Video de mi cámara Fotográfica, deja mucho que desear.. :D


Codigo:

Es una pequeña modificación del código entregado junto al compilador CCS pero este lo entrega para teclado 3x4.

Código: CSS
  1. #include <18F452.h>
  2. #device adc=8
  3.  
  4. #FUSES NOWDT                    //No Watch Dog Timer
  5. #FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
  6. #FUSES HS                       //High speed Osc (> 4mhz)
  7. #FUSES NOPROTECT                //Code not protected from reading
  8. #FUSES NOOSCSEN                 //Oscillator switching is disabled, main oscillator is source
  9. #FUSES BROWNOUT                 //Reset when brownout detected
  10. #FUSES BORV45                   //Brownout reset at 4.5V
  11. #FUSES PUT                      //Power Up Timer
  12. #FUSES STVREN                   //Stack full/underflow will cause reset
  13. #FUSES NODEBUG                  //No Debug mode for ICD
  14. #FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
  15. #FUSES NOWRT                    //Program memory not write protected
  16. #FUSES NOWRTD                   //Data EEPROM not write protected
  17. #FUSES NOWRTB                   //Boot block not write protected
  18. #FUSES NOWRTC                   //configuration not registers write protected
  19. #FUSES NOCPD                    //No EE protection
  20. #FUSES NOCPB                    //No Boot Block code protection
  21. #FUSES NOEBTR                   //Memory not protected from table reads
  22. #FUSES NOEBTRB                  //Boot block not protected from table reads
  23.  
  24. #use delay(clock=20000000)
  25.  
  26.  
  27. #define segmentos  output_d
  28. int c;
  29. int bcd_7seg[16]={
  30. //ABCDEFG  
  31. 0b00111111,//0
  32. 0B00000110,//1
  33. 0B01011011,//2
  34. 0B01001111,//3
  35. 0B01100110,//4
  36. 0B01101101,//5
  37. 0B01111101,//6
  38. 0B00000111,//7
  39. 0B01111111,//8
  40. 0B01100111,//9
  41. 0B01110111,//10 A
  42. 0B01111100,//11 B
  43. 0B00111001,//12 C
  44. 0B01011110,//13 D
  45. 0B01111001,//14 E
  46. 0B01110001,//15 F
  47. };
  48.  
  49. #include "TECLADO4X4.C"
  50. #ZERO_RAM
  51.  
  52. void main()
  53. {
  54.    port_b_pullups(TRUE);
  55.    setup_adc_ports(NO_ANALOGS);
  56.    setup_adc(ADC_OFF);
  57.    setup_psp(PSP_DISABLED);
  58.    setup_spi(SPI_SS_DISABLED);
  59.    setup_wdt(WDT_OFF);
  60.    setup_timer_0 (RTCC_OFF);
  61.    setup_timer_1(T1_DISABLED);
  62.    setup_timer_2(T2_DISABLED,0,1);
  63.    setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
  64.    kbd_init();
  65.    output_a(0b00001000);
  66.    
  67.       while(1){
  68.       c = kbd_getc();
  69.       if(c!=0x10) segmentos(bcd_7seg[c]);
  70.       }
  71. }


LIBRERIA:

Código: CSS
  1. #define use_portb_kbd TRUE
  2.  
  3. #if defined(__PCH__)
  4. #if defined use_portb_kbd
  5.    #byte kbd = 0xF81                   // This puts the entire structure
  6. #else
  7.    #byte kbd = 0xF83                   // This puts the entire structure
  8. #endif
  9. #else
  10. #if defined use_portb_kbd
  11.    #byte kbd = 6                  // on to port B (at address 6)
  12. #else
  13.    #byte kbd = 8                 // on to port D (at address 8)
  14. #endif
  15. #endif
  16.  
  17. #if defined use_portb_kbd
  18.    #define set_tris_kbd(x) set_tris_b(x)
  19. #else
  20.    #define set_tris_kbd(x) set_tris_d(x)
  21. #endif
  22.  
  23.  
  24.  
  25. //Keypad connection:   (for example column 0 is B2)
  26. //                Bx:
  27.  
  28. #ifdef blue_keypad  ///////////////////////////////////// For the blue keypad
  29. #define COL0 (1 << 2)
  30. #define COL1 (1 << 3)
  31. #define COL2 (1 << 6)
  32.  
  33. #define ROW0 (1 << 4)
  34. #define ROW1 (1 << 7)
  35. #define ROW2 (1 << 1)
  36. #define ROW3 (1 << 5)
  37.  
  38. #else ////////////////////////////////////////////////// For the black keypad
  39. #define COL0 (1 << 0)
  40. #define COL1 (1 << 1)
  41. #define COL2 (1 << 2)
  42. #define COL3 (1 << 3)
  43.  
  44. #define ROW0 (1 << 4)
  45. #define ROW1 (1 << 5)
  46. #define ROW2 (1 << 6)
  47. #define ROW3 (1 << 7)
  48.  
  49. #endif
  50.  
  51. #define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
  52. #define ALL_PINS (ALL_ROWS|COL0|COL1|COL2|COL3)
  53.  
  54. // Keypad layout:
  55. char const KEYS[4][4] = {{0X01,0X02,0X03,0X0A},
  56.                          {0X04,0X05,0X06,0X0B},
  57.                          {0X07,0X08,0X09,0X0C},
  58.                          {0X0E,0X00,0X0F,0X0D}};
  59.  
  60. #define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
  61.                                   // n is the number of times you expect
  62.                                   // to call kbd_getc each second
  63.  
  64.  
  65. void kbd_init() {
  66. }
  67.  
  68. char kbd_getc( ) {
  69.    static BYTE kbd_call_count;
  70.    static short int kbd_down;
  71.    static char last_key;
  72.    static BYTE col;
  73.  
  74.    BYTE kchar;
  75.    BYTE row;
  76.  
  77.    kchar=0x10;
  78.    if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
  79.        switch (col) {
  80.          case 0   : set_tris_kbd(ALL_PINS&~COL0);
  81.                     kbd=~COL0&ALL_PINS;
  82.                     break;
  83.          case 1   : set_tris_kbd(ALL_PINS&~COL1);
  84.                     kbd=~COL1&ALL_PINS;
  85.                     break;
  86.          case 2   : set_tris_kbd(ALL_PINS&~COL2);
  87.                     kbd=~COL2&ALL_PINS;
  88.                     break;
  89.          case 3   : set_tris_kbd(ALL_PINS&~COL3);
  90.                     kbd=~COL3&ALL_PINS;
  91.                     break;            
  92.                    
  93.        }
  94.  
  95.        if(kbd_down) {
  96.          if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
  97.            kbd_down=FALSE;
  98.            kchar=last_key;
  99.            last_key=0x10;
  100.          }
  101.        } else {
  102.           if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
  103.              if((kbd & ROW0)==0)
  104.                row=0;
  105.              else if((kbd & ROW1)==0)
  106.                row=1;
  107.              else if((kbd & ROW2)==0)
  108.                row=2;
  109.              else if((kbd & ROW3)==0)
  110.                row=3;
  111.              last_key =KEYS[row][col];
  112.              kbd_down = TRUE;
  113.           } else {
  114.              ++col;
  115.              if(col==4)
  116.                col=0;
  117.           }
  118.        }
  119.       kbd_call_count=0;
  120.    }
  121.   set_tris_kbd(ALL_PINS);
  122.   return(kchar);
  123. }