Scanning a Matrix Keypad with MINI-MAX/51


This example demonstrates how to scan a matrix keypad such as KP1-4X4 with MINI-MAX/51 boards. This particular keypad has 4 columns and 4 rows. Rows and columns are connected to Port 2 (P2) of 8051. The example sets each one of the row pins to a low state, one at a time and then reads the columns to see if any of the rows are physically connected to a column due to a key press. KeyTable[] array holds the actual characters that map to the activated row and column. delay(500) provides a small delay for key debouncing.

Toolkit:Micro C 8051 Development System

Location:\bipom\devtools\MicroC\Examples\8051\medium\Keypad

Code Example


char ScanKeypad()
{
	char row;
	char col;
	
	col = 0;  //  current column 

	
	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;
}