/* Tommy Thorn 1999 Generic interface to a 4x4 matrix keyboard. This interface assumes that all 8 lines are connected to one port with the row and columns bits consecutive. In my case the matrix is wired as such 1 2 3 A row 1 4 5 6 B row 2 7 8 9 C row 3 * 0 # D row 4 col 1 2 3 4 col1 .. col4 row1 .. row4 are mapped directly to one port. The most obvious scheme for decoding would be to map the cols the outputs and rows to inputs and scan one column at the time. Unfortunately that would risk shorting the outputs should the user press down several keys from the same row. Instead we just enable one column at a time as output, letting all the rest be inputs with pullups (Only AVRs can do this?) The lowest level interface is very simple: unsigned int scan() `scan' simply returns a 16 bit bitmap with ones where keys are pressed. The higher level `int getch()' loops (blocks) until a key is pressed and released and returns it's number. */ #include #define KEYPORT PORTB #define OUT 0 #define DDR -1 #define IN -2 unsigned int scan();