/******************************************************************************* ** ** ** Datei: Tastatur.C ** ** Das Programm beinhaltet: Routinen zur Kommunikation zwischen ** ** Controller und Tastatur ** ** über I2C-Bus ** ** ** ** ** ** Eingangsparameter: KEINE! (void) ** ** ** ** Ausgangsparameter: KEINE! (void) ** ** ** ** Rueckgabewert: KEINER! (void) ** ** ** ******************************************************************************** ** ** ** Autor: Thorsten Breuer ** ** Quellen: ** ** Datum: 09.01.1999 WS 98/99 ** ** ** *******************************************************************************/ /***************************** globale Variablen ******************************/ unsigned char key; /* speichert, welche Taste gedrückt ist */ /********************************* Prototypen *********************************/ void wait(unsigned int); /* Warteschleife für kurze Pausen */ unsigned char read_tast(void); /* Byte von Tastatur lesen */ void write_tast(unsigned char); /* Byte an Tastatur anlegen */ unsigned char taste_einlesen(void); /* Tastendruck ermitteln */ /****************************** Tastaturabfrage *******************************/ unsigned char taste_einlesen(void) { unsigned char test = 0; unsigned char taste = 0; unsigned char taste_wert='q'; /* dummywert */ write_tast(0xFE); wait(25); /* 1. Spalte einlesen */ test = read_tast(); if ( test == 0xEE ) {taste = 1; taste_wert = '1';} if ( test == 0xDE ) {taste = 2; taste_wert = '4';} if ( test == 0xBE ) {taste = 3; taste_wert = '7';} if ( test == 0x7E ) {taste = 4; taste_wert = 'A';} write_tast(0xFD); wait(25); /* 2. Spalte einlesen */ test = read_tast(); if ( test == 0xED ) {taste = 5; taste_wert = '2';} if ( test == 0xDD ) {taste = 6; taste_wert = '5';} if ( test == 0xBD ) {taste = 7; taste_wert = '8';} if ( test == 0x7D ) {taste = 8; taste_wert = '0';} write_tast(0xFB); wait(25); /* 3. Spalte einlesen */ test = read_tast(); if ( test == 0xEB ) {taste = 9; taste_wert = '3';} if ( test == 0xDB ) {taste = 10; taste_wert = '6';} if ( test == 0xBB ) {taste = 11; taste_wert = '9';} if ( test == 0x7B ) {taste = 12; taste_wert = 'B';} write_tast(0xF7); wait(25); /* 4. Spalte einlesen */ test = read_tast(); if ( test == 0xE7 ) {taste = 13; taste_wert = 'F';} if ( test == 0xD7 ) {taste = 14; taste_wert = 'E';} if ( test == 0xB7 ) {taste = 15; taste_wert = 'D';} if ( test == 0x77 ) {taste= 16; taste_wert = 'C';} return taste_wert; } /************************ Byte an die Tastatur anlegen ************************/ void write_tast(unsigned char byte) { *A0_HIGH = 0xC0; /* C0H in S1 =>ES0 = 1; */ wait(50); /* I2C-Bus Kommunikation freigeben */ *A0_LOW = 0x42; /* laden der slave addresse */ wait(50); *A0_HIGH = 0xC5; /* C5H in S1 =>START; slave addresse und */ wait(50); /* clock pulse für slave acknowledgement */ /* wird rausgeschickt. */ *A0_HIGH = 0xC1; /* C1H in S1 => Freigabe der I2C-Bus Kommun. */ wait(50); /* SDC und SCL sind HIGH */ /* Naechste Lese- oder Schreiboperation ist */ /* zu/von data transfer register S0 wenn A=LOW */ *A0_LOW = byte; /* Byte übertragen */ wait(50); *A0_HIGH = 0xC2; /* C2H in S1 =>STOP */ } /************************ Byte von der Tastatur lesen *************************/ unsigned char read_tast(void) { unsigned char wert = 0; *A0_HIGH = 0xC0; /* C0H in S1 =>ES0 = 1; */ wait(25); /* I2C-Bus Kommunikation freigeben */ *A0_LOW = 0x43; /* laden der slave addresse */ wait(25); *A0_HIGH = 0xC5; /* C5H in S1 =>START; slave addresse und */ wait(25); /* clock pulse für slave acknowledgement */ /* wird rausgeschickt. */ *A0_HIGH = 0xC1; /* C1H in S1 -> Freigabe der I2C-Bus Kommun.*/ wait(25); /* SDC und SCL sind HIGH */ /* Naechste Lese- oder Schreiboperation ist */ /* zu/von data transfer register S0 wenn A=LOW */ wert = *A0_LOW; /* Byte lesen */ wait(25); *A0_HIGH = 0xC2; /* C2H in S1 =>STOP */ return wert; }