This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Key de-bouncing in 4x4 matrix....

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)
                  {






Parents
  • 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;
    

Reply
  • 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;
    

Children