Autor Tema: Pq no funciona el programa para el teclado matricial  (Leído 1973 veces)

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

Desconectado jbernardinocesar

  • PIC10
  • *
  • Mensajes: 4
Pq no funciona el programa para el teclado matricial
« en: 13 de Enero de 2004, 11:47:00 »
Hola a todos es la primera vez que acudo a un foro para pedir ayuda , estoy haciendo el proyecto final de carrera y necesito utilizar un teclado matricial con el pic 16F877 he cogido de base el programa que te biene con el PIC C Compiler y le he hecho las correspondientes modificaciones.

Mi teclado lo tengo conectado al puerto C con el siguente orden

row0  row1  row2  row3  col3  col2  col1  colo
RC7    RC6   RC5    RC4    RC3  RC2  RC1  RC0

Al tenerlo conectado al puerto C he colocado unas resistencias de 2K2 en RC3 RC2 RC1 RC0

Aqui os coloco el codigo que utilizo:

Programa Principal


include <16F877.H>


#fuses XT, NOWDT, NOPROTECT, BROWNOUT, NOLVP, PUT



#use delay(clock=4000000)
#use fast_io(A)      // acceso al puerto sin programar TRIS_A
#use fast_io(B)      // acceso al puerto sin programar TRIS_B
#use fast_io(C)      // acceso al puerto sin programar TRIS_C
#use fast_io(D)      // acceso al puerto sin programar TRIS_D
#use fast_io(E)      // acceso al puerto sin programar TRIS_E

#define Ed   PIN_E0
#define Ep1   PIN_E1
#define Ep2   PIN_E2
#define C5AC   PIN_B1
#define C12AC   PIN_B2
#define CB   PIN_B3
#define CA   PIN_B4

#byte puerto_d=8
#iNCLUDE <pruelcd.c>
#include <pruekbd.c>

//DECLARACION DE FUNCIONES

void inicializaciones_puertos();


main()
{

      char k;

inicializaciones_puertos();
inicializaciones_interrupciones();
 lcd_init();
 kbd_init();
lcd_putc( "Preparado:";
   while(TRUE)
      {
    k=kbd_getc();

      if(k!="")
               {
 
                 if(k=="A")
                  lcd_putc("Hola";
                 else
                    {
                   lcd_putc("f";
                   lcd_putc(k);
                   delay_us(50);

                    }

             }
       
       }

}

void inicializaciones_puertos()
   {
      //Configuración de puertos
      //bits     76543210
     set_tris_a( 0b00000000);    //1 -input  0 -output
     set_tris_c( 0b00000000);    //1 -input  0 -output
    set_tris_b( 0b00000000);
     set_tris_d( 0b00000000);
   set_tris_e( 0b00000000);

    output_low(Ed);
   output_low(Ep1);
    output_low(Ep2);
    output_low(C5AC);
     output_low(C12AC);
     output_low(CB);
     output_low(CA);
   }


Libreria del teclado




#byte kbd = 7


//Keypad connection:   (for example column 0 is c7)
//                cx:


#define COL0 (1 << 3)
#define COL1 (1 << 2)
#define COL2 (1 << 1)
#define COL3 (1 << 0)

#define ROW0 (1 << 7)
#define ROW1 (1 << 6)
#define ROW2 (1 << 5)
#define ROW3 (1 << 4)


#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
#define ALL_PINS (ALL_ROWS|COL0|COL1|COL2|COL3)

// Keypad layout:
char const KEYS[4][4] =
{{"3","2","1","0"},
                         {"7","6","5","4"},
                         {"B","A","9","8"},
                         {"F","E","D","C"}};

#define KBD_DEBOUNCE_FACTOR 33    // Set this number to apx n/333 where
                                  // n is the number of times you expect
                                  // to call kbd_getc each second


void kbd_init() {
}



char kbd_getc( ) {
   static int kbd_call_count;
   static short int kbd_down;
   static char last_key;
   static byte col;

   char kchar;
   int row;

   kchar="";
   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR)
   {


       switch (col)
       {
         case 0   : set_tris_c(ALL_PINS&~COL0);
                    kbd=~COL0&ALL_PINS;

                    break;
         case 1   : set_tris_c(ALL_PINS&~COL1);
                    kbd=~COL1&ALL_PINS;

                    break;
         case 2   : set_tris_c(ALL_PINS&~COL2);
                    kbd=~COL2&ALL_PINS;

                    break;
         case 3   : set_tris_c(ALL_PINS&~COL3);
                    kbd=~COL3&ALL_PINS;

                    break;
      }

       if(kbd_down)
       {
         if((kbd & (ALL_ROWS))==(ALL_ROWS))
            {

              kbd_down=false;
              kchar=last_key;
              last_key="";
            }
       }
        else
         {
          if((kbd & (ALL_ROWS))!=(ALL_ROWS))
            {
         
             if((kbd & ROW0)==0)
               row=0;


             else if((kbd & ROW1)==0)

               row=1;


             else if((kbd & ROW2)==0)

               row=2;


             else if((kbd & ROW3)==0)
               row=3;


             last_key =KEYS[row][col];
             kbd_down = true;
            }
            else
               {
                 
                 ++col;
                if(col==4)
                  col=0;
               }
         }
      kbd_call_count=0;
   }
  set_tris_c(ALL_PINS);

  return(kchar);
}


Muchas gracias por vuestra ayuda


Desconectado ejmc

  • PIC18
  • ****
  • Mensajes: 311
RE: Pq no funciona el programa para el teclado matricial
« Respuesta #1 en: 14 de Enero de 2004, 21:12:00 »
Hola, si trabajas con C hay un foro de este un poco mas abajo de este, seguro que alguien te va a ayyudar
Saludos


 

anything