Keypad (4x4)


Example of connecting a 4x4 keypad to MINI-MAX/51-C

Toolkit:SDCC 8051 Development System

Location:/bipom/devtools/SDCC/examples/keypad/4x4/

Code Example


#define MAX_ROWS	4
#define MAX_COLS	4

                
static char KeyTable[] = { 	'1', '2', '3', 'A',
							'4', '5', '6', 'B',	 
							'7', '8', '9', 'C',
							'*', '0', '#', 'D'
						 };
						
static unsigned char RowTable[] = { 0xFE, 0xFD, 0xFB, 0xF7 };


char ScanKeypad();


main()
{
	char key;

	serinit(CBR_19200);
	printf ("\nKeypad example");
	
	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 & 0x80) )		
			col = 4;

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