📄 calculator.c
字号:
/******************************************************************************************************
******************************************************************************************************/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
uchar keyVal=0; //读取的键盘值
uint lOpD=0; //左操作数
uint rOpD=0; //右操作数
ulong res=0; //计算结果
/*uchar code table0[16]={
0x48,0x84,0x44,0x24, // 0 1 2 3
0x82,0x42,0x22,0x81, // 4 5 6 7
0x41,0x21,0x18,0x14, // 8 9 + -
0x12,0x11,0x88,0x28 // * / C =
};*/
uchar code table1[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //LED段码
uchar getKey(ulong d);
void display(ulong d);
void delay(uchar t);
//====================================================================================================
//====================================================================================================
void main()
{
uchar op1,op2,count=0,key=0;
while(1)
{
key=0;
count=0;
L: while(key<10) //获取左操作数
{
display(res);
key=getKey(lOpD);
if(count==0&&key>=10) //第一次按键是操作符时继续
{
lOpD=0;
key=0;
}
count++;
if(key<10)
{
lOpD=lOpD*10+key;
display(lOpD);
}
if(key==14||lOpD>=65535) //若按下 ON/C 键或溢出全部清0重来
{
lOpD=0;
key=0;
}
if(key==15) //若按下 = 继续读左操作数
key=0;
}
op1=key; //保存操作符
key=0;
R: while(key<10) //获取右操作数
{
key=getKey(rOpD);
display(rOpD);
if(key<10)
{
rOpD=rOpD*10+key;
display(rOpD);
}
if(key==14||lOpD>=65535) //若按下 ON/C 键或溢出全部清0重来
{
lOpD=0;
rOpD=0;
key=0;
goto L;
}
}
op2=key;
switch(op1)
{
case 10:res=lOpD+rOpD;break; //
case 11:res=lOpD-rOpD;break; //
case 12:res=lOpD*rOpD;break; //
case 13:res=lOpD/rOpD;break; //
default:res=0;break;
}
if(res>999999) //结果溢出
{
lOpD=0;
rOpD=0;
res=0;
key=0;
P3=0xFF;
}
display(res);
if(op2>=10&&op2<=13)
{
op1=op2;
lOpD=res;
key=0;
goto R;
}
P0=0xF0;
while(P0==0xF0)
{
display(res);
P0=0xF0;
};
key=getKey(res);
if(14==key)
{
lOpD=0;
rOpD=0;
res=0;
key=0;
//P3=0x00;
display(0);
}
}
}
//====================================================================================================
//====================================================================================================
uchar getKey(ulong d)
{
uchar temp=0;
uchar low=0x0F;
uchar row=0;
uchar col=0;
uchar key=0;
uchar i=0,move;
P3=0x00;
S: P0=0x0F;
temp=P0&0x0F;
while(temp==0xF)
{
P0=0x0F;
temp=P0&0x0F;
display(d);
};
delay(20);
P0=0x0F;
temp=P0&0x0F;
if(temp==0x0F)
goto S;
temp=0x0F&temp;
switch(temp)
{
case 0x0E:row=0;break;
case 0x0D:row=1;break;
case 0x0B:row=2;break;
case 0x07:row=3;break;
default:row=0;break;
}
move=0xF0;
for(i=0;i<4;i++)
{
move<<=1;
P0=move|0x0F;
temp=P0&0x0F;
if(temp!=0x0F)
{
col=3-i;
break;
}
}
key=row*4+col;
P0=0x0F;
temp=P0&0x0F;
while(temp!=0xF)
{
display(d);
P0=0x0F;
temp=P0&0x0F;
}; //wait for key up
return key;
}
//====================================================================================================
//====================================================================================================
void display(ulong d)
{
uchar out[6]={0,0,0,0,0,0};
uchar temp,i,j,k;
ulong t=100000;
ulong b=d;
uchar count=5,move=0;
for(i=0;i<6;i++)
{
temp=b/t;
b=b%t;
t/=10;
out[i]=table1[temp];
if(count==5&&temp!=0)
count=i;
}
for(j=0;j<20;j++)
{
move=0x80;
for(k=5;k>=count;k--)
{
P3=move;
P2=out[k];
move>>=1;
delay(1);
}
}
}
//====================================================================================================
//====================================================================================================
void delay(uchar t) //1ms*t
{
uchar i,j;
for(i=0;i<t;i++)
{
for(j=0;j<121;j++);
}
}
//====================================================================================================
//====================================================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -