做了个计算器,望指点指点
时间:10-02
整理:3721RD
点击:
/**********
单片机AT89s52 1602液晶 矩阵键盘16个占用P1^0-P1^7
键盘符号
7 8 9 +
4 5 6 -
1 2 3 *
0 = / %
功能:
1:能实现十位数内运算及2147483647~2147483648
2:不能进行负数运算
3:可以进行用上次运算结果进行计算
4:运算结果可以是负数
5:等号再不做输出运算结果用时做清除键
6:不能进行小数运算
**********/
#include <reg52.h>
#include<intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={"0123456789=%/*-+"};
uchar code kc[]={0xe7,0xeb,0xdb,0xbb,0xed,0xdd,0xbd,0xee,0xde,0xbe,0xd7,0xb7,0x77,0x7b,0x7d,0x7e};
uchar code resul[]={"OUT"};
uchar code chushi[]={"INT"};
long result;
#define p P1
sbit RS=P2^0;
sbit RW=P2^1;
sbit LCDE=P2^2;
/**********延时函数**********/
delayms (uint a)
{
uchar i;
while(a--)
{
for(i=0;i++;i<114)
{;}
}
}
/**********1602液晶显示函数**********/
uchar busy ()
{
uchar bus;
RS=0;
RW=1;
LCDE=0;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
_nop_();
_nop_();
bus=P0;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
return bus;
}
write(uchar date)
{
while(busy()&0x80);
RW=0;
RS=1;
LCDE=0;
P0=date;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
delayms(2);
}
writerom(uchar date)
{
while(busy()&0x80);
RW=0;
RS=0;
LCDE=0;
P0=date;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
delayms(2);
}
writedate(uchar adr,uchar date )
{
writerom(adr|0x80);
write(date);
}
/*uchar read()
{
while(busy()==1);
uchar date;
RW=1;
RS=1;
LCDE=0;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
date=P0;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
return date;
}*/
clear()
{
while(busy()==1);
writerom(0x01);
}
/*uchar readdate (uchar adr)
{
writerom(adr);
return(read());
} */
lcdintl()
{
writerom(0x06);
writerom(0x0f) ;
writerom(0x38) ;
clear();
delayms(10);
}
/**********矩阵键盘扫描 获得按键值**********/
uchar getkey()
{
uchar k,i;
uchar temp1,temp2;
p=0xf0;
if(p!=0xf0)
{
delayms(20);
if(p!=0xf0)
{
temp1=p;
p=0x0f;
temp2=p ;
while(p!=0x0f);
temp1=temp1|temp2;
}
}
for(i=0;i<16;i++)
{
if(temp1==kc)
{
k=i;break;
}
}
return k;
}
/**********result显示**********/
void resultscan (long date1,long date2,uchar date3)
{
uchar i,f;
switch(date3)
{
case 11: result=date1%date2;break;
case 12: result=date1/date2;break;
case 13: result=date1*date2;break;
case 14: result=date1-date2;break;
case 15: result=date1+date2;break;
default:result=0;
}
writerom(0x40|0x80);
i=0;
while(resul!='\0')
{ write(resul);i++; }
write(':');
if(OV==0)
{
if(result<0)
{
write(tab[14]);
result=-result;
f=1;
}
}
else
{ i=11;}
if(result>=0) i=1;
if(result>=10) i=2;
if(result>=100) i=3;
if(result>=1000) i=4;
if(result>=10000) i=5;
if(result>=100000) i=6;
if(result>=1000000) i=7;
if(result>=10000000) i=8;
if(result>=100000000) i=9;
if(result>=1000000000) i=10;
switch(i)
{
case 10:write(tab[result/1000000000]);
case 9:write(tab[result/1000000000%10]);
case 8:write(tab[result/10000000%10]);
case 7:write(tab[result/1000000%10]);
case 6:write(tab[result/100000%10]);
case 5:write(tab[result/10000%10]);
case 4:write(tab[result/1000%10]);
case 3:write(tab[result/100%10]);
case 2:write(tab[result/10%10]);
case 1:write(tab[result/1%10]); break;
case 11: write('E'); write('R'); write('R');
}
if(f==1)
{
result=-result;
f=0;
}
}
/**********输入处理、运算函数**********/
void caculate()
{
uchar w=1,k,start,x,y,fh,i;
long num1,num2;
while(1)
{
p=0xf0;
if(p!=0xf0)
{
k=getkey();
if((k==10)&&(y==0))
{
start=0;
w=1;
num1=0;
x=0;
}
if(start==0)
{
lcdintl();
clear();
delayms(100);
writerom(0x00|0x80);
i=0;
while(chushi!='\0')
{ write(chushi);i++; }
start=1;
write(':');
}
if(w==1)
{
if(k<10)
{
x=1;
num1=num1*10+k;
write(tab[k]);
}
if(k>10)
{
if(x==1)
{
w=2;
fh=k;
write(tab[k]);
}
if(x==0)
{
w=2;
fh=k;
num1=result;
i=0;
while(resul!='\0')
{ write(resul);i++; }
write(tab[k]);
}
}
}
else
{
if(k<10)
{
y=1;
num2=num2*10+k;
if((busy()&0x7f)==0x10){ writerom(0x07);}
write(tab[k]);
}
if((k==10)&&(y==1))
{
w=1;
x=y=0;
start=0;
resultscan(num1,num2,fh);
num1=num2=0;
writerom(0x0c);
writerom(0x03);
}
}
}
}
}
/**********主函数**********/
void main()
{
uchar i;
lcdintl();
clear();
delayms(100);
writerom(0x00|0x80);
i=0;
while(chushi != '\0')
{ write(chushi);i++; }
write(':');
caculate() ;
}
单片机AT89s52 1602液晶 矩阵键盘16个占用P1^0-P1^7
键盘符号
7 8 9 +
4 5 6 -
1 2 3 *
0 = / %
功能:
1:能实现十位数内运算及2147483647~2147483648
2:不能进行负数运算
3:可以进行用上次运算结果进行计算
4:运算结果可以是负数
5:等号再不做输出运算结果用时做清除键
6:不能进行小数运算
**********/
#include <reg52.h>
#include<intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={"0123456789=%/*-+"};
uchar code kc[]={0xe7,0xeb,0xdb,0xbb,0xed,0xdd,0xbd,0xee,0xde,0xbe,0xd7,0xb7,0x77,0x7b,0x7d,0x7e};
uchar code resul[]={"OUT"};
uchar code chushi[]={"INT"};
long result;
#define p P1
sbit RS=P2^0;
sbit RW=P2^1;
sbit LCDE=P2^2;
/**********延时函数**********/
delayms (uint a)
{
uchar i;
while(a--)
{
for(i=0;i++;i<114)
{;}
}
}
/**********1602液晶显示函数**********/
uchar busy ()
{
uchar bus;
RS=0;
RW=1;
LCDE=0;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
_nop_();
_nop_();
bus=P0;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
return bus;
}
write(uchar date)
{
while(busy()&0x80);
RW=0;
RS=1;
LCDE=0;
P0=date;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
delayms(2);
}
writerom(uchar date)
{
while(busy()&0x80);
RW=0;
RS=0;
LCDE=0;
P0=date;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
delayms(2);
}
writedate(uchar adr,uchar date )
{
writerom(adr|0x80);
write(date);
}
/*uchar read()
{
while(busy()==1);
uchar date;
RW=1;
RS=1;
LCDE=0;
_nop_();
_nop_();
LCDE=1;
_nop_();
_nop_();
date=P0;
_nop_();
_nop_();
_nop_();
_nop_();
LCDE=0;
return date;
}*/
clear()
{
while(busy()==1);
writerom(0x01);
}
/*uchar readdate (uchar adr)
{
writerom(adr);
return(read());
} */
lcdintl()
{
writerom(0x06);
writerom(0x0f) ;
writerom(0x38) ;
clear();
delayms(10);
}
/**********矩阵键盘扫描 获得按键值**********/
uchar getkey()
{
uchar k,i;
uchar temp1,temp2;
p=0xf0;
if(p!=0xf0)
{
delayms(20);
if(p!=0xf0)
{
temp1=p;
p=0x0f;
temp2=p ;
while(p!=0x0f);
temp1=temp1|temp2;
}
}
for(i=0;i<16;i++)
{
if(temp1==kc)
{
k=i;break;
}
}
return k;
}
/**********result显示**********/
void resultscan (long date1,long date2,uchar date3)
{
uchar i,f;
switch(date3)
{
case 11: result=date1%date2;break;
case 12: result=date1/date2;break;
case 13: result=date1*date2;break;
case 14: result=date1-date2;break;
case 15: result=date1+date2;break;
default:result=0;
}
writerom(0x40|0x80);
i=0;
while(resul!='\0')
{ write(resul);i++; }
write(':');
if(OV==0)
{
if(result<0)
{
write(tab[14]);
result=-result;
f=1;
}
}
else
{ i=11;}
if(result>=0) i=1;
if(result>=10) i=2;
if(result>=100) i=3;
if(result>=1000) i=4;
if(result>=10000) i=5;
if(result>=100000) i=6;
if(result>=1000000) i=7;
if(result>=10000000) i=8;
if(result>=100000000) i=9;
if(result>=1000000000) i=10;
switch(i)
{
case 10:write(tab[result/1000000000]);
case 9:write(tab[result/1000000000%10]);
case 8:write(tab[result/10000000%10]);
case 7:write(tab[result/1000000%10]);
case 6:write(tab[result/100000%10]);
case 5:write(tab[result/10000%10]);
case 4:write(tab[result/1000%10]);
case 3:write(tab[result/100%10]);
case 2:write(tab[result/10%10]);
case 1:write(tab[result/1%10]); break;
case 11: write('E'); write('R'); write('R');
}
if(f==1)
{
result=-result;
f=0;
}
}
/**********输入处理、运算函数**********/
void caculate()
{
uchar w=1,k,start,x,y,fh,i;
long num1,num2;
while(1)
{
p=0xf0;
if(p!=0xf0)
{
k=getkey();
if((k==10)&&(y==0))
{
start=0;
w=1;
num1=0;
x=0;
}
if(start==0)
{
lcdintl();
clear();
delayms(100);
writerom(0x00|0x80);
i=0;
while(chushi!='\0')
{ write(chushi);i++; }
start=1;
write(':');
}
if(w==1)
{
if(k<10)
{
x=1;
num1=num1*10+k;
write(tab[k]);
}
if(k>10)
{
if(x==1)
{
w=2;
fh=k;
write(tab[k]);
}
if(x==0)
{
w=2;
fh=k;
num1=result;
i=0;
while(resul!='\0')
{ write(resul);i++; }
write(tab[k]);
}
}
}
else
{
if(k<10)
{
y=1;
num2=num2*10+k;
if((busy()&0x7f)==0x10){ writerom(0x07);}
write(tab[k]);
}
if((k==10)&&(y==1))
{
w=1;
x=y=0;
start=0;
resultscan(num1,num2,fh);
num1=num2=0;
writerom(0x0c);
writerom(0x03);
}
}
}
}
}
/**********主函数**********/
void main()
{
uchar i;
lcdintl();
clear();
delayms(100);
writerom(0x00|0x80);
i=0;
while(chushi != '\0')
{ write(chushi);i++; }
write(':');
caculate() ;
}
自己给自己顶一个,哈哈。
帮你顶一下
能把原理图晒出来不?