Search code examples
cgpiomsp430

K60 family GPIO keyboard in C


I am trying to set up 4x4 matrix keyboard for MK60D10. I am using Port A.

My keyboard is connected to PTAxx like this:

#define KEYBOARD_ROW_1 8 // P1(23)
#define KEYBOARD_ROW_2 10 // P1(24)
#define KEYBOARD_ROW_3 6 // P1(25)
#define KEYBOARD_ROW_4 11 // P1(26)
#define KEYBOARD_COL_1 7 // P1(27)
#define KEYBOARD_COL_2 9 // P1(28)
#define KEYBOARD_COL_3 27 // P1(35)
#define KEYBOARD_COL_4 29 // P1(36)

where xx is number defined.

I understand that ROWs should be set as outputs and COLs as input, so I already set data direction for port a like this

PTA->PDDR = GPIO_PDDR_PDD(0b110101000000);

Now i am little stuck by how to read which key has been pressed. I am trying to understand concept and I understand that if there ROW 1 set to 0 (output) and COL 1 set to 1 (input) and I press button 1 (which is intersection of those two), they connect, but what does change?

Thanks for your help.


Solution

  • I assume your keyboard has normally-open contacts. The following is a common way to do it, but you can reverse the polarity (use pull-downs and so on).

    First of all, you need pull-up(s) on your input pins, because otherwise they could float. Unpressed button will read as 1. Then, the normal state of the rows is "1" (all up), i.e., you must keep them high most the time.

    Then you must actively poll the keyboard.

    Every poll cycle consists in lowering a single row, wait just a little, and read the inputs; only those inputs which, through a pressed button, are low, will read as "0". So, if you read a pin as 0, then the single button which connects that single row you drove, and that single column you have read, is pressed. Pull down the rows one after the other, in sequence. At any given moment only one row has to be low.

    Hope this is clear.