4位数加法计算器的设计
1.系统通过4x4的矩阵键盘输入数字及运算符。 (40分)
2.可以进行4位十进制数以内的加法运算,如果计算结果超过4位十进制数,则屏幕显示E(30分)
3.可以进行加法以外的计算 (乘、除、减) 。 (20 )。4@,其他功能 (创新部分 10分)
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:- #include <REGX52.H>
- void display(int num1);
- void DelayXms(unsigned int x);
- void keyScan();
- void keyAdd();
- void pd();
- int n1, n2, n3, n4, numKey=10000, num=0, num0=0;
- char fuhao='0',fuhao0='0';
- unsigned char duan[11]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x86};
- 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);
- }
- }
复制代码
仿真程序:
四位数码管计算器.7z
(51.33 KB, 下载次数: 31)
|