<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

      1. 專注電子技術學習與研究
        當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

        51單片機計算器源程序

        作者:huqin   來源:本站原創   點擊數:  更新時間:2014年04月08日   【字體:
        終于做完了。。。都是坑,電路:p0口是數碼管的段選,
        sbit LSA=P2^2;
        sbit LSB=P2^3;
        sbit LSC=P2^4;這三個是數碼管的位選,矩陣鍵盤接在p1口. #include<reg51.h> void Delay10ms(unsigned int c); unsigned char code Num[17]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71,0x77}; int Data[8]; void err(int id); void DigDisplay(); int scan(); double runnum(int c); int rpn=0,rpm=0,rpi=0,rpt=0,rpo=0; double rnum1=0,rnum2=0; double rnum3=0,rnum4=0; void outputnum(double op); void run(int numb); sbit LSA=P2^2; sbit LSB=P2^3; sbit LSC=P2^4; void main() { int res=16; while(1) { for(;;) { DigDisplay(); res=scan(); if (res!=16) { if(res!=17) { run(res); break; } else err(02); } } } } void Delay10ms(unsigned int c) { unsigned char a, b; for (;c>0;c--) { for (b=38;b>0;b--) { for (a=130;a>0;a--); } } } void DigDisplay() { unsigned char i; unsigned int j; P0=0x00; for(i=0;i<8;i++) { switch(i) //位選,選擇點亮的數碼管, { case(7): LSA=0; LSB=0; LSC=0; break; case(6): LSA=1; LSB=0; LSC=0; break; case(5): LSA=0; LSB=1; LSC=0; break; case(4): LSA=1; LSB=1; LSC=0; break; case(3): LSA=0; LSB=0; LSC=1; break; case(2): LSA=1; LSB=0; LSC=1; break; case(1): LSA=0; LSB=1; LSC=1; case(0): LSA=1; LSB=1; LSC=1; break; } P0=Data[i]; j=10; while(j--); P0=0x00; } } void err(int id)//ok { int t=0,g=0; rpt=1; t=id/16; g=id%16; Data[7]=Num[t]; Data[6]=Num[g]; Data[5]=0x40; Data[1]=Num[0]; Data[0]=Num[16]; Data[3]=Num[16]; Data[2]=Num[16]; Data[4] =Num[14]; DigDisplay(); rpm=0; rpn=0; rpi=0; rpo=0; rnum1=0; rnum2=0; rnum4=0; rnum3=0; } int scan() { int re,a=0; re=0; P1=0x0f; if (P1!=0x0f) { Delay10ms(1); if (P1!=0x0f) { P1=0X0f; switch (P1) { case 0x07: re =0; break; case 0x0b: re =1; break; case 0x0d: re =2; break; case 0x0e: re =3; break; default:re=17; } if (re==17) return 17; P1=0Xf0; switch (P1) { case 0x70: re =re+0; break; case 0xb0: re =re+4;break; break; case 0xd0: re =re+8; break; case 0xe0: re =re+12; break; default:re=17; } if (re==17) return 17; while((a<50)&&(P1!=0xf0)) //按鍵松手檢測 { Delay10ms(1); a++; } a=0; return re; } } else return 16; } void run(int numb) { int rz=99,pi=0; int lo=0; double x; if (rpt==1) { for(pi=0;pi<8;pi++) Data[pi]=0x00; rpt=0; return; } switch (numb) { case 0:rz=7; break; case 1:rz=8; break; case 2:rz=9; break; case 4:rz=4; break; case 5:rz=5; break; case 6:rz=6; break; case 8:rz=1; break; case 9:rz=2; break; case 10:rz=3; break; case 3:rz=90; break;//+ case 7:rz=91; break; //- case 11:rz=92; break; //* case 15:rz=93; break; // / case 12:rz=94; break;//. case 13:rz=0; break; case 14:rz=95; break;//= } if(rz>=0&&rz<=9) { if (rpn>=7) { err(0x0a); return; } if (rpm==0) { rnum1=rz+rnum1*10; rpn=rpn+1; if(rnum1==0) rpn=rpn-1; rnum4=rnum1+rnum2; if(rnum4>9999999) err(0xCC); outputnum(rnum4); return; } else { x=rz; for(pi=0;pi<=rpi;pi++) x=x*0.1 ; rpi++; rpn++; rnum2=rnum2+x; rnum4=rnum1+rnum2; if(rnum4>9999999||rnum4<-9999999) err(0xCD); outputnum(rnum4); return; } } if(rz==94) { if(rnum1==0) rpn++; if(rpm==0) { rpm=1; if(rnum1+rnum2==0) Data[0]=Num[0]+0x80; else Data[0]=Data[0]+0x80; } else err(0x0C); } if (rz==90) { if(rpo==0) { rpo=1; rnum3=rnum4; rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; outputnum(rnum3); return; } if(rpo!=0) { rnum3=runnum(rpo); outputnum(rnum3); if(rnum3>9999999||rnum3<-9999999) err(0xCe); rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; rpo=1; return; } } if (rz==91) { if(rpo==0) { rpo=2; rnum3=rnum4; rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; outputnum(rnum3); return; } if(rpo!=0) { rnum3=runnum(rpo); outputnum(rnum3); if(rnum3>9999999||rnum3<-9999999) err(0xCe); rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; rpo=2; return; } } i if (rz==92) { if(rpo==0) { rpo=3; rnum3=rnum4; rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; outputnum(rnum3); return; } if(rpo!=0) { rnum3=runnum(rpo); outputnum(rnum3); if(rnum3>9999999||rnum3<-9999999) err(0xCe); rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; rpo=3; return; } } if (rz==93) { if(rpo==0) { rpo=4; rnum3=rnum4; rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; outputnum(rnum3); return; } if(rpo!=0) { rnum3=runnum(rpo); outputnum(rnum3); if(rnum3>9999999||rnum3<-9999999) err(0xCe); rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; rpo=4; return; } } if (rz==95) //= { if(rpo==0) { outputnum(rnum3); rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; rnum3=0; return; } if(rpo!=0) { rnum3=runnum(rpo); outputnum(rnum3); if(rnum3>9999999||rnum3<-9999999) err(0xCe); rpm=0; rpn=0; rpi=0; rnum1=0; rnum2=0; rnum4=0; rpo=0; rnum3=0; return; } } } void outputnum(double op) { long int x; int i=0,k=10,lx; double y,op0; for(i=0;i<8;i++) Data[i]=0x00; if(op<0) { op=-op; Data[7]=0x40; } x=op; y=op-x; if (op>=1||op<=-1) { if(y==0) for(i=0;i<7;i++) { Data[i]=Num[x%10]; x=x/10; if(x==0) break; } else { op0=op; for(i=0;i<7;i++) { op0=op0*10; x=op0; if(op0==x) break; } lx=i; x=op0; for(i=0;i<7;i++) { Data[i]=Num[x%10]; x=x/10; if(x==0) break; } Data[lx+1]=Data[lx+1]+0x80; x=op; if(x==0) Data[i+1]=Num[0]+0x80; } } else { if(op==0) { Data[0]=Num[0]; if(rpm==1) Data[0]=Num[0]+0x80; return; } op0=op; for(i=0;i<7;i++) { op0=op0*10; x=op0; if(op0==x) break; } lx=i; for(i=0;i<=lx;i++) Data[i]=Num[0]; for(i=0;i<7;i++) { Data[i]=Num[x%10]; x=x/10; if(x==0) break; } Data[lx+1]=Num[0]+0x80; } } double runnum(int c) { double a; switch (c) { case 1: a=rnum3+rnum4; break; case 2: a=rnum3-rnum4; break; case 3: a=rnum3*rnum4; break; case 4: if (rnum4!=0) { a=rnum3/rnum4; break; } else err(0xC0); } if(rnum3>=999999||rnum3<=-999999) err(0x33); return a; }
        關閉窗口

        相關文章

        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮