《简单的51单片机计算器程序(共6页).doc》由会员分享,可在线阅读,更多相关《简单的51单片机计算器程序(共6页).doc(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上请支持原创,谢谢1、电路图2、程序:/*design by DX from GDOU*/#include #include #define uint8 unsigned charsbit led1=P10;sbit led2=P11;sbit led3=P12;sbit led4=P13;long int count1,count2; /存放上次的输入和现在的输入uint8 symbol,p;/存放符号的数值 p用于多数输入清屏作用long int outputcount; /存放结果uint8 KeyCodeTable=0x28,0x14,0x24,0x44,0x1
2、2,0x22,0x42,0x11,0x21,0x41,0x18,0x88,0x84,0x82,0x81,0x48;/键盘按键特征表 int table=0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90 ;/不懂uint8 a4;void init()uint8 j;a0=0xc0;for(j=1;j4;j+)aj=0xff; count1=0;count2=0;symbol=0;p=0;outputcount=0;void delayms(int x) /延时程序 uint8 i; while(x-) for(i=0;i120;i+); voi
3、d display() /LED显示P0 = a0;led1=0;delayms(5);led1=1;P0 = a1;led2=0;delayms(5);led2=1;P0 = a2;led3=0;delayms(5);led3=1;P0 = a3;led4=0;delayms(5);led4=1;uint8 keyscan() /键盘扫描 uint8 sCode,kCode,i,k; P2=0xf0; if(P2&0xf0)!=0xf0) delayms(2); /软件去抖 if(P2&0xf0)!=0xf0) sCode=0xfe; for(k=0;k4;k+) P2=sCode; del
4、ayms(12); if(P2&0xf0)!=0xf0) kCode=P2; for(i=0;i=1000)i=x/1000;z0=i;aj=tablei;j+;if(x=100)i=(x-z0*1000)/100;z1=i;aj= tablei;j+; if(x=10)i=(x-z0*1000-z1*100)/10;z2=i;aj= tablei;j+;i=x%10;aj=tablei;void Calculate(uint8 key) /计算程序并显示LEDuint8 i,j; static k=0;if(key10) if(p) p=0; for(j=0;j4;j+)aj=0xff; a
5、k=tablekey; k=+k%4;count2*=10;count2+=key;outputcount=count2; /把当前按下的值先赋给outputcountelse if(key=10) /清零键 k=0 ; init(); else if(key15) k=0; p=1; switch(symbol) /多则运算时用于把之前的结果赋给COUNT1 case 0 :count1=count2 ;break; case 11:count1+=count2;break;case 12:count1-=count2;break;case 13:count1*=count2;break;c
6、ase 14:count1/=count2;break;default: break; count2=0; /有符号输入时把之前的值赋给count1,并把符号标号赋给symbol; symbol=key; else switch(symbol) case 11:outputcount=count1+count2;break;case 12:outputcount=count1-count2;break;case 13:outputcount=count1*count2;break;case 14:outputcount=count1/count2;break;default:break;for(j=0;j4;j+)aj=0xff; output(outputcount); void main()uint8 key;init(); P0=0xff;led1=1;led2=1;led3=1;led4=1;while(1)display();if(key=keyscan()!=100) while(keyscan()!=100) ;Calculate(key); 专心-专注-专业