hi , i am not well in english ,try to understand my language.
here i am using 4x4 matrix keypad ....
problem: key de bouncing problem
requirement: when i press key and i hold it some time. i want to display only one time that key in LCD but it is not displayed one time (so many numbers are printed on lcd)...
check my code
#include <LPC177x_8x.H> #include "7Seg.h" #include "lcd.h" unsigned char keypad (void) { unsigned char key; LPC_GPIO1->CLR|=(c1|c2|c3|c4|r1|r2|r3|r4|r5); while(1) { LPC_GPIO1->CLR|=c1; LPC_GPIO1->SET|=(c2|c3|c4); // first column = 0 if((LPC_GPIO1->PIN&r1)==0) { Delay_ms(100); if((LPC_GPIO1->PIN&r1)==0) { key='0'; keypad_delay(); return key; } } if((LPC_GPIO1->PIN&r2)==0) { Delay_ms(100) if((LPC_GPIO1->PIN&r2)==0) { key='4'; keypad_delay(); return key; } } if((LPC_GPIO1->PIN&r3)==0) { Delay_ms(100); if((LPC_GPIO1->PIN&r3)==0) {
Consider separating the scanning from the processing. So remove your delays from the scanning - the scanning is fine with just enough delays that each signal line has time to stabilize before you read out a value. And for most situations, the signals stabilizes "instantly" compared to the speed of the processor.
Require the keyboard scan to produce the same key press n times in a row - then accept it as a press. Then stop accepting key presses and instead require the keyboard scan to produce "empty" n times in a row. Then back to wait for a new key press.
res = scan_keyboard(); if (waiting_for_key) { // Waiting for a distinct key press for n scan iterations. if (res == -1) { consecutive_samples = 0; last_res = -1; } else { if (last_res != res) { consecutive_samples = 1; last_res = res; } else if (++consecutive_samples >= debounce_limit) { waiting_for_key = false; return res; // return the pressed key } } } else { // Waiting for distinct keyboard idle for n scan iterations. if (res == -1) { if (++consecutive_samples >= debounce_limit) { waiting_for_key = true; last_res = -1; } } else { consecutive_samples = 0; } } // no key pressed // or multiple keys pressed // or state not properly debounced yet return -1;
Thanx for your reply,
from u r code and u r explanation i have understand how to use key scanning ....finally i got output...i am doing project on"PULSE GENERATOR"..... i am using keypad with LCD 4x20 display in that by using key pad i manage cursor moving positions .....when i press down key the cursor moves to very fastly from 1st line to 4th line ..... my requirement is when i press key (Holding some time) cursor position goes to next line only...that is requirement .....finally i got it ....thanx for u r help...and if u want more clarification about my project just reply me .....
i am using this logic in timer_IRQ......
if(((LPC_GPIO1->PIN&r5)!=0)&((LPC_GPIO1->PIN&r3)!=0)) { key_pad=0; }
My short demo code already handles the issue of having the down key only move one step down unless you press it multiple times with release in between.
And the demo code is trivial to extend to also handle long presses - to either generate key repeats at some suitable frequency for a long press or to produce an alternative return code.