Skip to main content

How to interface keypad with AT89C51

hi, how are you today



Let's learn how to interface a KEYPAD with our baby (8051uC)

Description

 This article helps you to understand the concept behind the interfacing of keypad with at89c51. Keypad is a widely used input device in 90% of electronics devices and plays an important role in our life. From a small AC/TV remote  to huge Laptop/Desktop. Keypad is designed to take user input to perform any function.
       Here I am going to demonstrate you that how you can assign value to keypad for
performing any operation. Here I interfaced a LCD where we will check the output of
the ket pressed on keypad, but you can also use 7-segment to display numeric value or
LED's to display different patterns.

Here we use a 4x4 matrix keypad which is connected to port 1 and a LCD connected to

port3. So 4x4 means that we have 4 rows and 4 columns, first we connect 4 rows to
first 4 pins and 4 columns to rest 4 pins of any port1 of at89c51 microcontroller.

To learn programming of keypad will be simple if you remember the below mentioned

series of steps...

Set all columns to 1 -----> Set all rows to 0 -----> Scanning of key pressed ---

--> Find row location -----> Find column location -----> Display the data on LCD

Here we learn how to take input from keypad and display the same on LCD as an output

so that we can check as keypad is working as per the values mentioned to keypad.

To understand this programming keep 6 steps in mind

  1. Set all columns to 1
  2. Set all rows to 0
  3. Scan which key is pressed
  4. Find row location
  5. Find column location
  6. Display data

 Programming

1:  #include<reg51.h>  
2:  sfr lcd=0xB0;  
3:  sbit rs=P2^5;  
4:  sbit rw=P2^6;  
5:  sbit en=P2^7;  
6:  sbit row1=P1^0;  
7:  sbit row2=P1^1;  
8:  sbit row3=P1^2;  
9:  sbit row4=P1^3;  
10:  sbit col1=P1^4;  
11:  sbit col2=P1^5;  
12:  sbit col3=P1^6;  
13:  sbit col4=P1^7;   
14:  void delay (int time)  
15:  {  
16:  int i,j;  
17:  for (i=0;i<=time;i++)  
18:  for (j=0;j<=1275;j++);  
19:  }   
20:  void lcmd(unsigned char m) // function to send command to LCD  
21:     {  
22:    lcd=m;  
23:    rs=0;  
24:    rw=0;  
25:    en=1;  
26:    delay(10);  
27:    en=0;  
28:   }  
29:  void ldata(unsigned char m) // function to send data to LCD  
30:     {  
31:    lcd=m;  
32:    rs=1;  
33:    rw=0;  
34:    en=1;  
35:    delay(10);  
36:    en=0;  
37:   }    
38:  void display(unsigned char *str) // Function to send string to LCD  
39:     {  
40:      int i=0;  
41:      while(str[i]!='\0')  
42:        {  
43:         ldata(str[i]);  
44:         i++;  
45:      delay(1);  
46:        }  
47:   }  
48:  unsigned char keypad [4][4]={'%','9','8','7',  
49:                        'x','6','5','4',  
50:                        '-','3','2','1',  
51:                        '+','=','0','#'};  
52:  unsigned char colloc,rowloc; //char to assign row and column  
53:  void main()  
54:       {  
55:        lcmd(0x38); delay(10);  
56:        lcmd(0x0E); delay(10);  
57:        lcmd(0x01); delay(10);  
58:        lcmd(0x06); delay(10);  
59:        lcmd(0x80); delay(10);  
60:        display("Salah Tutorials");  
61:        lcmd(0xc0); delay(10);  
62:        display("by Er MS Dandyan");  
63:        delay(50);  
64:        lcmd(0x01);  
65:        lcmd(0x80); delay(10);  
66:        display("Here we go with ");  
67:        lcmd(0xc0); delay(10);  
68:        display("Keypad + LCD  ");  
69:        delay(50);  
70:        lcmd(0x01);  
71:        lcmd(0x80); delay(10);  
72:        display("*Press Any Key* ");  
73:        lcmd(0xc0);  
74:             /* Set columns to 1 */  
75:        col1=1;col2=1;col3=1;col4=1;  
76:        while (1)      
77:           {  
78:           do  
79:            {  
80:             /* set row to 0 end of scanning process */  
81:             row1=0;row2=0;row3=0;row4=0;  
82:            }  
83:        while (col1==1&&col2==1&&col3==1&&col4==1);  
84:        delay(20);  
85:        while (1)  
86:          {  
87:            /* finding row location */  
88:           row1=0;row2=1;row3=1;row4=1;  
89:           if (col1!=1||col2!=1||col3!=1||col4!=1)  
90:            {  
91:             rowloc=0;  
92:             break;  
93:            }  
94:           row1=1;row2=0;row3=1;row4=1;  
95:           if (col1!=1||col2!=1||col3!=1||col4!=1)  
96:            {  
97:             rowloc=1;  
98:             break;  
99:            }  
100:           row1=1;row2=1;row3=0;row4=1;  
101:           if (col1!=1||col2!=2||col3!=1||col4!=1)  
102:            {  
103:             rowloc=2;  
104:             break;  
105:            }  
106:           row1=1;row2=1;row3=1;row4=0;  
107:           if (col1!=1||col2!=2||col3!=1||col4!=1)  
108:            {  
109:             rowloc=3;  
110:             break;  
111:            }  
112:          }  
113:               /* Finding column location */  
114:        if (col1==0&&col2==1&&col3==1&&col4==1)  
115:          colloc=0;  
116:        if (col1==1&&col2==0&&col3==1&&col4==1)  
117:          colloc=1;  
118:        if (col1==1&&col2==1&&col3==0&&col4==1)  
119:          colloc=2;  
120:        if (col1==1&&col2==1&&col3==1&&col4==0)  
121:          colloc=3;  
122:          /* Display data on lcd by sensing which key pressed */  
123:        if (colloc==0)  
124:          ldata (keypad[rowloc][0]);  
125:        if (colloc==1)  
126:          ldata (keypad[rowloc][1]);  
127:        if (colloc==2)  
128:          ldata (keypad[rowloc][2]);  
129:        if (colloc==3)  
130:          ldata (keypad[rowloc][3]);  
131:       }  
132:  }  

For enhance concept just visit www.salahtutorials.com
Hope you all like this post and I wish this must be useful for you.
Thanks :)

Comments

Translate to your language

Popular posts from this blog

Different Stages of I2C communication

Different Stages Of I2C

How to import Pygame in pycharm

hi, how are you today

Working demonstration of UART communication using 2 8051 uController on Proteus with Embedded C code.

Here we are going to use 2 at89c51 uControllers just to understand UART communication in 8051.