Autor Tema: Pq no funcionea este codigo para un teclado matricial  (Leído 2300 veces)

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

Desconectado jbernardinocesar

  • PIC10
  • *
  • Mensajes: 4
Pq no funcionea este codigo para un teclado matricial
« en: 15 de Enero de 2004, 07:46: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!="\0")
{

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="\0";
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="\0";
}
}
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 pocher

  • Moderador Local
  • DsPIC30
  • *****
  • Mensajes: 2568
RE: Pq no funcionea este codigo para un teclado matricial
« Respuesta #1 en: 18 de Enero de 2004, 00:21:00 »
Yo trabajo con las rutinas para LCD y teclado de Microsystems (PICTRAINER), si quieres te las paso y les haces las modificaciones oportunas.

lcd2.c ------> control en PORTA y 8 bits de datos en PORTB

kbd2.c -----> usa PULLUPS internas con RB0 .. RB3 entradas de PIC para colúmnas del teclado (salidas de teclado) y RB4 .. RB7 salidas de PIC para filas del teclado (entradas al teclado).

Un saludo

Desconectado jbernardinocesar

  • PIC10
  • *
  • Mensajes: 4
RE: Pq no funcionea este codigo para un teclado matricial
« Respuesta #2 en: 18 de Enero de 2004, 07:40:00 »
Muchas gracias por tu interes

Si me interesaría que me eniaras la rutina del teclado ya que el LCD ya me funciona bien.

Si me lo pudieras adjuntar en una respuesta te lo agradecería.

Desconectado pocher

  • Moderador Local
  • DsPIC30
  • *****
  • Mensajes: 2568
RE: Pq no funcionea este codigo para un teclado matricial
« Respuesta #3 en: 18 de Enero de 2004, 13:38:00 »
Hola bernardino.

Esta es la rutina para el teclado:

/*-------------------------------------------*
 |  KBD2.C                                   |
 |  Autor: Pedro Mª Jiménez                  |
 |  (c) Microsystems Engineering (Bilbao)    |
*-------------------------------------------*/

// Funciones para la gestión del teclado de la Trainer PLUS.

// char kbd_getc()   Devuelve el código ASCII de la tecla pulsada.
//         Si no se pulsó ninguna, devuelve 0.


// Conexiones del teclado.
/*
            RB0 RB1 RB2 RB3
             ^   ^   ^   ^
             |   |   |   |
           |---|---|---|---|
  RB4 ---> | 1 | 2 | 3 | F |
           |---|---|---|---|
  RB5 ---> | 4 | 5 | 6 | E |
           |---|---|---|---|
  RB6 ---> | 7 | 8 | 9 | D |
           |---|---|---|---|
  RB7 ---> | A | 0 | B | C |
           |---|---|---|---|
*/

// Caracteres ASCII asociados a cada tecla:
char const KEYS[4][4] = {{"1","2","3","F"},
                         {"4","5","6","E"},
                         {"7","8","9","D"},
                         {"A","0","B","C"}};

#byte kbd_port_b = 6


char kbd_getc()
{
  char tecla=0;
  int f,c,t,i,j;

  port_b_pullups(true);
  set_tris_b(0b00001111); // RB7-RB4 salidas, RB3-RB0 entradas

  for(f=0x10, i=0; i<4; f<<=1, i++)
  {
    for(c=0x01, j=0; j<4; c<<=1, j++)
    {
      kbd_port_b = ~f;
      delay_cycles(1);
      t = kbd_port_b & 0x0F;
      t = ~(t | 0xF0);
      if(t == c)
      {
        delay_ms(20);
        tecla=KEYS[j];
        while(t==c)
        {
          restart_wdt();
          t = kbd_port_b & 0x0F;
          t = ~(t | 0xF0);
        }
        break;
      }
    }
    if(tecla)
      break;
  }

  port_b_pullups(false);
  return tecla;
}

Un saludo

Desconectado Enrique_86

  • PIC12
  • **
  • Mensajes: 62
Re: Pq no funcionea este codigo para un teclado matricial
« Respuesta #4 en: 01 de Marzo de 2008, 01:13:28 »
Hola!
Alguien me podria ayudar?? estoy buscando un codigo para teclado matricial pero en C18 de Microchip, en verdad se los agradeceria bastante!!!  :-)
...enrique  :-)


 

anything