Keypad (3x5)


Example of connecting a keypad to 8051

Toolkit:SDCC 8051 Development System

Location:/bipom/devtools/SDCC/examples/keypad/3x5/

Code Example


#define MAX_ROWS	5
#define MAX_COLS	3

                
static char KeyTable[] = { 	'1', '2', '3', 
							'4', '5', '6',
							'7', '8', '9',
							'*', '0', '#',
							'!', '$', '&' };
						
static unsigned char RowTable[] = { 0xEF, 0xBF, 0x7F, 0xFD, 0xFE };
static unsigned char ColTable[] = { 0xB0, 0x38, 0xA8 };


char ScanKeypad();


main()
{
	char key;

	serinit(CBR_19200);
	
	for( ;; )
	{
		key = ScanKeypad();

		if( key )
		{
			printf( "\nKey: '%c'", key );
		}
	}		
}


char ScanKeypad()
{
	char row;
	char col;
	
	col = 0;
	
	for( row=0; row<MAX_ROWS; row++ )
	{
		P2 = RowTable[row];

		if( !(P2 & 0x04) )		
			col = 1;
		
		if( !(P2 & 0x08) )
			col = 2;
			
		if( !(P2 & 0x20) )
			col = 3;
			
		if( col != 0 ) 						
		{
			delay(500);
			return KeyTable[col-1 + row*MAX_COLS];
		}					
	}
	
	return 0;
}