File "keypad.c"

Full Path: /home/analogde/www/private/Projet/keypad.c
File size: 3.44 KB
MIME-type: text/x-c
Charset: utf-8

/* ===========================================================================

    keypad.c  - KEYPAD routines for Axiom CML12SDP256 and 912x boards using
                port H

    Version:        1.0
    Author:         Dusty Nidey, Axiom Manufacturing (www.axman.com)
    Compiler:       GNU for 68HC11 & 68HC12 (www.gnu-m68hc11.org)

    This is freeware - use as you like
    Modified By Jason Siegel MOnday Dec 13 2004   siegeljb
==============================================================================
------------------------------------------------------------------------------
*/

#include "keypad.h"
#include "..\ports_d256.h"

#define DelAY 0x10
// Constant Definitions
// --------------------
const char keytable[17] = ".123A456B789C*0#D";

// RAM Variables
// -------------
unsigned char Newkey, Oldkey;


/*----------------------------------------------------------------------------
InitKeyPad
----------
 Initialize KEYPAD port used

     Port H 0-3 must be output
            4-7 must be input with pull-down enabled
 ----------------------------------------------------------------------------*/
void InitKeypad(){
    DDRH = 0x0F;    // set Port H direction (0=input, 1=output)
    PPSH = 0xF0;    // set for pull downs
    PERH = 0xF0;    // enable pull downs
    
    //PIEH=  0xF0; //enable interrupts
    
    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 kstat0, column,output,strobeout;
    unsigned char delaytime;
     output=0;

    PTH = 0x0F;     // set all KEYPAD columns
    for(delaytime=DelAY; delaytime > 0; delaytime--) ;
    kstat0 = PTH& 0xF0;    // get KEYPAD data
    if(kstat0 != 0)

        {
        strobeout=1;
        
        for(column=0;column<4;column++)
        {
             PTH = strobeout;     // set all KEYPAD columns
             strobeout<<=1;
             for(delaytime=DelAY; delaytime > 0; delaytime--) ;
             kstat0 = PTH & 0xF0;    // get KEYPAD data
             kstat0 >>= 4;
             if(kstat0 != 0)
             {
              if(kstat0 == 0x04)   kstat0 = 3;
              if(kstat0 == 0x08)   kstat0 = 4;
              output=keytable[(4*column)+kstat0];
              column=5;
             }
        }

        }

    return output;   // 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(){
    long debcount;  // debounce counter

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

    for(debcount=0; debcount < 1000; debcount++){         //KEY_DEBTIME
        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);
}