目录
资料下载地址:51单片机四位数码管计算器 Proteus仿真程序
概要
1.系统通过4x4的矩阵键盘输入数字及运算符。
2.可以进行4位十进制数以内的加法运算,如果计算结果超过4位十进制数,则屏幕显示E
3.可以进行加法以外的计算 (乘、除、减) 。
仿真图
部分代码
cpp
void main(){
while(1){
keyScan();
pd();
display(num);
}
}
void keyAdd(){
if(numKey<10000){
num=num*10+numKey;
numKey=10000;
}
}
void pd(){
keyAdd();
if(fuhao=='c'){
num=0;
num0=0;
fuhao0='0';
fuhao='0';
}
if(fuhao=='+'){
fuhao0='+';
fuhao='0';
num0=num;
num=0;
}
if(fuhao=='-'){
fuhao0='-';
fuhao='0';
num0=num;
num=0;
}
if(fuhao=='*'){
fuhao0='*';
fuhao='0';
num0=num;
num=0;
}
if(fuhao=='/'){
fuhao0='/';
fuhao='0';
num0=num;
num=0;
}
if(fuhao=='='){
if(fuhao0=='+')num=num0+num;
else if(fuhao0=='-')num=num0-num;
else if(fuhao0=='*')num=num0*num;
else if(fuhao0=='/')num=num0/num;
fuhao='0';
num0=0;
}
}
void keyScan(){
P3=0xef;//1110 1111
if(!P3_3){numKey=7;while(!P3_3);}if(!P3_2){numKey=8;while(!P3_2);}if(!P3_1){numKey=9;while(!P3_1);}if(!P3_0){fuhao='/';while(!P3_0);}
P3=0xdf;
if(!P3_3){numKey=4;while(!P3_3);}if(!P3_2){numKey=5;while(!P3_2);}if(!P3_1){numKey=6;while(!P3_1);}if(!P3_0){fuhao='*';while(!P3_0);}
P3=0xbf;
if(!P3_3){numKey=1;while(!P3_3);}if(!P3_2){numKey=2;while(!P3_2);}if(!P3_1){numKey=3;while(!P3_1);}if(!P3_0){fuhao='-';while(!P3_0);}
P3=0x7f;
if(!P3_3){fuhao='c';while(!P3_3);}if(!P3_2){numKey=0;while(!P3_2);}if(!P3_1){fuhao='=';while(!P3_1);}if(!P3_0){fuhao='+';while(!P3_0);}
}
void display(int num1){
if(num1<= 9999)
{
n4=num1%10;
n3=num1/10%10;
n2=num1/100%10;
n1=num1/1000%10;
P2=0x01;
P0=duan[n1];
DelayXms(4);
P2=0x02;
P0=duan[n2];
DelayXms(4);
P2=0x04;
P0=duan[n3];
DelayXms(4);
P2=0x08;
P0=duan[n4];
DelayXms(4);
}
else
{
P2=0x01;
P0=duan[10];
DelayXms(4);
P2=0x02;
P0=duan[10];
DelayXms(4);
P2=0x04;
P0=duan[10];
DelayXms(4);
P2=0x08;
P0=duan[10];
DelayXms(4);
}
}
void DelayXms(unsigned int x)
{ unsigned char i, j;
while(x--)
{ i = 2;
j = 239;
do{while (--j);
}while (--i);
}
}