electroSome

Reading Multiple Pressed Keys from Matrix Keypad using PIC Microcontroller

Matrix Keypad

Contents

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

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

 

Exit mobile version