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

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

  • 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

  • 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?

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

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

  • 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.

  • Login link:

    Nothing Found

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


  • >