/* calculator Project for 8051---
Possible Operation- add, sub, mul, div
limitations- decimal part is truncated
result upto only 6decimals */
/* algorithm::
step1:store the entered values in an array
if any operator key is pressed goto step2
step2:store the var1 here (using convertArrayToDecimal() function)
and also store the operator pressed
if '=' operator is pressed goto step3
step3:do the respective operation and display the result */
#include<reg51.h>
#include"lcd_display.h"
int pow(int, int)
void convertArrayToDecimal(int*)
void clear_array()
void column1()
void column2()
void column3()
void column4()
char getkey()
sbit c1=P2^0
sbit c2=P2^1
sbit c3=P2^2
sbit c4=P2^3
sbit r1=P2^4
sbit r2=P2^5
sbit r3=P2^6
sbit r4=P2^7
int var1=0,var2=0,dummy_var,row,col
char values[4][4]={{55,56,57,'/'},
{52,53,54,'*'},
{49,50,51,'-'},
{' ',48,'=','+'}}
int num[5]
int i=0
void main()
{
char key
unsigned char operation
int var_count=0
int result
r1=r2=r3=r4=rs=en=0
c1=c2=c3=c4=1
P3=0x00
TMOD=0x01
while(1)
{
while(c1==1 && c2==1 && c3==1 && c4==1)
key=getkey()
if(key==' ')
{
lcd_init()
var1=0
var2=0
clear_array()
}
if(key!='/' && key!='+' && key!='-' && key!='*' && key!=' ' && key!='=')
{
num[i]=key-48
lcd_data(key)
i=i+1
}
if(key=='/' || key=='+' || key=='-' || key=='*' )
{
dummy_var=0
convertArrayToDecimal(num)
var1=dummy_var
operation=key
lcd_data(key)
}
if(key=='=')
{
dummy_var=0
convertArrayToDecimal(num)
var2=dummy_var
lcd_cmd(0xc0)
lcd_dataS("result is-")
switch(operation)
{
case '+': result=var1+var2
lcd_dataint(result)
break
case '-': result=var1-var2
lcd_dataint(result)
break
case '*': result=var1*var2
lcd_dataint(result)
break
case '/': result=var1/var2
lcd_dataint(result)
break
}
}
} //end of while
}
char getkey()
{
if(c1==0)
{
column1()
while(!c1)
row=0
}
if(c2==0)
{
column2()
while(!c2)
row=1
}
if(c3==0)
{
column3()
while(!c3)
row=2
}
if(c4==0)
{
column4()
while(!c4)
row=3
}
return values[col][row]
}
void convertArrayToDecimal(int *p)
{
int k=i-1
while(i>0)
{
dummy_var=dummy_var+((*p) * pow(10,k))
k--
i--
p++
}
}
int pow(int x, int z)
{
int value=1
while(z>0)
{
value=value*x
z=z-1
}
return value
}
void clear_array()
{
int j
for(j=0
num[j]='\0'
}
void column1()
{
r1=r2=r3=r4=1
r1=0
if(c1==0)
col=0
r1=1
if(c1==0)
col=1
r1=1
if(c1==0)
col=2
r1=1
if(c1==0)
col=3
r1=r2=r3=r4=0
}
void column2()
{
r1=r2=r3=r4=1
r1=0
if(c2==0)
col=0
r1=1
if(c2==0)
col=1
r1=1
if(c2==0)
col=2
r1=1
if(c2==0)
col=3
r1=r2=r3=r4=0
}
void column3()
{
r1=r2=r3=r4=1
r1=0
if(c3==0)
col=0
r1=1
if(c3==0)
col=1
r1=1
if(c3==0)
col=2
r1=1
if(c3==0)
col=3
r1=r2=r3=r4=0
}
void column4()
{
r1=r2=r3=r4=1
r1=0
if(c4==0)
col=0
r1=1
if(c4==0)
col=1
r1=1
if(c4==0)
col=2
r1=1
if(c4==0)
col=3
r1=r2=r3=r4=0
}
No comments:
Post a Comment