Toll-Free Customer Support 24/7

Calculator Code

#include"reg51.h"
#include"string.h"

#define KeyPort P1
#define LCD_PORT P2

sbit rs=P3^5;
sbit en=P3^7;
sbit D7=P2^7;
sbit rw=P3^6;

 


void busy();

void CMD_WRT(unsigned char);
//void LCD_WRT(unsigned char *);
void DATA_WRT(unsigned char);
void CONVERT_DISPLAY(unsigned int d);
void delay(unsigned int );

unsigned char check();
//unsigned char SCI_ReceiveByte( void );

unsigned char byte1,byt;

 

void DELAY();


int main()
{
unsigned char temp1,val,val1,frist[5],temp;

unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i;

// unsigned char temp;

for(i=0;i<5;i++)
{
TEMP1=CMD[i];
CMD_WRT(TEMP1);
}


TMOD=0X20;
TH1=0XFD;
SCON=0X50;
TR1=1;

DELAY();

 

while(1)

{

temp1=0;

frist[0]='0';
frist[1]='0';
frist[2]='0';
while((temp1!='+')&(temp1!='-')&(temp1!='*')&(temp1!='/'))

{
temp1=check();
DATA_WRT(temp1);

if((temp1!='+')&(temp1!='-')&(temp1!='*')&(temp1!='/'))
{
frist[2]=frist[1];
frist[1]=frist[0];
frist[0]=temp1;
}

}
temp=temp1;
val=(((frist[2]-48)*100)+((frist[1]-48)*10)+((frist[0]-48)));

val1=val;
temp1=0;

frist[0]='0';
frist[1]='0';
frist[2]='0';
while(temp1!='=')
{
temp1=check();
DATA_WRT(temp1);

if(temp1!='=')
{
frist[2]=frist[1];
frist[1]=frist[0];
frist[0]=temp1;
}
}
val=(((frist[2]-48)*100)+((frist[1]-48)*10)+((frist[0]-48)));

// val=val1+val;
// CONVERT_DISPLAY(val);

if(temp=='+')
{
CONVERT_DISPLAY(val1+val);
delay(200000);
}

else if(temp=='-')
{
CONVERT_DISPLAY(val1-val);
delay(200000);
}
else if(temp=='*')
{
CONVERT_DISPLAY(val1*val);
delay(200000);
}
else if(temp=='/')
{

CONVERT_DISPLAY(val1/val);
delay(200000);
}


}
}


void DELAY()
{
unsigned int X=800000;
while(X--);
}


void busy()
{
D7=1;
rs=0;
rw=1;
while(D7!=0)
{
en=0;
en=1;
}
}

 

void CMD_WRT(unsigned char val)
{
busy();
LCD_PORT=val;
rs=0;
rw=0;
en=1;
en=0;
}


void DATA_WRT(unsigned char ch)
{
busy();
LCD_PORT = ch;
rs=1;
rw=0;
en=1;
en=0;
}

unsigned char check()
{
unsigned char colloc=0,rowloc=0, colloc1,rowloc1;
unsigned char keypad[4][4]={'+','=','0','*',
'-','9','8','7',
'*','6','5','4',
'/','3','2','1'};

 

{

do
{
KeyPort = 0X0f;
do
{
colloc1 = KeyPort;
colloc1 &= 0x0F;
}while(colloc1 != 0x0F);

delay(20);

do
{
colloc1=KeyPort;
colloc1 &= 0x0F;
}while(colloc1==0x0F);


delay(20);

KeyPort = 0x0f;
colloc1=KeyPort;
colloc1 &= 0x0F;
if(colloc1==0x0E)
{
colloc=0;
}
else if(colloc1==0x0D)
{
colloc=1;
}
else if(colloc1==0x0B)
{
colloc=2;
}
else if(colloc1==0x07)
{
colloc=3;
}
KeyPort = 0xf0;
rowloc1 = KeyPort;
rowloc1 &= 0xf0;
if(rowloc1 == 0xE0)
{
rowloc=0;
}

else if(rowloc1==0xD0)
{
rowloc=1;
}
else if(rowloc1==0xB0)
{
rowloc=2;
}
else if(rowloc1==0x70)
{
rowloc=3;
}
// CMD_WRT(0XC0);
// DATA_WRT(keypad[rowloc][colloc]);

return(keypad[rowloc][colloc]);

}while(1);
}
}

void delay(unsigned int time)
{
unsigned int i;
for(i=0;i<time;i++);
for(i=0;i<10000;i++);
}

void CONVERT_DISPLAY(unsigned int d)
{

unsigned char dig1,dig2,dig3,dig[3];
unsigned char x;
unsigned char temp;
temp=d;
temp=temp/10;
dig1=d%10;
dig2=temp%10;
dig3=temp/10;


dig[0]=dig3;
dig[1]=dig2;
dig[2]=dig1;


for(x=0;x<3;x++)
{
temp=dig[x]|0x30;

DATA_WRT(temp);
}

}

Back to top