I am trying to write program for simple calculator. actually I wanted to understand interfacing of keypad with micro controller. so I decided to make small calculator I have keypad, 8051 and LCD. Previously I have been written C program for LCD Display. I am having problem to write code for keypad. This is hardware design

I have been gone throw some tutorials and visited on some web sites but I am having difficulty to write program. can any one tell me what I have to write in program to make calculator.
Here is my small effort
Program code:

I have been gone throw some tutorials and visited on some web sites but I am having difficulty to write program. can any one tell me what I have to write in program to make calculator.
Here is my small effort
Program code:
C:
#include<reg51.h>
/*LCD Module Connections */
#define port P1
sbit RS = P0^0;
sbit RW = P0^1;
sbit EN = P0^2;
/* Keypad Connections */
sbit R1 = P1^0;
sbit R2 = P1^1;
sbit R3 = P1^2;
sbit R4 = P1^3;
sbit C1 = P1^4;
sbit C2 = P1^5;
sbit C3 = P1^6;
sbit C4 = P1^7;
/* function for delay */
void Delay(unsigned int time)
{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
/* Function to send command to LCD */
void LCD_Command(unsigned char cmd)
{
port = cmd;
RS=0;
RW=0;
EN=1;
Delay(2);
EN=0;
}
/*Function to send message on LCD */
void LCD_Data(unsigned char Data)
{
port = Data;
RS=1;
RW=0;
EN=1;
Delay(2);
EN=0;
}
/* Function to prepare the LCD */
void LCD_init()
{
LCD_Command(0x38);
Delay(20);
LCD_Command(0x0f);
Delay(20);
LCD_Command(0x01);
Delay(20);
LCD_Command(0x81);
Delay(20);
}
char Read_Keypad()
{
C1=1;
C2=1;
C3=1;
C4=1;
R1=0;
R2=1;
R3=1;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '7';}
if(C2==0){Delay(100);while(C2==0);return '8';}
if(C3==0){Delay(100);while(C3==0);return '9';}
if(C4==0){Delay(100);while(C4==0);return '/';}
R1=1;
R2=0;
R3=1;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '4';}
if(C2==0){Delay(100);while(C2==0);return '5';}
if(C3==0){Delay(100);while(C3==0);return '6';}
if(C4==0){Delay(100);while(C4==0);return 'X';}
R1=1;
R2=1;
R3=0;
R4=1;
if(C1==0){Delay(100);while(C1==0);return '1';}
if(C2==0){Delay(100);while(C2==0);return '2';}
if(C3==0){Delay(100);while(C3==0);return '3';}
if(C4==0){Delay(100);while(C4==0);return '-';}
R1=1;
R2=1;
R3=1;
R4=0;
if(C1==0){Delay(100);while(C1==0);return 'C';}
if(C2==0){Delay(100);while(C2==0);return '0';}
if(C3==0){Delay(100);while(C3==0);return '=';}
if(C4==0){Delay(100);while(C4==0);return '+';}
return 0;
}
void main()
{
while(1)
{
}
}
Last edited by a moderator: