您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 简单的51单片机计算器程序
请支持原创,谢谢1、电路图2、程序:/***********************************************************************************************************designbyDXfromGDOU**************************************************************************************/#includereg51.h#includeintrins.h#defineuint8unsignedcharsbitled1=P1^0;sbitled2=P1^1;sbitled3=P1^2;sbitled4=P1^3;longintcount1,count2;//存放上次的输入和现在的输入uint8symbol,p;//存放符号的数值p用于多数输入清屏作用longintoutputcount;//存放结果uint8KeyCodeTable[]={0x28,0x14,0x24,0x44,0x12,0x22,0x42,0x11,0x21,0x41,0x18,0x88,0x84,0x82,0x81,0x48};//键盘按键特征表inttable[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90};//不懂uint8a[4];voidinit(){uint8j;a[0]=0xc0;for(j=1;j4;j++){a[j]=0xff;}count1=0;count2=0;symbol=0;p=0;outputcount=0;}voiddelayms(intx)//延时程序{uint8i;while(x--)for(i=0;i120;i++);}voiddisplay()//LED显示{P0=a[0];led1=0;delayms(5);led1=1;P0=a[1];led2=0;delayms(5);led2=1;P0=a[2];led3=0;delayms(5);led3=1;P0=a[3];led4=0;delayms(5);led4=1;}uint8keyscan()//键盘扫描{uint8sCode,kCode,i,k;P2=0xf0;if((P2&0xf0)!=0xf0){delayms(2);//软件去抖if((P2&0xf0)!=0xf0){sCode=0xfe;for(k=0;k4;k++){P2=sCode;delayms(12);if((P2&0xf0)!=0xf0){kCode=~P2;for(i=0;i16;i++)if(kCode==KeyCodeTable[i])return(i);}elsesCode=_crol_(sCode,1);}}}return(100);//没按键输入}voidoutput(longintx){inti;uint8j=0;uint8z[3]={0,0,0};if(x=1000){i=x/1000;z[0]=i;a[j]=table[i];j++;}if(x=100){i=(x-z[0]*1000)/100;z[1]=i;a[j]=table[i];j++;}if(x=10){i=(x-z[0]*1000-z[1]*100)/10;z[2]=i;a[j]=table[i];j++;}i=x%10;a[j]=table[i];}voidCalculate(uint8key)//计算程序并显示LED{uint8i,j;statick=0;if(key10){if(p){p=0;for(j=0;j4;j++){a[j]=0xff;}}a[k]=table[key];k=++k%4;count2*=10;count2+=key;outputcount=count2;//把当前按下的值先赋给outputcount}elseif(key==10)//清零键{k=0;init();}elseif(key15){k=0;p=1;switch(symbol)//多则运算时用于把之前的结果赋给COUNT1{case0:count1=count2;break;case11:count1+=count2;break;case12:count1-=count2;break;case13:count1*=count2;break;case14:count1/=count2;break;default:break;}count2=0;//有符号输入时把之前的值赋给count1,并把符号标号赋给symbol;symbol=key;}else{switch(symbol){case11:outputcount=count1+count2;break;case12:outputcount=count1-count2;break;case13:outputcount=count1*count2;break;case14:outputcount=count1/count2;break;default:break;}for(j=0;j4;j++){a[j]=0xff;}output(outputcount);}}voidmain(){uint8key;init();P0=0xff;led1=1;led2=1;led3=1;led4=1;while(1){display();if((key=keyscan())!=100){while((keyscan())!=100);Calculate(key);}}}
本文标题:简单的51单片机计算器程序
链接地址:https://www.777doc.com/doc-6110912 .html