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

  • Ligo George says:

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

  • Farah Mansor says:

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

  • Ligo George says:

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

  • Yoyo Hedia says:

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

  • Yoyo Hedia says:

    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

  • Ligo George says:

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

  • A. Lahiru says:

    Can anyone upload the MPLAB XC8 code?

  • Ligo George says:

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

  • Syed says:

    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?

  • Ligo George says:

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

  • Ligo George says:

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

  • Syed says:

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

  • Syed says:

    Any help would be appreciated!

  • Syed says:

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

  • Rakib says:

    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.

  • Dumken says:

    Couldn’t download in ma system.

  • Ligo George says:

    Now, you don’t require login to download…

  • Maribel Tolentino says:

    Login link:

    Nothing Found

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

  • Ligo George says:

    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

  • stefan3v says:

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


  • >