Reading Multiple Pressed Keys from Matrix Keypad using PIC Microcontroller

Reading Multiple Pressed Keys from Matrix Keypad using PIC Microcontroller

I have already post about Interfacing Matrix Keypad with PIC Microcontroller. I suggest to read that article before reading this. In some applications it may require to scan more than one key at a time. Reading Multiple Pressed Keys from Matrix Keypad is not simple as reading Single key. We want to change the program as well as the circuit of matrix keypad for reading multiple keys. The main problem is unexpected shorts may come to act when we press more than one key at a time. These unexpected shots will cause error in detecting pressed keys. This problem can be avoided by using a diode series with every switch. This diode will prevent unexpected shorts by allowing current only in one direction.

Circuit Diagram

Reading Multiple Pressed Keys from Matrix Keyboard using PIC Microcontroller

Matrix Keypad to read Multiple Pressed Keys

 Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

In the circuit diagram, you can see that diode is connected in series with each switches to avoid undesired shorts arises due to multiple  pressing of keys. In this example a 16*2 LCD Display is used to display all the pressed keys.

MikroC Programming

Function to Scan Keypad

void readKeyboard()
{
  unsigned int i, k = 0;
  for(i=0;i<4;i++)
  {
    if(i == 0)
      PORTD = 1;
    else if(i == 1)
      PORTD = 2;
    else if(i == 2)
      PORTD = 4;
    else if(i == 3)
      PORTD = 8;
    Delay_ms(50);
    if(PORTD.F4)
    {
       keysPressed[k] = findKey(i,0);
       k++;
    }
    if(PORTD.F5)
    {
      keysPressed[k] = findKey(i,1);
      k++;
    }
    if(PORTD.F6)
    {
      keysPressed[k] = findKey(i,2);
      k++;
    }
    if(PORTD.F7)
    {
      keysPressed[k] = findKey(i,3);
      k++;
    }
  }
  keysPressed[k] = '\0';
}

This function initiates the matrix keypad scanning and it detects all the pressed keys. It stores character corresponds to each pressed key in the array named keysPressed[]. This function uses the function findKey() to find the character corresponds to a pressed key.


Function to Find Key

char findKey(unsigned short a, unsigned short b)
{
  if(b == 0)
  {
    if(a == 3)
      return '0';
    else if(a == 2)
      return '1';
    else if(a == 1)
      return '2';
    else if(a == 0)
      return '3';
  }
  else if(b == 1)
  {
    if(a == 3)
      return '4';
    else if(a == 2)
      return '5';
    else if(a == 1)
      return '6';
    else if(a == 0)
      return '7';
  }
  else if(b == 2)
  {
    if(a == 3)
      return '8';
    else if(a == 2)
      return '9';
    else if(a == 1)
      return 'A';
    else if(a == 0)
      return '-';
  }
  else if(b == 3)
  {
    if(a == 3)
      return 'C';
    else if(a == 2)
      return 'U';
    else if(a == 1)
      return 'E';
    else if(a == 0)
      return 'F';
   }
}

This function returns the character corresponds to a particular row and column of the matrix keypad. You may edit this function to change character corresponds to each key

 

Share this post

Comments (21)

  • stefan3v

    download does not work. I am registered and logged in.

    December 13, 2012 at 4:04 am
  • Ligo George

    Download is working fine……………This is the download link…

    http://www.electrosome.com/wp-content/plugins/download-monitor/download.php?id=11

    Go to this links after login…You can login at http://www.login.electrosome.com

    December 13, 2012 at 7:03 am
  • Maribel Tolentino

    Login link:

    Nothing Found

    Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.

    July 5, 2013 at 5:07 pm
  • Ligo George

    Now, you don’t require login to download…

    July 5, 2013 at 6:39 pm
  • Dumken

    Couldn’t download in ma system.

    December 11, 2013 at 3:57 pm
  • Rakib

    Hi
    your tutorial here matrix keypad. But I want same work by 2 buttons.
    When press 1st button i want it show 1 number . and while press again it’ll increase.
    When press 2nd button number will decrease.

    March 28, 2015 at 5:58 pm
  • Syed

    Can’t seem to find any link to download this. Could you possibly make it available?
    Thanks!

    April 6, 2015 at 6:02 pm
  • Syed

    Any help would be appreciated!

    April 8, 2015 at 12:16 am
  • Syed

    Can someone share the code for this project please? Need to look through the keypad+LCD routine.
    No reply so far!

    April 9, 2015 at 4:01 am
  • Ligo George

    Sorry, those files are not available now. Buy you can make it using above two functions.

    April 13, 2015 at 10:24 am
  • Ligo George

    You can make it .. using above two functions…
    Just call the function to read keypad.. and print in on the LCD.

    April 13, 2015 at 10:35 am
  • Syed

    Right, how can this be modified to just output an index of 1-16 rather than actual characters? Was looking for something more generic. Is it possible to know the main settings for the ports like TRIS etc?

    April 13, 2015 at 1:55 pm
  • Ligo George

    Complete code is published above.. you can modify it as per your application.

    April 27, 2015 at 9:56 pm
  • A. Lahiru

    Can anyone upload the MPLAB XC8 code?

    April 22, 2016 at 12:00 am
  • Ligo George

    About program is not using any MikroC specific functions. So you can easily convert it to MPLAb.

    June 2, 2016 at 6:12 pm
  • Yoyo Hedia

    Hello
    I don’t understand the condition of if in keyspad function if (portd.fx)
    Which x is=4,5,so on.
    Thnx in advance

    October 14, 2016 at 6:59 pm
  • Yoyo Hedia

    Hello
    I dont understand the condition of if (portd.fx)
    Which x=4,5&so on
    Thnx in advance

    October 14, 2016 at 7:20 pm
  • Ligo George

    if(PORTD.Fx) is same is if(PORTD.Fx == 1)

    November 27, 2016 at 1:15 pm
  • Farah Mansor

    can i replace the diode n the switch with 4×4 keypad and use the same coding to simulate

    November 30, 2016 at 4:01 pm
  • Ligo George

    If you are using Keypad, you won’t be able to connect diodes like that.

    December 1, 2016 at 10:35 am

Leave a Reply

Your email address will not be published. Required fields are marked *