时间有限,只用了数码管显示一位的数的加减乘除
唯一的两个亮点,就是减法数码管能显示负数
除法能显示小数
所有的Proteus仿真图和C源代码都在里面
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #include<math.h>
- sbit Sel1=P3^0;
- sbit Sel2=P3^1;
- sbit Sel3=P3^2;
- sbit Sel4 =P3^3;
- sbit Sel5=P3^4;
- sbit Sel6 =P3^5;
- sbit led=P2^0;
- unsigned char line=0,cross=0,comline=0,comcross=0;//显示扫描后的行列值
- unsigned char shu1=0,shu2=0;//显示计算的数据
- unsigned char keynum=0,workflag=0;
- int keyflag=0,i=0,cons=0,cons1,cons2; //显示按键和运算状态的逻辑标志变量
- unsigned char Shuma[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//数码管段码值
- unsigned char Shuma1[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//数码管段码值
- void delay(int k); //延时函数
- void Keycode(); //键盘编码函数
- void Keys(); //键盘扫描函数
- void main()
- {
- unsigned char n=0;
- unsigned int temp=0;
- TMOD=0x01;
- TH0=(2^16-50000)/2^8;TL0=(2^16-50000)%2^8;EA=1;TR0=1;ET0=1;
- while(1)
- {
- Keycode();
- if(keyflag == 1)
- shu1 = keynum;
- else if(keyflag == 2)
- shu2 = keynum;
- else if(keyflag == 3)
- {
- switch(workflag)
- {
- case 1:cons = shu1+shu2;break;
- case 2:cons = shu1-shu2;break;
- case 3:cons = shu1*shu2;break;
- case 6:temp =(int)(((float)(shu1)/(float)(shu2))*10);cons1 = temp/10;cons2=temp%10;
- break;
- default:break;
- }
- }
- else ;
-
- if (workflag == 5)
- {
- keyflag = 0;cross=0;line=0;workflag=0;keynum=0;P0=0xff;
- shu1=0;shu2=0;cons=0;
- }
-
- for(n=0;n<8;n++)
- {
- switch(n)
- {
- case 0:Sel1=1,Sel2=0,Sel3=0,Sel4=0,Sel5=0,Sel6=0;P0=~Shuma[shu1/10];break;
- case 1:Sel1=0,Sel2=1,Sel3=0,Sel4=0,Sel5=0,Sel6=0;P0=~Shuma[shu1%10];break;
- case 2:Sel1=0,Sel2=0,Sel3=1,Sel4=0,Sel5=0,Sel6=0;P0=~Shuma[shu2/10];break;
- case 3:Sel1=0,Sel2=0,Sel3=0,Sel4=1,Sel5=0,Sel6=0;P0=~Shuma[shu2%10];break;
- case 4:Sel1=0,Sel2=0,Sel3=0,Sel4=0,Sel5=1,Sel6=0;
- if(cons >= 0)
- P0=~Shuma[cons/10];
- else
- P0=0xBF;
- if(workflag == 6)
- {
- P0=~Shuma1[cons1];
- }break;
- case 5:Sel1=0,Sel2=0,Sel3=0,Sel4=0,Sel5=0,Sel6=1;
- P0=~Shuma[abs(cons)%10];
- if(workflag == 6)
- P0=~Shuma[cons2];break;
- default:break;
- }
- delay(20);
- P0=0xff;
- }
- }
- }
- void delay(int k)
- {
- while(k--);
- }
- void Keycode()
- {
- switch(cross)
- {
- case 1:
- switch(line)
- {
- case 1:keynum=1;break;
- case 2:keynum=2;break;
- case 3:keynum=3;break;
- case 4:workflag=1;break;
- default:break;
- };break;
-
- case 2:
- switch(line)
- {
- case 1:keynum=4;break;
- case 2:keynum=5;break;
- case 3:keynum=6;break;
- case 4:workflag=2;break;
- default:break;
- };break;
-
- case 3:
- switch(line)
- {
- case 1:keynum=7;break;
- case 2:keynum=8;break;
- case 3:keynum=9;break;
- case 4:workflag=3;break;
- default:break;
- };break;
-
- case 4:
- switch(line)
- {
- case 1:keynum=0;break;
- case 2:workflag=4;break;
- case 3:workflag=5;break;
- case 4:workflag=6;break;
- default:break;
- };break;
- default:break;
- }
- }
- void Keys()
- {
- P1=0xf0;
- if( P1 != 0xf0)
- keyflag++;
- switch(P1)
- {
- case 0x70:cross=1;break;
- case 0xb0:cross=2;break;
- case 0xd0:cross=3;break;
- case 0xe0:cross=4;break;
- }
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
Calculate.rar
(17.67 KB, 下载次数: 116)
|