 

     PH7...PH0               Masque               Touche                                Code 

     1111 1110              0001 0000                1  si résultat =0                    1 
     1111 1110              0010 0000                2                                    2 
     1111 1110              0100 0000                3                                    3 

     1111 1101              0001 0000                4                                    4 
     1111 1101              0010 0000                5                                    5 
     1111 1101              0100 0000                6                                    6 

     1111 1011              0001 0000                7                                    7 
     1111 1011              0010 0000                8                                    8 
     1111 1011              0100 0000                9                                    9 
       

     1111 0111              0001 0000                *                                   10 
     1111 0111              0010 0000                0                                   11 
     1111 0111              0100 0000                #                                   12 


 Algorithme d'encodage du clavier 

    * Documentation 
    * Nom: ScanKey 
    * Description: Sous routine qui balaye le clavier (Une seule fois) 
    * Intrants: Aucun 
    * Extrants: B = 0 si aucune touche n'a été appuyé 
    *           B = Code si touche appuyé 
    * 
    * Codé par : ABC le 02/novembre/2002 
    * 

     CompteC <-- 01 
     CompteR <-- 00 
     Masque <-- 0001 0000 
     ScanR  <-- 1111 1110 
     DDRC   <-- 0000 1111 
     Code <-- 00                      Touche nulle 

     DO 
         PortC <-- ScanR                                                 
         DO 
             PortC --> A 
             A <-- A AND Masque 
             IF (A=0) 
                 Code <-- CompteC + CompteR 
             ENDIF 
             CompteC++ 
             Shift left Masque 
         WHILE ( CompteC <= 3 AND Code = 0) 
         Masque  <-- 0001 0000 
         CompteC <-- 1 
         CompteR <-- CompteR + 3 
         C <-- 1                                                         
         Rotate Left ScanR 
      WHILE ( CompteR <=9 AND Code = 0) 
      B <-- Code 
      Retour       (si nécessaire)


/* Variables*/
unsigned char nouvelle_touche, ancienne_touche;


// Définition des constantes
const char *table_clavier = ".123A456B789C*0#D";

#define KEY_DEBTIME 0x40        // key debounce time



/* Prototypes des fonctions*/
void clavier_initialise(void);
char kbhit();
char getkeypad();



/* PORTH: rangées -> 0-3 sorties*/
/* PORTH: colonnes -> 4-7 entrée connectées à des resitances de tirages , sachant que PH7 n'est pas utilisé */


void clavier_initialise()
{
    DDRH = 0x0F;    // set Port H direction (0=input, 1=output)
    PPSH = 0xF0;    // set for pull downs
    PERH = 0xF0;    // enable pull downs
    Newkey=0; Oldkey=0; // reset keypad status variables
}


/*----------------------------------------------------------------------------
kbhit
-----
 Does a KEYPAD scan.

 returns 0 if NO key is pressed
 otherwise returns the representation of the key pressed from the included table

 ----------------------------------------------------------------------------*/
char kbhit(){
    unsigned char column, keycount, kstat, delaytime;

    PTH = 0x0F;     // set all KEYPAD columns
    kstat = PTH;    // get KEYPAD data
    kstat &= 0xF0;  // mask rows
    if(kstat != 0){ // if key pressed

        for(column = 0x01, keycount=0; column < 0x10; column <<= 1, keycount++){
            PTH = 0;    // clear all KEYPAD columns
            PTH = column;   // set this KEYPAD column
            for(delaytime=0x20; delaytime > 0; delaytime--) ;   // wait for lines to settle
            kstat = PTH;    // get KEYPAD data
            kstat &= 0xF0;  // mask rows
            if(kstat != 0){ // if key pressed on this column
                kstat >>= 4;
                if(kstat == 0x04)   kstat = 3;
                if(kstat == 0x08)   kstat = 4;
                return(keytable[(keycount*4) + kstat]);
            }
        }
    }
    return 0;   // return no key pressed

}

/*----------------------------------------------------------------------------
getkeypad
---------
 Returns a key pressed on the keypad.  First waits for any current key pressed
 to be released, then does not return until a new key is debounced.

 Returns the representation of the key pressed from the array keytable[]
 ----------------------------------------------------------------------------*/
char getkeypad(){
    char debcount;  // debounce counter

    do{
        Newkey = kbhit();   // get key pressed status
    }while(Newkey == Oldkey);   // while same key pressed as last scan

    for(debcount=0; debcount < KEY_DEBTIME; debcount++){
        Newkey = kbhit();
        if(Newkey == 0 || Newkey != Oldkey){    // if key not still pressed
            debcount=0; // clear debounce counter
        }
        Oldkey = Newkey;    // save for next scan
    }
    return(Newkey);
}

/**************************************************************************************************************/
vois main(void)
{
    char keyval;

    lcd_affiche("Appui sur un touche :");

    clavier_initialise();  

    for(; ; )
       { touche = getkeypad();	// renvoie la touche choisie
         putchar(touche);    	// affiche la touche qui vient d'être saisie
       }
}

