Not sure why the diodes aren't working.
On the software side... I wonder if converting keypad row and column into an index might make it easier to maintain or modify a keymap array that mimics the layout of the actual keypad?
Code: Select all
#define bitmask(bitnbr) (1 << (bitnbr))
const char keymap[] = { '0','1','2','3', // row 0, columns 0..3
'4','5','6','7', // row 1, columns 0..3
'8','9','A','B', // row 2, columns 0..3
'C','D','E','F', // row 3, columns 0..3
0 }; // null (no switch pressed)
/********************************************
* Scan Keypad *
********************************************/
char scan(void) // scan keypad matrix
{ char keyndx = 16; // null (no switch pressed)
for(char col = 0; col < 4; col++) // column 0..3
{ DDRD = bitmask(col); // set 'col' bit to output, all others inputs
PORTD = ~(bitmask(col)); // set 'col' bit to '0', pull-up all other pins
for(char row = 0; row < 4; row++) // rows 0..3
{ if(PIND & bitmask(row + 4) == 0) // if active low key press (row bit == '0')
keyndx = row * 4 + col; // save index, 0..15
} // next row
} // next col
return(keymap[keyndx]); // return 0 or ASCII keycode
}