How to interface keypad with FRDM K64

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to interface keypad with FRDM K64

1,387 Views
oliviachristyva
Contributor III

Hi guys,

I have to interface 4*4 button pad with FRDM K64 . Please give me some possible  solutions and  examples if you have for reference.How to implement this using interrupts in MQX.

Please help

Thanks and Regards,

Olivia

0 Kudos
5 Replies

824 Views
oliviachristyva
Contributor III

Hi guys,

This is a polling code for interfacing keypad in MQX.

while(1)
{
lwgpio_set_value(&Column1,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Column2,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Column3,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Column4,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row1,LWGPIO_VALUE_LOW);
lwgpio_set_value(&Row2,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row3,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row4,LWGPIO_VALUE_HIGH);
if((lwgpio_get_value(&Column1) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column1) == 0));
printf("Key pressed: 1\n");
}
if((lwgpio_get_value(&Column2) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column2) == 0));
printf("Key pressed: 2\n");
}
if((lwgpio_get_value(&Column3) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column3) == 0));
printf("Key pressed: 3\n");
}
if((lwgpio_get_value(&Column4) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column4) == 0));
printf("Key pressed: A\n");
}
lwgpio_set_value(&Row1,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row2,LWGPIO_VALUE_LOW);
lwgpio_set_value(&Row3,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row4,LWGPIO_VALUE_HIGH);
if((lwgpio_get_value(&Column1) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column1) == 0));
printf("Key pressed: 4\n");
}
if((lwgpio_get_value(&Column2) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column2) == 0));
printf("Key pressed: 5\n");
}
if((lwgpio_get_value(&Column3) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column3) == 0));
printf("Key pressed: 6\n");
}
if((lwgpio_get_value(&Column4) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column4) == 0));
printf("Key pressed: B\n");
}
lwgpio_set_value(&Row1,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row2,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row3,LWGPIO_VALUE_LOW);
lwgpio_set_value(&Row4,LWGPIO_VALUE_HIGH);
if((lwgpio_get_value(&Column1) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column1) == 0));
printf("Key pressed: 7\n");
}
if((lwgpio_get_value(&Column2) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column2) == 0));
printf("Key pressed: 8\n");
}
if((lwgpio_get_value(&Column3) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column3) == 0));
printf("Key pressed: 9\n");
}
if((lwgpio_get_value(&Column4) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column4) == 0));
printf("Key pressed: C\n");
}
lwgpio_set_value(&Row1,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row2,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row3,LWGPIO_VALUE_HIGH);
lwgpio_set_value(&Row4,LWGPIO_VALUE_LOW);
if((lwgpio_get_value(&Column1) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column1) == 0));
printf("Key pressed: *\n");
}
if((lwgpio_get_value(&Column2) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column2) == 0));
printf("Key pressed: 0\n");
}
if((lwgpio_get_value(&Column3) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column3) == 0));
printf("Key pressed: #\n");
}
if((lwgpio_get_value(&Column4) == 0))
{
_time_delay(10);
while((lwgpio_get_value(&Column4) == 0));
printf("Key pressed: D\n");
}
}
}

initialize rows and columns by 

mentioned only one pin here.

#define R1  (GPIO_PORT_C | GPIO_PIN11)  //Row 1

#define R1_MUX_GPIO           (LWGPIO_MUX_C11_GPIO)

if (!lwgpio_init(&Row1, R1, LWGPIO_DIR_OUTPUT,
LWGPIO_VALUE_LOW))
{
printf("Initializing Row 1 GPIO as output failed.\n");
}
else
{
// switch pin functionality (MUX) to GPIO mode
lwgpio_set_functionality(&Row1, R1_MUX_GPIO);
}

trying to generate a code for keypad using interrupts. will post it as soon i get it.

Thanks and regards,

Olivia

824 Views
oliviachristyva
Contributor III

Hi Guys,

Thank you all for your reply. I wrote a code using processor expert with interrupts. 4 output GPIO(rows) and 4 Input/Output GPIOs for columns.Column is initially 1. Then row is 0. On key press it comes to this interrupt.

This is working this is detecting the key press. but not working fine. it is not fast. keypress is not detected at the first press.

Code for only one column is here

void C1Int_OnInterrupt(LDD_TUserData *UserDataPtr)
{

if(C1_GetVal() == 0)
{
R1_SetVal();
if(C1_GetVal() == 1)
update(1);
else
{
R2_SetVal();
if(C1_GetVal() == 1)
update(4);
else
{
R3_SetVal();
if(C1_GetVal() == 1)
update(7);
else
update('*');
}
}
}

}

what am i doing wrong

Please help

Thanks and regards

Olivia

0 Kudos

824 Views
egoodii
Senior Contributor III

Even if you DON''T have external filters/protection on the row and column pins you shouldn't expect a 'row' change to IMMEDIATELY reflect as a corresponding column change.

And of course the 'noisy' press-event edge means you MAY get signaled for this interrupt, but WHILE YOU ARE LOOKING the 'bouncy activation' COULD be in a subsequent 'open period' and you will be unable to detect anything.  That's a main reason we all SCAN these kinds of inputs on a timer-driven polling interval -- that also leaves you free to 'make a change' to the row pattern as you exit ONE poll interval, and allow the 'whole poll interval' for the column read-back to settle for a 'read' coming into the next interval, without 'wasting time' waiting for it.  For four rows to scan, I might do 'one piece' of the scan every 5ms, when you 'find the key' lock on to it until you get 5 stable intervals 'on', call that 'activate', and when you see 5 stable 'off' call that 'deactivate' (and return to all-rows-on to look for ANY new keypress).

824 Views
mjbcswitzerland
Specialist V

Hi

See http://www.utasker.com/docs/uTasker/uTaskerKeypad.PDF

Regards

Mark

Professional support for Kinetis: http://www.utasker.com/index.html
Remote desktop one-on-one coaching: http://www.utasker.com/services.html
Getting started to expert videos: https://www.youtube.com/results?search_query=utasker+shorts

0 Kudos

824 Views
egoodii
Senior Contributor III

In general, one doesn't put 'user input switches' on any kind of interrupt, as the 'edge events' are VERY messy.  One would 'poll' such things, say at 5ms intervals, and 'debounce' any sensed activity for 50ms (more for some kinds of 'resistive pill' elastomeric actuations).

The 'general process' would be to have 4 'output' GPIO, and 4 'input' to scan your matrix.  Put pull-downs on the inputs (fairly low value, even down to 1K, for noise immunity).  Leave your four column-outputs driving all 'high' most of the time, and at each 'check interval' see if ANY input has been pulled-high, and in THAT case switch-over to a 'scanning' mode, where you FLOAT 3 of the 4 columns, and check your inputs again, and scan across those columns one-high-at-a-time until you find an active input.  If you see the 'same result' for 10 tick-driven-scans in a row, you call that an input-press.  When your scans 'come up empty' for 10 scans in a row, you go back to 'idle mode' with all columns driven 'high' again, and then each 'tick' again just looks for 'anything in' or just quits.  You don't mention if you are looking just for 2-key rollover, or full N-key rollover -- the latter requires diodes in the matrix, and more sophisticated scanning that looks for a continuation of multiple combinations.

0 Kudos