Listing 31 continued KEYC

OSTimeDlyHMSM(0, 0, 0, KEY_SCAN_TASK_DLY) ; switch (KeyScanState) { case KEY_STATE__UP :

if (KeylsKeyDownO ) {

KeyScanState = KEY_STATE_DEBDUNCE; KeyDownTftir = 0;

break;

case KEY_STATE_DEBOUNCE: if (KeylsKeyDownO ) { code

KeyBufln(code) ;

/* Delay between keyboard scans */

/* See if key is pressed */ /* Next call we will have denounced the key */

/* Key pressed, get scan code and buffer /* See if key is pressed /* Determine the key scan code /* Input scan code in buffer

KeyRptStartDlyCtr = KEY_RPT_START_DLY; / * Start delay to auto-repeat function KeyScanState = KEYJ=TATE_RPr_START_DLY;

KeySelRcw(KEY_ALL_ROWS) ; KeyScanState = KEY_STATE_UP;

break;

case KEY_STATE__RFT_START_DLY : if (KeylsKeyDownO ) {

if (KeyRptStartDlyCtr > 0) { KeyRptS tartDlyCtr— ; if (KeyRptStartDlyCtr == 0) { code = KeyDecode ( ) ;

KeyBufln(code) ; KeyI^)tDlyCtr = KEY_RPT_DLY; KeyScanState = KEY_STATE_RPT_

KeyScanState = KEY_STATE_DEBOUNCE;

break ;

/* Select all row

/* Key was not pressed after all!

/* See if key is still pressed /* See if we need to delay before auto rpt /* Yes, decrement counter to start of rpt /* If delay to auto repeat is ccnpleted ... /* Determine the key scan code /* Input scan code in buffer /* Load delay before next repeat DLY;

/* Key was not pressed after all case KEY_STATE__RPT_DLY :

if (KeylsKeyDownO ) {

if (KeyRptDlyCtr > 0) { KeyRptDlyCtr— ; if (KeyRptDlyCtr == 0) {

KeyScanState

break;

KEY_STATE_DEBOUNCE ;

/* See if key is still pressed

/* See if we need to wait before repeat key

/* Yes, dec. wait time to next key repeat

/* Determine the key scan code

/* Input scan code in buffer

/* Reload delay counter before auto repeat

/* Key was not pressed after all

*************************************************************

* READ COLUMNS

* Description : This function is called to read the column port.

* Arguments : none

* Returns : the carplement of the column port thus, ones are keys pressed *********************************************************************************************************

(tifndef CPG_C

1NT8U KeyGetCol (void) {

return (~inp(KEY_PORT_COL) ) ; /* Complement columns (ones indicate key is pressed) */

#endif

*********************************************************************************************************

* INITIALIZE I/O PORTS

*********************************************************************************************************

#ifndef CPG_C void KeylnitPort (void) (

OUtp(KEY_POKT_CW, 0x82); /* Initialize 82C55: A=OOT, B=3N (COLS), C=OOT (ROWS) *I

********************************************************************************************************* * SELECT A ROW

* Description : This function is called to select a tcm an the keyboard.

* Arguments : 'row' is the row number (0..7) or KEY_ALL__RCWS

* Note : The rcw is selected by writing a LOW. *********************************************************************************************************

void KeySelRow (INrSU row) (

outp(KEY_PORT_ROW, 0x00); /* Force all rows I£W */

outp{KEY_PORT_RCW, ~(1 « row)); /* Force desired row LCW */

#endif

Listing 3.2 KEY.H

Embedded Systems Building Blocks Carplete and Ready-to-Use Modules in C

Matrix Keyboard Driver

(c) Copyright 1999, Jean J. Labrosse, Weston, EL All Rights Reserved

* Programmer : Jean J. Labrosse

user defined constants

Note: These #defines would normally reside in your application specific code. ******************************************************************************************

«ifndef CPGH #define key_buf_size

10 /* Size of the KEYBOARD buffer

#define KEY_PORT_ROW

#define KEY_PORT_COL

#define KEY_PORT_CW

#define KEY_MAX_ROWS

«define KEY_MAX_COLS

«define KEY_RPT_DLY

«define KEY_RPr_START_DLY

«define KEY_SCAN_TASK_DLY

«define KEY_SCAN_TASK_PRIO

«define KEY_SCAN_TASK_STK_SIZE

«define KEY_SHIET1_MSK

«define KEY_SHIET1_0FFSET

«define KEY_SHIET2_MSK

«define KEY_SHIET2_0FFSET

«define KEY_SHIET3_MSK

«define KEY_SHIET3_OFFSET «endif

0x0312 /* The port address of the keyboard matrix ROWs *

0x0311 /* The port address of the keyboard matrix COLUMNS *

0x0313 /* The port address of the I/O ports control word *

4 /* The maximum number of rcws on the keyboard *

6 /* The maximum number of columns on the keyboard *

2 /* Number of scan times before auto repeat executes again * 10 /* Number of scan times before auto repeat function engages*

50 /* Number of milliseconds between keyboard scans *

50 /* Set priority of keyboard scan task *

1024 /* Size of keyboard scan task stack *

0x80 /* The SHIET1 key is on bit B7 of the column input port *

/* (A 0x00 indicates that a SHIET1 key is not present) *

24 /* The scan code offset to add when SHIFT1 is pressed *

0x40 /* The SHIET2 key is on bit B6 of the column input port *

/* (A 0x00 indicates that an SHIET2 key is not present) *

48 /* The scan code offset to add when SHIFT2 is pressed *

0x00 /* The SHIET3 key is on bit B5 of the column input port *

/* (A 0x00 indicates that a SHIET3 key is not present) *

0 /* The scan code offset to add when SHIET3 is pressed *

«define KEY_ALL_ROWS OxFF /* Select all rows (i.e. all rows LOW)

Was this article helpful?

0 0

Post a comment