#includereg52.h#defineuintunsignedint#defineucharunsignedcharsbitdula=P2^6;sbitwela=P2^7;sbitkey1=P3^4;ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};ucharnum,temp,num1;voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}ucharkeyscan();voiddisplay(ucharaa);voidmain(){num=17;dula=1;P0=0;dula=0;wela=1;P0=0xc0;wela=0;while(1){display(keyscan());}}voiddisplay(ucharaa){dula=1;P0=table[aa-1];dula=0;}ucharkeyscan(){P3=0xfe;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case0xee:num=1;break;case0xde:num=2;break;case0xbe:num=3;break;case0x7e:num=4;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}P3=0xfd;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case0xed:num=5;break;case0xdd:num=6;break;case0xbd:num=7;break;case0x7d:num=8;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}P3=0xfb;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case0xeb:num=9;break;case0xdb:num=10;break;case0xbb:num=11;break;case0x7b:num=12;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}P3=0xf7;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case0xe7:num=13;break;case0xd7:num=14;break;case0xb7:num=15;break;case0x77:num=16;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}returnnum;}/***************89C51单片机【计算器】C语言程序******************//****************P2位选P0段选时钟12MHZ*********************/#includereg52.h/*包含的头文件*/#defineucharunsignedchar/*宏定义*/#defineuintunsignedintucharLed[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};/*数码管段选*/longfloatNumber[]={0,0,0,0};/*数码管显示位控制寄存器*/ucharA[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};longintD[]={0,0,0,0,0,0,0,0};//数码管显示内容寄存器ucharcodeC[]={0x0,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};/*数码管位选*//*列扫描控制LED1位2位3位4位5位6位7位8位*/uchark=16;/*按键对外控制*/ucharb;/*按键【+】【-】【*】【/】对外的控制*/longfloatOut_put;/*定义变量【Out_put】作为结果*/uchare=0;/*数字的位存储递进的控制*/ucharg=0;/*运算方式的控制变量*/ucharL=0;/*运算方式的控制变量*/ucharg1=0;/*运算方式的控制变量*/ucharg2=0;/*运算方式的控制变量*/ucharg3=0;/*运算方式的控制变量*/ucharg4=0;/*运算方式的控制变量*/charj=-1;/*与Number[]数组连用*/ucharm=0;/*按键【=】的控制变量*/ucharn=0;/*按键【.】的控制变量*/ucharx=0;/*小数点个数的记录变量*/ucharxo=0;/*控制开始计数小数点的变量*/longintresult;voidDelay(uinto)/*延时函数delay()的定义*/{uinti,j;for(i=0;io;i++){for(j=0;j121;j++){;}}}voidshow(longfloatOut_put){ucharr;uchark;longintq,p;uchars=0;uchari;longinttemp;temp=Out_put;if((Out_put-temp)!=0){result=Out_put*10000;r=4;}else{result=Out_put;r=0;}p=result;if(m==1){if(result0){result=-result;p=result;q=result;for(i=1;i=8;i++){q=q/10;if(q==0){k=i;i=9;}}P0=0x40;P2=C[8-k];Delay(1);}//负号的显示P0=0x80;P2=C[r];Delay(1);P0=0x00;result=p;P0=Led[result%10];P2=C[8];result=result/10;Delay(1);if((result%10==0)&&(result%100==0)&&(result%1000==0)&&(result%10000==0)&&(result%100000==0)&&(result%1000000==0)){P0=0x00;}else{P0=Led[result%10];P2=C[7];result=result/10;Delay(1);}if((result%10==0)&&(result%100==0)&&(result%1000==0)&&(result%10000==0)&&(result%100000==0)){P0=0x00;}else{P0=Led[result%10];P2=C[6];result=result/10;Delay(1);}if((result%10==0)&&(result%100==0)&&(result%1000==0)&&(result%10000==0)){P0=0x00;}else{P0=Led[result%10];P2=C[5];result=result/10;Delay(1);}if((result%10==0)&&(result%100==0)&&(result%1000==0)){P0=0x00;}else{P0=Led[result%10];P2=C[4];result=result/10;Delay(1);}if((result%10==0)&&(result%100==0)){P0=0x00;}else{P0=Led[result%10];P2=C[3];result=result/10;Delay(1);}if((result%10==0)){P0=0x00;}else{P0=Led[result%10];P2=C[2];result=result/10;Delay(1);}if(result==0){P0=0x00;}else{P0=Led[result%10];P2=C[1];result=result/10;Delay(1);}}}voidIn_put()//输入函数输入小数加小数点{uchari;if(k=0&&k=9)switch(e){case8:D[7]=D[6];A[7]=A[6];//用来存放数值case7:D[6]=D[5];A[6]=A[5];case6:D[5]=D[4];A[5]=A[4];case5:D[4]=D[3];A[4]=A[3];case4:D[3]=D[2];A[3]=A[2];case3:D[2]=D[1];A[2]=A[1];case2:D[1]=D[0];A[1]=A[0];case1:if(n==0){D[0]=k;A[0]=Led[k];if(xo==1)x++;}if(n==1){A[0]=Led[k]|0x80;xo=1;n=0;}}if(k=11&&k=15&&b==1){switch(k){case11:g1++;g++;if(g2==1)L=1;if(g3==1)L=2;if(g4==1)L=3;break;//+case12:g2++;g++;if(g1==1)L=4;if(g3==1)L=5;if(g4==1)L=6;break;//-case13:g3++;g++;if(g1==1)L=7;if(g2==1)L=8;if(g4==1)L=9;break;//*case14:g4++;g++;if(g1==1)L=10;if(g2==1)L=11;if(g3==1)L=12;break;///}j++;Number[j]=10000000*D[7]+1000000*D[6]+100000*D[5]+10000*D[4]+1000*D[3]+100*D[2]+10*D[1]+D[0];for(i=1;i=x;i++){Number[j]=(float)Number[j]/10;}x=0;for(i=0;i=7;i++)//数据存储器清零D[i]=0;for(i=0;i=8;i++)//数据显示清零{A[i]=0x00;}b=0;e=0;xo=0;}}voidKey_scan()//键盘扫描函数Key_scan(){unsignedcharX,Y,Z;P3=0xff;P3=0x0f;//先对P2置数行扫描if(P3!=0x0f)//判断是否有键按下{Delay(20);//延时10ms,软件去干扰if(P3!=0x0f)//确认按键按下;{X=P3;//保存行扫描时有键按下时状态P3=0xf0;//列扫描Y=P3;//保存列扫描时有键按下时状态Z=X|Y;//取出键值while(P3!=0xf0);//按键防止抖动switch(Z)//判断键值(那一个键按下){case0xe7:k=0;e++;In_put();break;//数字【0】case0xd7:k=1;e++;In_put();break;//数字【1】case0xdb:k=2;e++;In_put();break;//数字【2】ca