Hey everyone! Here my 1st. post here. I need help writing a code that makes an Arduino UNO to read the 16 keys of a 4x4 matrix keypad (0,1,2,3,4,5,6,7,8,9,A,B,C,D,#,*).
I am trying to find the electric schematic of the keypad but, in the meantime, I got a tested C code example that works for the keypad and a 8051 based board.
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;
}
Thanks in advance for your help.