求基于AT89C51单片机简易计算器设计的程序,请帮帮忙,谢谢。
#include<reg52.h>
delay10ms()
{
unsigned char a,b;
for(a=100;a>0;a--)
for(b=50;b>0;b--);
}
delay1ms()
{
unsigned char a,b;
for(a=10;a>0;a--)
for(b=50;b>0;b--);
}
unsigned char code tab[]=
{
0x3f,6,0x5b,0x4f,0x66,0x6d,0x7d,7,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
main()
{
unsigned char m,i,j,k,l,p,cc2,cc3,cc4,cc5,o;
unsigned long c1,c2,cc;
unsigned int cc1;
unsigned char aa0,aa1,aa2,aa3,bb0,bb1,bb2,bb3;
unsigned char a0,a1,a2,a3,b0,b1,b2,b3;
bit q,n;
while(1)
{static dian=0x80,fuhao=0;
for (j=0;j<=3;j++)
{
k=0xfe<<j;
l=0xfe>>(8-j);
P0=k|l;
if(P0_4==0)
{
delay10ms();
if(P0_4==0)
{
while(P0_4==0)
{
P2=b0;
P1=0xfe;
delay1ms();
P2=0;
P2=b1;
P1=0xfd;
delay1ms();
P2=0;
P2=b2;
P1=0xfb;
delay1ms();
P2=0;
P2=b3;
P1=0xf7;
delay1ms();
P2=0;
P2=a0;
P1=0xef;
delay1ms();
P2=0;
P2=a1;
P1=0xdf;
delay1ms();
P2=0;
P2=a2;
P1=0xbf;
delay1ms();
P2=0;
P2=a3;
P1=0x7f;
delay1ms();
P2=0;
P2=dian;
P1=0xef;
delay1ms();
P2=0;
P2=fuhao;
P1=0xef;
delay1ms();
P2=0;
}
if(tab[4*j]==0x39)
{
n=1;
i=3;
}
else
{m++;
if(m<=4&n==0)
{
a3=a2;
a2=a1;
a1=a0;
a0=tab[4*j];
aa3=aa2;
aa2=aa1;
aa1=aa0;
aa0=4*j;
}
else
{
if(p<=3&n==1)
{
b3=b2;
b2=b1;
b1=b0;
b0=tab[4*j];
bb3=bb2;
bb2=bb1;
bb1=bb0;
bb0=4*j;
p++;
}
}
}
}
}
if(P0_5==0)
{
delay10ms();
if(P0_5==0)
{
while(P0_5==0)
{
P2=b0;
P1=0xfe;
delay1ms();
P2=0;
P2=b1;
P1=0xfd;
delay1ms();
P2=0;
P2=b2;
P1=0xfb;
delay1ms();
P2=0;
P2=b3;
P1=0xf7;
delay1ms();
P2=0;
P2=a0;
P1=0xef;
delay1ms();
P2=0;
P2=a1;
P1=0xdf;
delay1ms();
P2=0;
P2=a2;
P1=0xbf;
delay1ms();
P2=0;
P2=a3;
P1=0x7f;
delay1ms();
P2=0;
P2=dian;
P1=0xef;
delay1ms();
P2=0;
P2=fuhao;
P1=0xef;
delay1ms();
P2=0;
}
if(tab[4*j+1]==0x5e)
{
i=4;
n=1;
}
else
{m++;
if(m<=4&n==0)
{
a3=a2;
a2=a1;
a1=a0;
a0=tab[4*j+1];
aa3=aa2;
aa2=aa1;
aa1=aa0;
aa0=4*j+1;
}
else
{
if(p<=3&n==1)
{
b3=b2;
b2=b1;
b1=b0;
b0=tab[4*j+1];
bb3=bb2;
bb2=bb1;
bb1=bb0;
bb0=4*j+1;
p++;
}
}
}
}
}
if(P0_6==0)
{
delay10ms();
if(P0_6==0)
{
while(P0_6==0)
{
P2=b0;
P1=0xfe;
delay1ms();
P2=0;
P2=b1;
P1=0xfd;
delay1ms();
P2=0;
P2=b2;
P1=0xfb;
delay1ms();
P2=0;
P2=b3;
P1=0xf7;
delay1ms();
P2=0;
P2=a0;
P1=0xef;
delay1ms();
P2=0;
P2=a1;
P1=0xdf;
delay1ms();
P2=0;
P2=a2;
P1=0xbf;
delay1ms();
P2=0;
P2=a3;
P1=0x7f;
delay1ms();
P2=0;
P2=dian;
P1=0xef;
delay1ms();
P2=0;
P2=fuhao;
P1=0xef;
delay1ms();
P2=0;
}
if(tab[4*j+2]==0x77)
{
i=1;
n=1;
}
else
{
if(tab[4*j+2]==0x79)
{
q=1;
n=1;
}
else
{
m++;
if(m<=4&n==0)
{
a3=a2;
a2=a1;
a1=a0;
a0=tab[4*j+2];
aa3=aa2;
aa2=aa1;
aa1=aa0;
aa0=4*j+2;
}
else
{
if(p<=3&n==1)
{
b3=b2;
b2=b1;
b1=b0;
b0=tab[4*j+2];
bb3=bb2;
bb2=bb1;
bb1=bb0;
bb0=4*j+2;
p++;
}
}
}
}
}
}
if(P0_7==0)
{
delay10ms();
if(P0_7==0)
{
while(P0_7==0)
{
P2=b0;
P1=0xfe;
delay1ms();
P2=0;
P2=b1;
P1=0xfd;
delay1ms();
P2=0;
P2=b2;
P1=0xfb;
delay1ms();
P2=0;
P2=b3;
P1=0xf7;
delay1ms();
P2=0;
P2=a0;
P1=0xef;
delay1ms();
P2=0;
P2=a1;
P1=0xdf;
delay1ms();
P2=0;
P2=a2;
P1=0xbf;
delay1ms();
P2=0;
P2=a3;
P1=0x7f;
delay1ms();
P2=0;
P2=dian;
P1=0xef;
delay1ms();
P2=0;
P2=fuhao;
P1=0xef;
delay1ms();
P2=0;
}
if(tab[4*j+3]==0x71)
{
i=6;
n=1;
}
else
{
if(tab[4*j+3]==0x7c)
{
i=2;
n=1;
}
else
{m++;
if(m<=4&n==0)
{
a3=a2;
a2=a1;
a1=a0;
a0=tab[4*j+3];
aa3=aa2;
aa2=aa1;
aa1=aa0;
aa0=4*j+3;
}
else
{
if(p<=3&n==1)
{
b3=b2;
b2=b1;
b1=b0;
b0=tab[4*j+3];
bb3=bb2;
bb2=bb1;
bb1=bb0;
bb0=4*j+3;
p++;
}
}
}
}
}
}
if(i==6)//复位
{
a0=a1=a2=a3=b0=b1=b2=b3=0;
dian=0x80;
fuhao=0;
m=i=j=k=l=n=p=q=0;
aa0=aa1=aa2=aa3=bb0=bb1=bb2=bb3=0;
c1=c2=cc=cc1=cc2=cc3=cc4=cc5=0;
}
if(q==0)
{
P2=b0;
P1=0xfe;
delay1ms();
P2=0;
P2=b1;
P1=0xfd;
delay1ms();
P2=0;
P2=b2;
P1=0xfb;
delay1ms();
P2=0;
P2=b3;
P1=0xf7;
delay1ms();
P2=0;
P2=a0;
P1=0xef;
delay1ms();
P2=0;
P2=a1;
P1=0xdf;
delay1ms();
P2=0;
P2=a2;
P1=0xbf;
delay1ms();
P2=0;
P2=a3;
P1=0x7f;
delay1ms();
P2=0;
P2=dian;
P1=0xef;
delay1ms();
P2=0;
P2=fuhao;
P1=0xef;
delay1ms();
P2=0;
}
else
{
q=0;
c1=aa0+aa1*10+aa2*100+aa3*1000;
c2=bb0+bb1*10+bb2*100+bb3*1000;
if(i==1)//加法运算
{
cc=c1+c2;
}
if(i==2)//减法运算
{
if(c1>=c2)
{
cc=c1-c2;
}
else
{
cc=c2-c1;
fuhao=0x40;
}
}
if(i==3)//乘法运算
{
cc=c1*c2;
}
a3=tab[cc/10000000];
aa3=cc/10000000;
a2=tab[(cc%10000000)/1000000];
aa2=(cc%10000000)/1000000;
a1=tab[(cc%1000000)/100000];
aa1=(cc%1000000)/100000;
a0=tab[(cc%100000)/10000];
aa0=(cc%100000)/10000;
b3=tab[(cc%10000)/1000];
bb3=(cc%10000)/1000;
b2=tab[(cc%1000)/100];
bb2=(cc%1000)/100;
b1=tab[(cc%100)/10];
bb1=(cc%100)/10;
b0=tab[cc%10];
bb0=cc%10;
dian=0;//消除点
if(aa3==0)//消除多余的零
{
a3=0;
if(aa2==0)
{
a2=0;
if(aa1==0)
{
a1=0;
if(aa0==0)
{
a0=0;
if(bb3==0)
{
b3=0;
if(bb2==0)
{
b2=0;
if(bb1==0)
{
b1=0;
}
}
}
}
}
}
}
if(i==4)//除法运算
{
if(c2==0)
{
for(o=100;o>0;o--)
{
b2=b1=b0=0;
a3=0x79;
a2=a1=b3=0x77;
a0=0x3f;
P2=b0;
P1=0xfe;
delay1ms();
P2=0;
P2=b1;
P1=0xfd;
delay1ms();
P2=0;
P2=b2;
P1=0xfb;
delay1ms();
P2=0;
P2=b3;
P1=0xf7;
delay1ms();
P2=0;
P2=a0;
P1=0xef;
delay1ms();
P2=0;
P2=a1;
P1=0xdf;
delay1ms();
P2=0;
P2=a2;
P1=0xbf;
delay1ms();
P2=0;
P2=a3;
P1=0x7f;
delay1ms();
P2=0;
delay10ms();
}
i=6;
}
else
{
cc1=c1/c2;
a3=tab[cc1/1000];
aa3=cc1/1000;
a2=tab[(cc1%1000)/100];
aa2=(cc1%1000)/100;
a1=tab[(cc1%100)/10];
aa1=(cc1%100)/10;
a0=tab[cc1%10];
aa0=cc1%10;
dian=0x80;
cc2=(c1%c2)*10/c2;
b3=tab[cc2];
cc3=((c1%c2)*10%c2)*10/c2;
b2=tab[cc3];
cc4=(((c1%c2)*10%c2)*10%c2)*10/c2;
b1=tab[cc4];
cc5=((((c1%c2)*10%c2)*10%c2)*10%c2)*10/c2;
b0=tab[cc5];
if((((((c1%c2*10)%c2)*10%c2)*10%c2)*10%c2)*10/c2>=5)
{
b0=tab[cc5+1];
}
if(aa3==0)//消除多余的零
{
a3=0;
if(aa2==0)
{
a2=0;
if(aa1==0)
{
a1=0;
}
}
}
}
}
}
}
}
}
这个是数码管的
/********************************************************************************
*Design: qinhao
*********************************************************************************/
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
volatile char bf;
sbit rs=P3^0;
sbit rw=P3^1;
sbit e=P3^2;
#define bf P1^7
uchar code qin[]={
0x1F,0x11,0x11,0x11,0x11,0x11,0x11,0x1F, //方框
0x0E,0x0A,0x04,0x1F,0x04,0x0E,0x0A,0x1B, //小人
0x0A,0x15,0x0A,0x15,0x0A,0x15,0x0A,0x15, //黑白格子
0x1F,0x15,0x1F,0x1B,0x1B,0x1F,0x11,0x1F, //脸
0x0E,0x11,0x11,0x15,0x15,0x0E,0x04,0x04, //树
0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11, // 月
0x1F,0x00,0x1F,0x00,0x1F,0x00,0x1F,0x00, //黑白横条
0x15,0x15,0x15,0x15,0x15,0x15,0x15,0x15, //黑白竖条
};
void delay(unsigned int n)
{
unsigned int i,j;
for(j=n;j>0;j--)
for(i=112;i>0;i--);
} /////// 精确MS,
void check() // 判忙
{
rs=0;
rw=1;
e=0;
P1=0xff;
e=1;
_nop_();
_nop_();
while(P1&0x80){};
delay(10);
}
void sendc(uchar com) // 发命令字
{
check();
rs=0;
rw=0;
e=1;
P1=com;
_nop_();
e=0;
_nop_();
}
void sendd(uchar word) // 发显示字
{
check();
rs=1;
rw=0;
e=1;
P1=word;
_nop_();
e=0;
_nop_();
}
void inti() // 初始化
{
sendc(0x01);
sendc(0x38);
sendc(0x0f);
sendc(0x06);
delay(10);
}
void setcgram() ////// 写cgram
{
uchar x;
sendc(0x40);
for(x=0;x<64;x++)
{
sendd(*(qin+x));
};
}
void main()
{
unsigned char i;
setcgram();
inti();
sendc(0x80); /// 第一行
for(i=0;i<8;i++)
{
sendd(i+0x00);
};
sendc(0xc0); /// 第二行
for(i=0;i<8;i++)
{
sendd(0x07-i);
};
while(1){};
}
这是我对AVR编写的函数,参考一下思路吧
/*******************************************
函数名称: Key_scan
功 能: 扫描所按的键并返回键值
参 数: 无
返回值 : keycode--被按下键值
/********************************************/
uchar Mkey_scan(void)
{
uchar i=0x7F,keycode=0xFF; //i为扫描码,
Delayms(15); //延时消抖
if(Mkey_press()==TRUE) //如果有键按下
{
do
{
i=(i<<1|i>>7); //进行行扫描
KEY_DDR=0x0F; //定义数据端口低四位为输出,高四位为输入
KEY_PORTO=i; //输出扫描码
Delayms(1); //防止读入出错,加入延时
keycode=KEY_PORTI; //读入原始键码
}while((keycode&0xF0)==0xF0); //是否检测到被按下按键(扫描到就跳出循环,否则继续扫描)
do
{
}while(Mkey_press()==TRUE); //等待按键释放(为了防止按一次,执行多次的现象)
Delayms(15); //延时消抖
switch(keycode) //将原始键码翻译成0~F
{
case 0xEE:
keycode=0x0;
break;
case 0xDE:
keycode=0x1;
break;
case 0xBE:
keycode=0x2;
break;
case 0x7E:
keycode=0x3;
break;
case 0xED:
keycode=0x4;
break;
case 0xDD:
keycode=0x5;
break;
case 0xBD:
keycode=0x6;
break;
case 0x7D:
keycode=0x7;
break;
case 0xEB:
keycode=0x8;
break;
case 0xDB:
keycode=0x9;
break;
case 0xBB:
keycode=0xA;
break;
case 0x7B:
keycode=0xB;
break;
case 0xE7:
keycode=0xC;
break;
case 0xD7:
keycode=0xD;
break;
case 0xB7:
keycode=0xE;
break;
case 0x77:
keycode=0xF;
break;
}
}
return keycode; //返回经过翻译的键码
}