| ??? 02/27/03 21:54 Read: times |
#40270 - RE: Look up Table in \'c\' Responding to: ???'s previous message |
Here is some Keil C code with several lookup tables in it and the subroutine that uses them.
/*
**
** keypad scanning program to decode the keypad via column to row
** sensing and decoding into a valid key code. This routine is
** designed to be called repeatively by a timed interrupt routine
** to support keypad filtering, debounce, and key repeat/held
** down logic.
**
** The keypad connects to the microcontroller via the following
** connections. The numbers in the lower corner of each button
** the logical local key code for the button in hexadecimal.
**
** +------+ +------+ +------+ +------+ PU
** | |____| DOWN |____| |____| |___!____Row 0 Output
** | | | | | | | | P2.4
** +----0-+ +----1-+ +----2-+ +----3-+
** | | | |
** +------+ +------+ +------+ +------+ PU
** | LEFT |____| ENTER|____| RIGHT|____| |___!____Row 1 Output
** | | | | | | | | P2.5
** +----4-+ +----5-+ +----6-+ +----7-+
** | | | |
** +------+ +------+ +------+ +------+ PU
** | |____| |____| |____| LIGHT|___!____Row 2 Output
** | | | | | | | | P2.6
** +----8-+ +----9-+ +----A-+ +----B-+
** | | | |
** +------+ +------+ +------+ +------+ PU
** | |____| UP |____| |____| ESC |___!____Row 3 Output
** | | | | | | | | P2.7
** +----C-+ +----D-+ +----E-+ +----F-+
** | | | |
** | | | |
** | | | +----------- Col 3 Input
** | | | | P2.3
** | | | |
** | | +----------------------- Col 2 Input
** | | | | P2.2
** | | | |
** | +----------------------------------- Col 1 Input
** | | | | P2.1
** | | | |
** +----------------------------------------------- Col 0 Input
** | | | | P2.0
** X X X X
** X X X X
** | | | |
** +-----------+-----------+-----------+---- +3V
**
**
**
** The four columns P2.0 -> P2.3 are equipped with pullup resistors at the
** inputs to the microcontroller. In normal operation the
** columns are held high and placed low one column at a time
** while looking at the row inputs and seeing if there is
** a key pressed.
**
** scan codes are composed from the row and columns as two
** two bit pairs as CCRR to compose a nibble wise key code.
**
*/
/* globals that handle keyboard state logic */
unsigned char bdata KYBITS; /* place keyboard bits in bits area */
char data key_code; /* logical key code found in matrix */
char data key_flag; /* flag true of key_code has a key present */
/* 0 = no keypress present */
/* -1 = key in debounce state */
/* +1 = key is valid press */
#if REPEAT_ENABLE
char data key_cnt; /* used to count out the key repeat duration */
#endif
unsigned char xdata G_SYS_KeypadBuffer[KEYPAD_BUFFER_SIZE];
unsigned char data G_SYS_KeypadBufferHead; /* keypad queue pointers */
unsigned char data G_SYS_KeypadBufferTail;
/*
**
** routine to initialize the board scan port logic to the inactive
** state.
**
*/
void key_init(void)
{
P2 = 0xFF; /* init all of keyboard port as inputs */
key_code=0; /* clear keyboard status variables */
key_flag=0;
#if REPEAT_ENABLE
key_cnt=0;
#endif
G_SYS_KeypadBufferHead = 0; /* initialize the keypad queue to empty */
G_SYS_KeypadBufferTail = 0;
}
/* table to translate from the scan codes of the key matrix */
/* to the keyboard logical key codes. This contains zeros (KEY_NONE) for matrix */
/* positions that do not have a key located */
const char code key_xlat[16]={
/* col 0 col 1 col 2 col 3 (inputs) */
KEY_NONE, KEY_DOWN, KEY_NONE, KEY_NONE, /* row 0 output */
KEY_LEFT, KEY_ENTER, KEY_RIGHT, KEY_NONE, /* row 1 output */
KEY_NONE, KEY_NONE, KEY_NONE, KEY_LAMP, /* row 2 output */
KEY_NONE, KEY_UP, KEY_NONE, KEY_ESC, /* row 3 output */
};
/* this translates row number to low active row select bit */
const char code row_xlat[4]={
0xEF, /* row code 1110 */
0xDF, /* row code 1101 */
0xBF, /* row code 1011 */
0x7F, /* row code 0111 */
};
/* this prioritizes scan decode to columns 3->2->1->0. codes in */
/* table encode as low two bits of a nibble key matrix position */
const char code col_xlat[16]={
0x0, /* column code 0000 */
0x0, /* column code 0001 */
0x1, /* column code 0010 */
0x1, /* column code 0011 */
0x2, /* column code 0100 */
0x2, /* column code 0101 */
0x2, /* column code 0110 */
0x2, /* column code 0111 */
0x3, /* column code 1000 */
0x3, /* column code 1001 */
0x3, /* column code 1010 */
0x3, /* column code 1011 */
0x3, /* column code 1100 */
0x3, /* column code 1101 */
0x3, /* column code 1110 */
0x3, /* column code 1111 */
};
/*
**
** routine to scan the keypad matrix to detect keys that
** are pressed. This routine is designed to be called
** at a regular repeatitive rate. (100 Hz is recommended).
** a key is valid isf detected for two successive calls
** to this routine. A key detection is by placing a mask for
** this key code into the keys byte and then pushing the
** resulting mask for this key into the keyboard queue.
**
** The key scanner will repeat a key that is seen held
** down for an extended period of time causing repeated
** insertions of the code into the "key_inp" variable.
** it the KEY_REPEAT flag is defined as nonzero value
**
** This routine uses a single key lockout scheme and
** puts a priority on the scan order and stop at the
** first key seen. Is multiple keys are pressed the
** highest priority one takes precidence.
**
*/
void key_scan(void) using 1
{
char col;
char row;
char key;
unsigned char TempHead;
for(row=0; row<4; row++)
{
P2=row_xlat[row]; /* select a row output bit */
col=P2; /* fetch in the col data */
P2|=0xF0; /* force row bits high */
col=(col&0x0f)^0x0f; /* keep low bits and invert */
if(col != 0) /* if a col bit set then some key */
{
key=col_xlat[col]+(row<<2); /* get the matrix code */
key=key_xlat[key]; /* translate to key code */
if(key != KEY_NONE) /* is this a key code */
{
if(key_flag == 0) /* new key found */
{
key_flag=-1; /* setup the debounce state */
key_code=key;
return;
}
if(key == key_code) /* matching last time? */
{
if(key_flag < 0) /* 2nd sighting of same key */
{
key_flag=1; /* show valid key code */
#if REPEAT_ENABLE
key_cnt=KEY_DELAY; /* init for initial repeat */
#endif
/* test to see if room in queue for this */
/* keypress, buffer could be full */
TempHead = G_SYS_KeypadBufferHead+1;
if(TempHead >= KEYPAD_BUFFER_SIZE)
{
TempHead = 0;
}
if (TempHead != G_SYS_KeypadBufferTail)
{
/* room in queue add keyPress */
G_SYS_KeypadBuffer[G_SYS_KeypadBufferHead] = key;
/* TempHead already holds the new Head Index, just use it */
G_SYS_KeypadBufferHead = TempHead;
}
return;
}
#if REPEAT_ENABLE
if(key_cnt-- <= 0)
{
key_cnt=KEY_RATE; /* set period to repeat */
/* test to see if room in queue for the repeat */
/* keypress, buffer could be full */
TempHead = G_SYS_KeypadBufferHead+1;
if(TempHead >= KEYPAD_BUFFER_SIZE)
{
TempHead = 0;
}
if (TempHead != G_SYS_KeypadBufferTail)
{
/* room in queue add keyPress */
G_SYS_KeypadBuffer[G_SYS_KeypadBufferHead] = key;
/* TempHead already holds the new Head Index, just use it */
G_SYS_KeypadBufferHead = TempHead;
}
}
#endif
return;
}
else
{
key_flag=-1; /* setup debounce for this new one */
key_code=key;
return;
}
}
}
}
P2|=0xF0; /* force all column bits high */
/* no key seen so remove indication of any key */
key_code=0;
key_flag=0;
#if REPEAT_ENABLE
key_cnt=0;
#endif
}
Have Fun Michael Karas |
| Topic | Author | Date |
| Look up Table in 'c' | 01/01/70 00:00 | |
| RE: Look up Table in \'c\' | 01/01/70 00:00 | |
| RE: Look up Table in 'c' | 01/01/70 00:00 | |
| RE: Look up Table in \'c\' | 01/01/70 00:00 | |
| RE: Look up Table in 'c' | 01/01/70 00:00 | |
| RE: Look up Table in 'c' | 01/01/70 00:00 | |
| RE: Look up Table in \'c\' | 01/01/70 00:00 | |
| RE: Look up Table in 'c' | 01/01/70 00:00 | |
RE: Look up Table in \'c\' | 01/01/70 00:00 |



