Wednesday 18 February 2015

interfacing of matrix keypad with 8051


Matrix Keypad: Matrix Keypads are commonly used in calculators, telephones etc where a number of input switches are required. We know that matrix keypad is made by arranging push button switches in row and columns. In the straight forward way to connect a 4×4 keypad (16 switches) to a microcontroller we need 16 inputs pins. But by connecting switches in the following way we can read the status of each switch using 8 pins of the microcontroller.



Interfacing of matrix keypad with 8051:


1)     
a)    Circuit:




b)    Code:
·        ASM Code:
$mod51
org 00h
mov P1,#01h
acall command
mov P1,#06h
acall command
mov P1,#38h
acall command
mov P1,#0eh
acall command
mov P1,#80h
acall command
back:

mov P2,#11111110b
Jnb P2.4,S1
JNB P2.5,S2
JNB P2.6,S3
JNB P2.7,S4


mov P2,#11111101b
Jnb P2.4,S5
JNB P2.5,S6
JNB P2.6,S7
JNB P2.7,S8

mov P2,#11111011b
Jnb P2.4,S9
JNB P2.5,S10
JNB P2.6,S11
JNB P2.7,S12

mov P2,#11110111b
Jnb P2.4,S13
JNB P2.5,S14
JNB P2.6,S15
JNB P2.7,S16
sjmp back
S1: mov P1,#'A'
    Acall display
    Ljmp back
S2: mov P1,#'B'
    Acall display
    Ljmp back

S3: mov P1,#'C'
    Acall display
    Ljmp back

S4: mov P1,#'D'
    Acall display
    Ljmp back

S5: mov P1,#'E'
    Acall display
    Ljmp back

S6: mov P1,#'F'
    Acall display
    Ljmp back

S7: mov P1,#'G'
    Acall display
    Ljmp back

S8: mov P1,#'H'
    Acall display
    Ljmp back

S9: mov P1,#'I'
    Acall display
    Ljmp back

S10: mov P1,#'J'
    Acall display
    Ljmp back

S11: mov P1,#'K'
    Acall display
    Ljmp back

S12: mov P1,#'L'
    Acall display
    Ljmp back

S13: mov P1,#'M'
    Acall display
    Ljmp back

S14: mov P1,#'N'
    Acall display
    Ljmp back

S15: mov P1,#'O'
    Acall display
    Ljmp back

S16: mov P1,#'P'
    Acall display
    Ljmp back
command:
clr P3.0
clr P3.1
setb P3.2
acall delay
clr P3.2
ret
                        display:
setb P3.0
clr P3.1
setb P3.2
acall delay
clr P3.2
ret


delay:
mov R1,#100
  L1: mov R2,#100
   L2: mov R3,#50
        L3: Djnz R3,L3
      DjnZ R2,L2
     Djnz R1,L1
Ret
end

·        C Code:
#include<reg51.h>
sbit RS=P3^0;
sbit RW=P3^1;
sbit En=P3^2;

 sbit col1=P2^4;
 sbit col2=P2^5;
 sbit col3=P2^6;
 sbit col4=P2^7;

 int k;
 unsigned char b[]={0x01,0x06,0x80,0x0e,0x38};
  void delay()
 
  {
  for(k=0;k<35;k++)
  {
   TMOD=0x01;
   TH1=0x00;
   TL1=0x00;
   TR1=1;
   while(TF1==0);
   TF1=0;
   TR1=0;
   }
  }
 void command(unsigned char x)
 {
   P1=x;
   RS=0;
   RW=0;
   En=1;
   delay();
   En=0;
  }
   void display(unsigned char x)
 {
   P1=x;
   RS=1;
   RW=0;
   En=1;
   delay();
   En=0;
  }
  int j;
  void main()
  {
    For (j=0;j<5;j++)
       {
        command(b[j]);
       }
   while(1)
   {  P2=0xff;
      P2=0xfe;
         delay();
       if(col1==0)
       {
        command(0x01);
        display('A');
       }
       else if(col2==0)
       { command(0x01);
        display('B');
       }
       else if(col3==0)
       { command(0x01);
        display('C');
       }
       else if(col4==0)
       { command(0x01);
        display('D');
       }
       P2=0xfd;
       if(col1==0)
       {
        command(0x01);
        display('E');
       }
       else if(col2==0)
       { command(0x01);
        display('F');
       }
       else if(col3==0)
       { command(0x01);
        display('G');
       }
       else if(col4==0)
       { command(0x01);
        display('H');
       }
   P2=0xfb;
   if(col1==0)
       {
        command(0x01);
        display('I');
       }
       else if(col2==0)
       { command(0x01);
        display('J');
       }
       else if(col3==0)
       { command(0x01);
        display('K');
       }
       else if(col4==0)
       {
        command(0x01);
        display('L');
       }
       P2=0xf7;
       if(col1==0)
       {
        command(0x01);
        display('M');
       }
       else if(col2==0)
       { command(0x01);
        display('N');
       }
       else if(col3==0)
       { command(0x01);
        display('O');
       }
       else if(col4==0)
       { command(0x01);
        display('P');
       }
   }
  }


c)     Simulation:












0 comments:

Post a Comment

if you have any doubt please let me know