You are on page 1of 10

#include <reg51.

h>
sfr lcd_data_pin=0xA0;
sbit lcd_rs=P3^4; //Register select pin
sbit lcd_rw=P3^3; // Read write pin
sbit lcd_en=P3^2; //Enable pin
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;
unsigned char data_in;
unsigned char operation;
char step;
int operand1;
int operand2;
int ans;
void delay(unsigned int var1) //function for delay
{
int x,y;
for(x=0;x<var1;x++)
for(y=0;y<1275;y++);
}
void long_delay() //functi
on for a long delay
{
unsigned int i, j;
for (i = 0; i < 255; i++) {
for (j = 0; j < 255; j++);
}
}
void command(unsigned char var2) //function to send comma
nds to the LCD
{
lcd_data_pin=var2;
lcd_en=1;
lcd_rs=0;
lcd_rw=0;
delay(1);
lcd_en=0;
}
void data_out(unsigned char var3) //function to send data
to the LCD
{
lcd_data_pin=var3;
lcd_en=1;
lcd_rs=1;
lcd_rw=0;
delay(1);
lcd_en=0;
}
void disp_string(unsigned char *var4) //function to write string on LC
D
{
int x;
for(x=0;var4[x]!=0;x++)
{
data_out(var4[x]);
}
}
void disp_char (unsigned char var) //function to display nu
mbers on LCD; sends the ASCII values of characters
{
if (var == 0)
disp_string ("0");
if (var == 1)
disp_string ("1");
if (var == 2)
disp_string ("2");
if (var == 3)
disp_string ("3");
if (var == 4)
disp_string ("4");
if (var == 5)
disp_string ("5");
if (var == 6)
disp_string ("6");
if (var == 7)
disp_string ("7");
if (var == 8)
disp_string ("8");
if (var == 9)
disp_string ("9");
}
void disp_number (int number) //function to display in
tegers on LCD
{
//the function breaks the integers into single digits, so that their ASCII value
s can be sent to LCD for display
unsigned char a[5];
char x;
for (x=0;x<5;x++)
{
a[x]=number%10;
number=number/10;
}
for (x=4;x>=0;x--)
{
disp_char(a[x]);
}
}
void initialize() //functi
on to initialize LCD
{
command(0x38); //set mode 5x8, two lines
delay(2);
command(0x0F); //display on cursor blinking
delay(2);
command(0x82); //Set cursor to blink at line 1 positon 0
delay(2);
command(0x0C); //display on cursor off
delay(2);
disp_string("Ready...");
}
void read_port (void) //function to re
ad keypad
{
C1=1;C2=1;C3=1;C4=1;
R4=1; R3=1; R2=1; R1=0; //test row 1

if (C1 == 0) // key 1 is presed


{ delay(1); //debounce
while (C1==0); //wait until release the
key
data_in = 1;
}
if (C2 == 0) //key 2 is pressed
{ delay(1); //debounce
while (C2==0); //wait until release the
key
data_in = 2;
}
if (C3 == 0) //key 3 is pressed
{ delay(1); //debounce
while (C3==0); //wait until release the
key
data_in = 3;
}
if (C4 == 0) //key 4 is pressed
{ delay(1); //debounce
while (C4==0); //wait until release the
key
data_in = 10;
}

R4=1; R3=1; R1=1; R2=0; //test row 2

if (C1 == 0) // key 5 is presed


{ delay(1); //debounce
while (C1==0); //wait until release the
key
data_in = 4;
}
if (C2 == 0) //key 6 is pressed
{ delay(1); //debounce
while (C2==0); //wait until release the
key
data_in = 5;
}
if (C3 == 0) //key 7 is pressed
{ delay(1); //debounce
while (C3==0); //wait until release the
key
data_in = 6;
}
if (C4 == 0) //key 8 is pressed
{ delay(1); //debounce
while (C4==0); //wait until release the
key
data_in = 11;
}

R4=1; R1=1; R2=1; R3=0; //test row 3

if (C1 == 0) // key 9 is presed


{ delay(1); //debounce
while (C1==0); //wait until release the
key
data_in = 7;
}
if (C2 == 0) //key 10 is pressed
{ delay(1); //debounce
while (C2==0); //wait until release the
key
data_in = 8;
}
if (C3 == 0) //key 11 is pressed
{ delay(1); //debounce
while (C3==0); //wait until release the
key
data_in = 9;
}
if (C4 == 0) //key 12 is pressed
{ delay(1); //debounce
while (C4==0); //wait until release the
key
data_in = 12;
}

R1=1; R2=1; R3=1; R4=0; //test row 4

if (C1 == 0) // key 13 is presed


{ delay(1); //debounce
while (C1==0); //wait until release the
key
data_in = 14;
}
if (C2 == 0) //key 14 is pressed
{ delay(1); //debounce
while (C2==0); //wait until release the
key
data_in = 16;
}
if (C3 == 0) //key 15 is pressed
{ delay(1); //debounce
while (C3==0); //wait until release the
key
data_in = 15;
}
if (C4 == 0) //key 16 is pressed
{ delay(1); //debounce
while (C4==0); //wait until release the
key
data_in = 13;
}

}
void main(void)
{
initialize();
operand1=0; operand2=0; operation=0; step=0;
long_delay();
command (0x01);
while(1)
{
read_port();
if (data_in ==1)
{
if (step < 5)
{
operand1= (10 * operand1) + 1;
disp_char (1);
}
if (step > 5)
{
operand2= (10 * operand2) + 1;
disp_char (1);
}

}
if (data_in == 2)
{
if (step < 5)
{
operand1= (10 * operand1) + 2;
disp_char (2);
}
if (step > 5)
{
operand2= (10 * operand2) + 2;
disp_char (2);
}

}
if (data_in == 3)
{
if (step < 5)
{
operand1= (10 * operand2) + 3;
disp_char (3);
}
if (step > 5)
{
operand2= (10 * operand2) + 3;
disp_char (3);
}

}
if (data_in ==4)
{
if (step < 5)
{
operand1= (10 * operand1) + 4;
disp_char (4);
}
if (step > 5)
{
operand2= (10 * operand2) + 4;
disp_char (4);
}

}
if (data_in ==5)
{
if (step < 5)
{
operand1= (10 * operand1) + 5;
disp_char (5);
}
if (step > 5)
{
operand2= (10 * operand2) + 5;
disp_char (5);
}

}
if (data_in ==6)
{
if (step < 5)
{
operand1= (10 * operand1) + 6;
disp_char (6);
}
if (step > 5)
{
operand1= (10 * operand2) + 6;
disp_char (6);
}

}
if (data_in ==7)
{
if (step < 5)
{
operand1= (10 * operand1) + 7;
disp_char (7);
}
if (step > 5)
{
operand2= (10 * operand2) + 7;
disp_char (7);
}
}
if (data_in ==8)
{
if (step < 5)
{
operand1= (10 * operand1) + 8;
disp_char (8);
}
if (step > 5)
{
operand2= (10 * operand2) + 8;
disp_char (8);
}

}
if (data_in ==9)
{
if (step < 5)
{
operand1= (10 * operand1) + 9;
disp_char (9);
}
if (step > 5)
{
operand2= (10 * operand2) + 9;
disp_char (9);
}

}
if (data_in ==16)
{
if (step < 5)
{
operand1= (10 * operand1) + 0;
disp_char (0);
}
if (step > 5)
{
operand2= (10 * operand2) + 0;
disp_char (0);
}

}
if (data_in ==10) //A (ADD)
{
command (0x01);
command (0xC2);
disp_string ("+ ");
disp_number (operand1);
operation = 1;
step=6;
command (0x82);
}
if (data_in ==11) //B (SUB)
{
command (0x01);
command (0xC2);
disp_string ("- ");
disp_number (operand1);
operation = 2;
step=6;
command (0x82);
}
if (data_in ==12) //C (MUL)
{
command (0x01);
command (0xC2);
disp_string ("x ");
disp_number (operand1);
operation = 3;
step=6;
command (0x82);
}
if (data_in ==13) //D (DIV)
{
command (0x01);
command (0xC2);
disp_string ("/ ");
disp_number (operand1);
operation = 4;
step=6;
command (0x82);
}
if (data_in ==15) //# (=)
{
command (0x01);
command (0xC2);
disp_string ("= ");
step=12;
command (0x01);
command (0x82);
}
if (data_in ==14) //* (RESET)
{
operation=0;
operand1=0;
operand2=0;
step=0;
ans=0;
command (0x01);
command (0x80); //Force cursor to beginning of 1
st line
disp_string (" RESET ");
long_delay();
command (0x01);
command (0x82);

if (step>=10)
{
if (operation == 1) ans = operand1 + operand2;
if (operation == 2) ans = operand1 - operand2;
if (operation == 3) ans = operand1 * operand2;
if (operation == 4) ans = operand1 / operand2;
command (0x82);
disp_string (" Result ");
command (0xC2);
disp_number (ans);
operand1=ans;
ans=0;
step=6;
}

data_in=0;

}
}

You might also like