matrix keypad 2


Using 4x4 matrix keypad with MINI-MAX/AVR-C.
Each key causes a different ASCII message to be sent to the serial ports on MINI-MAX/AVR-C

Toolkit:AVR Development System

Location:/bipom/devtools/WinAVR/minimaxavrc/Examples/keypad4x4_2

Code Example


#define MAX_ROWS	4
#define MAX_COLS	4

static char* KeyStrings[] = { "Message1\r\n",	// row=1 col=1    

							  "Message2\r\n",	// row=1 col=2

							  "Message3\r\n",	// row=1 col=3

							  "Message4\r\n",	// row=1 col=4

							  "Message5\r\n",	// row=2 col=1

							  "Message6\r\n",	// row=2 col=2

							  "Message7\r\n",	// row=2 col=3

							  "Message8\r\n",	// row=2 col=4

							  "Message9\r\n",	// row=3 col=1

							  "Message10\r\n",	// row=3 col=2

							  "Message11\r\n",	// row=3 col=3

							  "Message12\r\n",	// row=3 col=4

							  "Message13\r\n",	// row=4 col=1

							  "Message14\r\n",	// row=4 col=2

							  "Message15\r\n",  // row=4 col=3

							  "Message16\r\n",  // row=4 col=4

							  };
						
static unsigned char RowTable[] = { 0xFE, 0xFD, 0xFB, 0xF7 };

char * ScanKeypad();


//*******************************************************************************

int main (void)
{
    char* pKey;

	// delay 500 milliseconds after power up

	_delay_ms(500);        

	// Initialize the serial port 1 to 19200 baud

	uart1Init(19200);

	
	// Set lower 4 bits of Port K as output, these are the rows of the keypad

	DDRK |= _BV(PK0);
	DDRK |= _BV(PK1);
	DDRK |= _BV(PK2);
	DDRK |= _BV(PK3);

	// Set upper 4 bits of Port H as input, these are the columns of the keypad

	
	PORTH |= _BV(PH4);     // Enable internal pull-up resistor for PH4 so it will not float

	PORTH |= _BV(PH5);     // Enable internal pull-up resistor for PH5 so it will not float

	PORTH |= _BV(PH6);     // Enable internal pull-up resistor for PH6 so it will not float

	PORTH |= _BV(PH7);     // Enable internal pull-up resistor for PH7 so it will not float

	
	DDRH &= ~_BV(PH4);     // Set PH4 as input

	DDRH &= ~_BV(PH5);     // Set PH5 as input

	DDRH &= ~_BV(PH6);     // Set PH6 as input

	DDRH &= ~_BV(PH7);     // Set PH7 as input



	uart1Printf("\n\rWaiting for key press\n\r");
	
	// Loop forever

	for( ;; )
	{
	    // Check to see if a key was pressed

		pKey = ScanKeypad();

        // If a key was pressed

		if( pKey != NULL )
		{
			// Print the message to serial port

			uart1Printf( pKey );
		}
	}			
}   	


//*******************************************************************************

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

	
	// Scan the keypad

	for( row=0; row<MAX_ROWS; row++ )
	{
		PORTK = RowTable[row];

		if( !(PINH & 0x80) ) col = 4;
		if( !(PINH & 0x40) ) col = 3;
		if( !(PINH & 0x20) ) col = 2;
		if( !(PINH & 0x10) ) col = 1;
			
		// Is any key pressed ?

		if( col != 0 )		
		{
		      // key debounce delay

			_delay_ms(500);

            // return the key that was pressed			

			return KeyStrings[col-1 + row*MAX_COLS];
		}					
	}
	
	return NULL;
}