Showing posts with label 07- 4*4 keypad with 89c51. Show all posts
Showing posts with label 07- 4*4 keypad with 89c51. Show all posts

Monday, 3 September 2018

8051: Keypad Interface Code


Description:

            Interfacing Matrix Keypad with 8051 Micro controller. When the numbers pressed in the matrix keypad load corresponding logical state at the input of the micro controller i.e., port 3.In this program, they were written as header files using the (name).h file.In this program, r represents row and c represents column, port 2 first set as HIGH and the a (first row) set as 0 and r1(first column) set 0,and other columns set as 1 ,if the first key pressed port 3 blinks the corresponding binary value and vice versa.


Code:

#include<reg51.h>
#include<keypad.h>
int key();
int main()
{
            int k,n=0;
            while(1)
            {
                        k=key();
                        if(k<10)
                        {
                                    n=n*10+k;
                                    P3=n;
                        }
                       
            }
}

*---------------------------keypad.h(header file)-------------------------------------------*

sbit a=P2^0;
sbit b=P2^1;
sbit c=P2^2;
sbit d=P2^3;
sbit r1=P2^4;
sbit r2=P2^5;
sbit r3=P2^6;
sbit r4=P2^7;



int key()
{
            int x=16;
            P2=0xff;
            a=0;
            b=c=d=1;
            while(r1==0 && r2==1 && r3==1 && r4==1)
            {
                        x=7;
            }
            while(r1==1 && r2==0 && r3==1 && r4==1)
            {
                        x=8;
            }
            while(r1==1 && r2==1 && r3==0 && r4==1)
            {
                        x=9;
            }
            while(r1==1 && r2==1 && r3==1 && r4==0)
            {
                        x=12;
            }
           
            b=0;
            a=c=d=1;
            while(r1==0 && r2==1 && r3==1 && r4==1)
            {
                        x=4;
            }
            while(r1==1 && r2==0 && r3==1 && r4==1)
            {
                        x=5;
            }
            while(r1==1 && r2==1 && r3==0 && r4==1)
            {
                        x=6;
            }
            while(r1==1 && r2==1 && r3==1 && r4==0)
            {
                        x=13;
            }
           
           
            c=0;
            b=a=d=1;
            while(r1==0 && r2==1 && r3==1 && r4==1)
            {
                        x=1;
            }
            while(r1==1 && r2==0 && r3==1 && r4==1)
            {
                        x=2;
            }
            while(r1==1 && r2==1 && r3==0 && r4==1)
            {
                        x=3;
            }
            while(r1==1 && r2==1 && r3==1 && r4==0)
            {
                        x=14;
            }
           
            d=0;
            b=c=a=1;
            while(r1==0 && r2==1 && r3==1 && r4==1)
            {
                        x=10;
            }
            while(r1==1 && r2==0 && r3==1 && r4==1)
            {
                        x=0;
            }
            while(r1==1 && r2==1 && r3==0 && r4==1)
            {
                        x=11;
            }
            while(r1==1 && r2==1 && r3==1 && r4==0)
            {
                        x=15;
            }
            return x;
}




Circuit Diagram: