1基于51单片机的简易计算器/****************************************************************阿斌独家制作:计算器数码管显示可计算10以下数加减乘除可连续运算最大显示结果数值9999*****************************************************************/键盘设置:主程序:#includereg52.h#defineuintunsignedint#defineucharunsignedcharucharnum,temp,jia=0,jian=0,cheng=0,chu=0,qing=0;uintkey,key1;uchartable[]={20x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x0};voiddelay(uintz){uintx,y;for(x=z;x0;x--)for(y=110;y0;y--);}voidkeyscan(){P3=0xfe;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;3switch(temp){case0xee:key=1;num=0;break;case0xde:key=2;num=0;break;case0xbe:key=3;num=0;break;case0x7e:num=1;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;4while(temp!=0xf0){temp=P3;switch(temp){case0xed:key=4;num=0;break;case0xdd:key=5;num=0;break;case0xbd:key=6;num=0;break;case0x7d:num=2;break;//减号}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}P3=0xfb;temp=P3;temp=temp&0xf0;while(temp!=0xf0){5delay(5);temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case0xeb:key=7;num=0;break;case0xdb:key=8;num=0;break;case0xbb:key=9;num=0;break;case0x7b:num=3;break;//乘号}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}}P3=0xf7;temp=P3;6temp=temp&0xf0;while(temp!=0xf0){delay(5);temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case0xe7:key=0;num=0;break;case0xd7:num=6;break;//清除case0xb7:num=5;break;//等于号case0x77:num=4;break;//除号}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}}7}}voiddisplay(uintshu){uintqian,bai,shi,ge;qian=shu/1000;bai=shu%1000/100;shi=shu%100/10;ge=shu%10;P2=0x1f;if(qian==0)P0=table[10];elseP0=table[qian];delay(5);P2=0x2f;if(qian==0&&bai==0)P0=table[10];elseP0=table[bai];8delay(5);P2=0x4f;if(qian==0&&bai==0&&shi==0)P0=table[10];elseP0=table[shi];delay(5);P2=0x8f;P0=table[ge];delay(5);}voidjia1(){jia=1;jian=cheng=chu=qing=0;key1=key;}voidjian1(){jian=1;9jia=cheng=chu=qing=0;key1=key;}voidcheng1(){cheng=1;jia=jian=chu=qing=0;key1=key;}voidchu1(){chu=1;jia=jian=cheng=qing=0;key1=key;}voidqing1(){qing=1;jia=jian=cheng=chu=0;key1=key=0;}voiddeng1()10{if(jia)key=key1+key;if(jian)key=key1-key;if(cheng)key=key1*key;if(chu)key=key1/key;display(key);jia=jian=cheng=chu=qing=0;}voiddeal(){switch(num){case0:display(key);break;case1:jia1();break;case2:jian1();break;case3:cheng1();break;case4:chu1();break;case5:deng1();break;11case6:qing1();break;}}voidmain(){P2=0x0f;while(1){keyscan();deal();}}12