/*////////////////////////////////////////////////////////////////////////////
// Libreria Teclado Matriz 4x4 EXPAN_I2C.C //
// con expansor I2C y int_ext //
// Adaptacion: 01/01/2011 //
///////////////////////////////////////////////////////////////////////////////
CHIP - BUS Resistencias TECLADO 4x4 IN(1)/OUT(0)
------------ ---------------- ------------ --------------
P0 ---------- R 330 -------- Fila 1 0
P1 ---------- R 330 -------- Fila 2 0
P2 ---------- R 330 -------- Fila 3 0
P3 ---------- R 330 -------- Fila 4 0
P4 -------------------------- Col 1 1
P5 -------------------------- Col 2 1
P6 -------------------------- Col 3 1
P7 -------------------------- Col 4 1
/////////////////////////////////////////////////////////////////////////////
teclado usado
CHIP - BUS PIC
----------- ---------
INT ------------- RB0/INT
TIPOS DE CHIP BUS ADDR + A2|A1|A0 + RW/R
====================
PCF8574 0100 XXX 0/1
PCF8574A 0111 XXX 0/1
PCA9554 0100 XXX 0/1
PCA9554A 0111 XXX 0/1
PARA CONFIGURAR: A0,A1,A2 conectar a masa(0) o a Vcc(1).
EJEMPLO: PCF8574 0100 001 x
PCF8574A 0111 001 x
'===================================================
Teclado_Inicializa
Teclado_DejaDePulsar
Teclado_LeeOrdenTecla
Teclado_LeeHex
orden tecla
============
0 1 2 3
4 5 6 7
8 9 A B
C D E F
teclado hex usado
==================
1 2 3 F
4 5 6 E
7 8 9 D
A 0 B C
=== DEFINICIONES ===
P_BUS //COMFIGURA PUERTO DEL EXPANSOR BUS I2C
ADDR_EXPAN //DIRECCION DEL ESCLAVO DEL EXPANSIONADOR DEL BUS I2C
M_BUS_R //MODO LECTURA DEL BUS
M_BUS_RW //MODO ESCRITURA DEL BUS
///////////////////////////////////////////////////////////////////////////*/
#DEFINE P_BUS 0b11110000
//#DEFINE ADDR_EXPAN 0b011101
#DEFINE M_BUS_R 0b01110011 //0x73
#DEFINE M_BUS_RW 0b01110010 //0x72
int8 expan_bus_r(){
int8 dato;
i2c_start(); // inicia bus i2c
i2c_write(M_BUS_R); // direccion dispositivo esclavo + lectura
dato = i2c_read(); // lee del bus i2c el dato
i2c_stop(); // Stop condition
//printf("\r LEE bus"); // add debug
//printf("\r Dato: %d",dato); // add debug
return dato; // retorna dato
}
void expan_bus_rw(int8 dato){
i2c_start(); // inicia bus i2c
i2c_write(M_BUS_RW); // direccion dispositivo esclavo + escritura
i2c_write(dato); // escribe en bus i2c el dato
i2c_stop(); // para bus i2c
//printf("\r Se EJECUTO PCF8574_RW ");//add debug
}
int8 escanea_tecla(void){
//printf("\r Escanea tecla() ");
int8 f,c;
signed int8 dato=-1; // da -1 si no se pulso tecla
int8 fila=0b11111110; // primera fila
//printf("\r fila: %d",fila);//add
for (f=0; f<=3; f++) // Recorre las 4 filas
{
//printf("\r fila for: %d",fila);//add
expan_bus_rw(fila);
rotate_left(&fila, 1); // siguientes filas, va desplazando el 0 hacia la izquierda
//fila = fila >> 1;
//printf("\r rota fila: %d",fila);//add
delay_ms(20); // necesario antes de leer 20
dato=expan_bus_r();
c=0;
if (!bit_test(dato,4)) c=1; //Col 1 4
else if (!bit_test(dato,5)) c=2; //Col 2 5
else if (!bit_test(dato,6)) c=3; //Col 3 6
else if (!bit_test(dato,7)) c=4; //Col 4 7
if(c) break; // Si c es distinto de 0 sale del for
}
if(c){
//printf("\r f:%d c:%d dato: %d",f,c,dato); //add debug
dato=(f*4)+c;
}
else{
dato=-1; //Ninguna tecla pulsada
expan_bus_rw(P_BUS); //Vuelve a poner las lineas como salida (0) y
//como entrada (1) las Columnas
//expan_bus_rw(0b00001111); //add debug change
//printf("\r Se reinicio el PCF"); //add debug
//printf("\r dato: %d",dato); //add debug
}
return dato;
}
//converion de teclado hex a nuestro teclado
char tecla_c(int8 codigo)
{
char tabla_tecla[17]={'1','2','3','F','4','5','6','E','7','8','9','D','A','0','B','C','Z'};
return tabla_tecla[codigo-1];
}
int8 lee_teclado(void)
{
int8 tecla_hex;
tecla_hex=escanea_tecla();
return tecla_hex;
}
void Teclado_ini(void){
//port_b_pullups (0b00000001); // Activa la resitencia pull-up interna del pin RB0/INT
ext_int_edge( H_TO_L ); // La int. se activara por flanco de bajada
expan_bus_rw(0b11110000); // Pone como salida (0) las Lineas y como entrada (1) las Columnas
//PCF8574_Escribe(0b00001111); //add cambio puerto
delay_ms(50);
//printf("\r\n SE inicializo el teclado");
}