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

Leave a Reply

21 Comments on "Reading Multiple Pressed Keys from Matrix Keypad using PIC Microcontroller"

avatar
  Subscribe  
newest oldest most voted
Notify of
stefan3v
Guest

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

Ligo George
Guest

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

Maribel Tolentino
Guest

Login link:

Nothing Found

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

Ligo George
Guest

Now, you don’t require login to download…

Dumken
Guest

Couldn’t download in ma system.

Rakib
Guest

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.

Syed
Guest

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

Syed
Guest

Any help would be appreciated!

Syed
Guest

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

Ligo George
Guest

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

Ligo George
Guest

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

Syed
Guest

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
Guest

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

Guest

Can anyone upload the MPLAB XC8 code?

Ligo George
Guest

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

Yoyo Hedia
Guest

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

Yoyo Hedia
Guest

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

Ligo George
Guest

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

Farah Mansor
Guest

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

Ligo George
Guest

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