/**************************************************************************
* Librerira para manejar teclados 3x4 y 4x4 para Hitech *
***************************************************************************
***************************************************************************
* El puerto de entrada puede estar con resistencias de Pull-up o con *
* resistencias de Pull-Down. *
***************************************************************************
***************************************************************************
* *
* Antes de incluir este archivo en nuestro programa hay que definir *
* algunos parametros. Estos son *
* *
* KeyOutPort (el puerto que se utilizara como salida) *
* KeyInPort (el puerto que se utilizara como entrada) *
* OutD0 (el primer bit de salida) *
* InD0 (el primer bit de entrada) *
* *
* Segun el teclado a utilizar hay que definir PHONE HEXA u OTRO *
* Si el tipo de teclado es PHONE OutD0 puede tomar los siguientes valores *
* 0, 1, 4 o 5. InD0 puede ser 0 o 4 *
* *
* Para teclados del tipo HEXA u OTRO OutD0 e InD0 solo pueden ser 0 o 4 *
* *
* Si se van utilizar resistencias de Pull-up hay que definir PULLUP *
* Si se van utilizar resistencias de Pull-Down hay que definir PULLDOWN *
* *
***************************************************************************
***************************************************************************
* La unica funcion disponible es GetKey y devuelve el codigo ASCII de la *
* tecla precionada *
***************************************************************************
***************************************************************************
* Disposicion de teclados *
* *
* Como los unicos bits definidos son OutD0 e InD0, el resto de los bits *
* se usan a partir del D0. Es decir, si definimos OutD0 1 quiere decir *
* que OutD0 tiene que estar conectado al bit 1 del puerto elegido, de *
* esta forma OutD1 tiene que ir conectado al bit 2 y OutD2 al bit 3 *
* El mismo razonamiento se utiliza para los bits de entrada *
***************************************************************************
***************************************************************************
* PHONE *
* *
* OutD0 OutD1 OutD2 *
* | | | *
* --- --- --- *
* InD0 ---| 1 |---| 2 |---| 3 | *
* --- --- --- *
* | | | *
* --- --- --- *
* InD1 ---| 4 |---| 5 |---| 6 | *
* --- --- --- *
* | | | *
* --- --- --- *
* InD2 ---| 7 |---| 8 |---| 9 | *
* --- --- --- *
* | | | *
* --- --- --- *
* InD3 ---| * |---| 0 |---| # | *
* --- --- --- *
* *
***************************************************************************
***************************************************************************
* HEXA *
* *
* OutD0 OutD1 OutD2 OutD3 *
* | | | | *
* --- --- --- --- *
* InD0 ---| 0 |---| 1 |---| 2 |---| 3 | *
* --- --- --- --- *
* | | | | *
* --- --- --- --- *
* InD1 ---| 4 |---| 5 |---| 6 |---| 7 | *
* --- --- --- --- *
* | | | | *
* --- --- --- --- *
* InD2 ---| 8 |---| 9 |---| A |---| B | *
* --- --- --- --- *
* | | | | *
* --- --- --- --- *
* InD3 ---| C |---| D |---| E |---| F | *
* --- --- --- --- *
* *
***************************************************************************
***************************************************************************
* OTRO *
* *
* OutD0 OutD1 OutD2 OutD3 *
* | | | | *
* --- --- --- --- *
* InD0 ---| 1 |---| 2 |---| 3 |---| A | *
* --- --- --- --- *
* | | | | *
* --- --- --- --- *
* InD1 ---| 4 |---| 5 |---| 6 |---| B | *
* --- --- --- --- *
* | | | | *
* --- --- --- --- *
* InD2 ---| 7 |---| 8 |---| 9 |---| C | *
* --- --- --- --- *
* | | | | *
* --- --- --- --- *
* InD3 ---| * |---| 0 |---| # |---| D | *
* --- --- --- --- *
* *
***************************************************************************
* Ejemplo de configuración y utilización
Se usa un teclado tipo PHONE, se leen los datos por el nibble alto del puerto B
con resistencias de pull-down
y para la salida hacia el teclado se usa parte del nibble bajo del puerto A
En un PIC 16F876A
#define PHONE
#define PULLDOWN
#define KeyOutPort PORTA
#define KeyInPort PORTB
#define OutD0 0
#define InD0 4
#include "Teclados.c"
void main (void)
{
unsigned char Tecla_pulsada;
CMCON = 7; // desactivo los comparadores
ADON = 0; // apago el conversor AD
ADCON1 = 0x07; // entradas digitales
TRISB = 0xF0; // el nibble alto como entrada
TRISA = 0; // puerto como salida
TRISC = 0; // puerto C como salida
PORTC = 0;
PORTA = 0x07; // salidas del puerto A en "1"
while (1)
{
if ((PORTB & 0xF0) != 0) //si hay alguna tecla presionada
{
// alguna rutina de espera de unos 10ms para Antirebote
if ((PORTB & 0xF0) != 0) si sigue presionada
{
Tecla_pulsada = GetKey(); //leeo el teclado
if (Tecla_pulsada != 0) PORTC = Tecla_pulsada; // muestro en el puerto C el valor leido
}
}
}
}
*/
#if (defined (KeyOutPort) && defined (KeyInPort) && defined (OutD0) && defined (InD0)) \
&& (defined (PHONE) || defined (HEXA) || defined (OTRO)) && \
(defined (PULLUP) || defined (PULLDOWN))
#if defined (PHONE)
const unsigned char AsciiKey [4][3] = {'1','2','3','4','5','6','7','8','9','*','0','#'};
unsigned char GetKey (void)
{
unsigned char key;
unsigned char fila = 0;
unsigned char columna = 0;
#if defined (PULLDOWN)
do
{
#if (OutD0 == 0)
KeyOutPort &= 0b11111000;
KeyOutPort |= (1 << columna);
#elif (OutD0 == 1)
KeyOutPort &= 0b11110001;
KeyOutPort |= (1 << (columna + 1));
#elif (OutD0 == 4)
KeyOutPort &= 0b10001111;
KeyOutPort |= (1 << (columna + 4));
#elif (OutD0 == 5)
KeyOutPort &= 0b00011111;
KeyOutPort |= (1 << (columna + 5));
#endif
#if (InD0 == 0)
key = PORTB & 0x0F;
#elif (InD0 == 4)
key = PORTB & 0xF0;
key = key >> 4;
#endif
columna++;
}
while ((key == 0) && (columna < 3));
if (key == 1) fila = 0;
else
if (key == 2) fila = 1;
else
if (key == 4) fila = 2;
else
if (key == 8) fila = 3;
#if (OutD0 == 0)
KeyOutPort |= 0b00000111;
#elif (OutD0 == 1)
KeyOutPort |= 0b00001110;
#elif (OutD0 == 4)
KeyOutPort |= 0b01110000;
#elif (OutD0 == 5)
KeyOutPort |= 0b11100000;
#endif
if (key != 0) return AsciiKey[fila][columna - 1];
#elif defined (PULLUP)
do
{
#if (OutD0 == 0)
KeyOutPort |= 0b00000111;
KeyOutPort &= ~(1 << columna);
#elif (OutD0 == 1)
KeyOutPort |= 0b00001110;
KeyOutPort &= ~(1 << (columna + 1));
#elif (OutD0 == 4)
KeyOutPort |= 0b01110000;
KeyOutPort &= ~(1 << (columna + 4));
#elif (OutD0 == 5)
KeyOutPort |= 0b11100000;
KeyOutPort &= ~(1 << (columna + 5));
#endif
#if (InD0 == 0)
key = PORTB & 0x0F;
#elif (InD0 == 4)
key = PORTB & 0xF0;
key = key >> 4;
#endif
columna++;
}
while ((key == 0x0F) && (columna < 3));
if (key == 0b00001110) fila = 0;
else
if (key == 0b00001101) fila = 1;
else
if (key == 0b00001011) fila = 2;
else
if (key == 0b00000111) fila = 3;
#if (OutD0 == 0)
KeyOutPort &= 0b11111000;
#elif (OutD0 == 1)
KeyOutPort &= 0b11110001;
#elif (OutD0 == 4)
KeyOutPort &= 0b10001111;
#elif (OutD0 == 5)
KeyOutPort &= 0b00011111;
#endif
if (key != 0x0F) return AsciiKey[fila][columna - 1];
#endif
return 0;
}
#elif defined (HEXA)
const unsigned char AsciiKey [4][4] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
unsigned char GetKey (void)
{
unsigned char key;
unsigned char fila = 0;
unsigned char columna = 0;
#if defined (PULLDOWN)
do
{
#if (OutD0 == 0)
KeyOutPort &= 0b11110000;
KeyOutPort |= (1 << columna);
#elif (OutD0 == 4)
KeyOutPort &= 0b00001111;
KeyOutPort |= (1 << (columna + 4));
#endif
#if (InD0 == 0)
key = PORTB & 0x0F;
#elif (InD0 == 4)
key = PORTB & 0xF0;
key = key >> 4;
#endif
columna++;
}
while ((key == 0) && (columna < 4));
if (key == 1) fila = 0;
else
if (key == 2) fila = 1;
else
if (key == 4) fila = 2;
else
if (key == 8) fila = 3;
#if (OutD0 == 0)
KeyOutPort |= 0b00001111;
#elif (OutD0 == 4)
KeyOutPort |= 0b11110000;
#endif
if (key != 0) return AsciiKey[fila][columna - 1];
#elif defined (PULLUP)
do
{
#if (OutD0 == 0)
KeyOutPort |= 0b00001111;
KeyOutPort &= ~(1 << columna);
#elif (OutD0 == 4)
KeyOutPort |= 0b11110000;
KeyOutPort &= ~(1 << (columna + 4));
#endif
#if (InD0 == 0)
key = PORTB & 0x0F;
#elif (InD0 == 4)
key = PORTB & 0xF0;
key = key >> 4;
#endif
columna++;
}
while ((key == 0x0F) && (columna < 4));
if (key == 0b00001110) fila = 0;
else
if (key == 0b00001101) fila = 1;
else
if (key == 0b00001011) fila = 2;
else
if (key == 0b00000111) fila = 3;
#if (OutD0 == 0)
KeyOutPort &= 0b11110000;
#elif (OutD0 == 4)
KeyOutPort &= 0b00001111;
#endif
if (key != 0x0F) return AsciiKey[fila][columna - 1];
#endif
return 0;
}
#elif defined (OTRO)
const unsigned char AsciiKey [4][4] = {'1','2','3','A','4','5','6','B','7','8','9','C','*','0','#','D'};
unsigned char GetKey (void)
{
unsigned char key;
unsigned char fila = 0;
unsigned char columna = 0;
#if defined (PULLDOWN)
do
{
#if (OutD0 == 0)
KeyOutPort &= 0b11110000;
KeyOutPort |= (1 << columna);
#elif (OutD0 == 4)
KeyOutPort &= 0b00001111;
KeyOutPort |= (1 << (columna + 4));
#endif
#if (InD0 == 0)
key = PORTB & 0x0F;
#elif (InD0 == 4)
key = PORTB & 0xF0;
key = key >> 4;
#endif
columna++;
}
while ((key == 0) && (columna < 4));
if (key == 1) fila = 0;
else
if (key == 2) fila = 1;
else
if (key == 4) fila = 2;
else
if (key == 8) fila = 3;
#if (OutD0 == 0)
KeyOutPort |= 0b00001111;
#elif (OutD0 == 4)
KeyOutPort |= 0b11110000;
#endif
if (key != 0) return AsciiKey[fila][columna - 1];
#elif defined (PULLUP)
do
{
#if (OutD0 == 0)
KeyOutPort |= 0b00001111;
KeyOutPort &= ~(1 << columna);
#elif (OutD0 == 4)
KeyOutPort |= 0b11110000;
KeyOutPort &= ~(1 << (columna + 4));
#endif
#if (InD0 == 0)
key = PORTB & 0x0F;
#elif (InD0 == 4)
key = PORTB & 0xF0;
key = key >> 4;
#endif
columna++;
}
while ((key == 0x0F) && (columna < 4));
if (key == 0b00001110) fila = 0;
else
if (key == 0b00001101) fila = 1;
else
if (key == 0b00001011) fila = 2;
else
if (key == 0b00000111) fila = 3;
#if (OutD0 == 0)
KeyOutPort &= 0b11110000;
#elif (OutD0 == 4)
KeyOutPort &= 0b00001111;
#endif
if (key != 0x0F) return AsciiKey[fila][columna - 1];
#endif
return 0;
}
#endif
#else
#error Faltan definir parametros
#endif