|
使用keil軟件生成hex文件,燒錄進(jìn)單片機(jī)即可上電運行
下載:
簡易計算器.doc
(42.5 KB, 下載次數(shù): 12)
2017-6-15 13:09 上傳
點擊文件名下載附件
C語言源程序 下載積分: 黑幣 -5
源程序:
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define LED P0
- #define KEY P1
- sbit led1=P2^2;
- sbit led2=P2^3;
- sbit led3=P2^4;
- sbit Beep=P1^5;
- sbit SRCLK=P3^6;
- sbit RCLK=P3^5;
- sbit SER=P3^4;
- unsigned char code CHARCODE[6][8]={
- {0x00,0x00,0x08,0x08,0x3e,0x08,0x08,0x00},
- {0x00,0x00,0x08,0x08,0x08,0x08,0x08,0x00},
- {0x00,0x00,0x22,0x14,0x08,0x14,0x22,0x00},
- {0x00,0x00,0x08,0x08,0x2a,0x08,0x08,0x00},
- {0x00,0x00,0x14,0x14,0x14,0x14,0x14,0x00},
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
- };
- uchar code TAB[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
- uchar code shu[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void zhifu(uchar q);
- void yinyue(uchar m);
- void xuehao(void);
- void delay10ms(uchar c);
- uchar keyscan(void);
- void Hc595Byte(uchar dat);
- void lay(unsigned int i);
- int main()
- {
- uchar a=0x00,num1=0x00,num2=0x00,num3=0x00,w=0x00,w2=0x00,g,an,d,ans1,ans2,ans3,C,L;
- while(1) //++++++++++++++++++?a??????+++++++++++++++++++++++++++
- {
- xuehao();
- a=keyscan();
- if(a==16)
- break;
- }
- while(1)
- {
- g=0x00; // 存鍵入的第一個字符
- while(1)
- { num1=keyscan();
- if(num1<10)
- break;
- }
- while(1)
- { w2=w; //存鍵入的第一個數(shù)
- d=g;//+-*/
- g=0;
- w=num1;//存鍵入的數(shù)
- L=num1; //
- while(1)//顯示鍵入的第一位數(shù)
- {
- led3=0,led2=0,led1=0,LED=shu[num1],LED=0x00;
- num2=keyscan();
- if(num2<=16)
- break;
- }
- if(num2<10)
-
- {
-
- w=(num1)*10+num2;//當(dāng)前數(shù)值
- while(1)
-
- {
-
- led3=0,led2=0,led1=0,LED=shu[num2],LED=0x00;
- led3=0,led2=0,led1=1,LED=shu[num1],LED=0x00;
- num3=keyscan();
- if(num3<=16)
- {
- if(num3<10)
- {
- w=(num1)*100+(num2)*10+num3;
- while(1)
- {
- led3=0,led2=0,led1=0,LED=shu[num3],LED=0x00;
- led3=0,led2=0,led1=1,LED=shu[num2],LED=0x00;
- led3=0,led2=1,led1=0,LED=shu[num1],LED=0x00;
- g=keyscan();
- if(g==15)
- break;
- if(g>10&&g<16) //判斷是否為+-*/
- {
- while(1)
- {
- led3=0,led2=0,led1=0,LED=shu[num3];
- lay(5);
- LED=0x00; //消隱
- led3=0,led2=0,led1=1,LED=shu[num2];
- lay(5);
- LED=0x00;
- led3=0,led2=1,led1=0,LED=shu[L];
- lay(5);
- LED=0x00;
- led3=1,led2=1,led1=1,zhifu(g),LED=0x00;
- num1=keyscan();
- if(num1<10)
- break;
- }
- }
- if(g>10&&g<16)
- break;
- }
- }
- else
- { g=num3;
- if(g==15)
- break;
- while(1)
- {
- led3=0,led2=0,led1=0,LED=shu[num2];
- lay(5);
- LED=0x00;
- led3=0,led2=0,led1=1,LED=shu[L];
- lay(5);
- LED=0x00;
- led3=1,led2=1,led1=1,zhifu(g),LED=0x00;
- num1=keyscan();
- if(num1<10)
- break;
- }
- }
- if(g==15)
- break;
- }
- if(g>10)
- break;
- }
- }
- else
- { g=num2;
- w=num1;
- if(g==15) //================================================
- break; //===============================================
- while(1) //===================================================
- {
- led3=0,led2=0,led1=0,LED=shu[L];
- lay(5);
- LED=0x00;
- led3=1,led2=1,led1=1,zhifu(g),LED=0x00;
- num1=keyscan();
- if(num1!=17)
- break;
- }
- }
- if(g==15)
- break;
- else
- continue;
- }
- switch(d) //+++++++++++++++++++++???????+++++++++++++++++++++++
- {case 0x0b: an=w2+w; break;
- case 0x0c: an=w2-w; break;
- case 0x0d: an=w2*w; break;
- case 0x0e: an=w2/w; break;
- default: an=0;break;
- }
- while(1)
- {
- if(an<10)
- {
- led3=0,led2=0,led1=0,LED=shu[an];
- lay(5);
- LED=0x00;
- led3=1,led2=1,led1=1,zhifu(g),LED=0x00; }
-
- else
- {
- if(an<100)
- {
- ans2=an/10;
- ans1=an%10;
- led3=0,led2=0,led1=0,LED=shu[ans1];
- lay(5);
- LED=0x00;
- led3=0,led2=0,led1=1,LED=shu[ans2];
- lay(5);//延時
- LED=0x00;
- led3=1,led2=1,led1=1,zhifu(g),LED=0x00;
- }
- else
- {
- ans3=an/100;
- ans2=(an%100 )/10;
- ans1=an%10;
- led3=0,led2=0,led1=0,LED=shu[ans1];
- lay(5);
- LED=0x00;
- led3=0,led2=0,led1=1,LED=shu[ans2];
- lay(5);
- LED=0x00;
- led3=0,led2=1,led1=0,LED=shu[ans3];
- lay(5);
- LED=0x00;
- led3=1,led2=1,led1=1,zhifu(g),LED=0x00;
- }
- }
- C=keyscan();
- if(C==16)
- break;
- }
- }
- //======================================================
- return 0;
- }
- //===================================
- void xuehao(void)
- {
- led3=0,led2=0,led1=0,LED=shu[1],LED=0x00;
- led3=0,led2=0,led1=1,LED=shu[2],LED=0x00;
- led3=0,led2=1,led1=0,LED=shu[0],LED=0x00;
- led3=0,led2=1,led1=1,LED=shu[3],LED=0x00;
- led3=1,led2=0,led1=0,LED=shu[2],LED=0x00;
- led3=1,led2=0,led1=1,LED=shu[3],LED=0x00;
- led3=1,led2=1,led1=0,LED=shu[0],LED=0x00;
- led3=1,led2=1,led1=1,LED=shu[3],LED=0x00;
- }
- //=====================================
- uchar keyscan(void)
- { uchar Keyvalue=17;
- KEY=0x0f;
- if(KEY!=0x0f)
- {
- delay10ms(1);
- if(KEY!=0x0f)
- {
- KEY=0x0F;
- switch(KEY)
- {
- case(0x07): Keyvalue=1;break;
- case(0x0b): Keyvalue=2;break;
- case(0x0d): Keyvalue=3;break;
- case(0x0e): Keyvalue=4;break;
- }
- //2a?DD
- KEY=0xf0;
- switch(KEY)
- {
- case(0x70): Keyvalue=Keyvalue; break;
- case(0xb0): Keyvalue=Keyvalue+4; break;
- case(0xd0): Keyvalue=Keyvalue+8; break;
- case(0xe0): Keyvalue=Keyvalue+12;break;
- }
- while(KEY!=0xf0);
-
- }
- yinyue( Keyvalue);
- }
- if(Keyvalue==10)
- {
- Keyvalue=Keyvalue-10;
- }
- return Keyvalue;
- }
- //....................................................................................
- //.....................................................................................
- void delay10ms(uchar c)
- {
- uchar a,b;
- for(;c>0;c--)
- for(b=38;b>0;b--)
- for(a=130;a>0;a--);
- }
- //............................................................................................
- void yinyue(uchar m)
- {
- uchar k;
- for(k=0;k<100;k++)
- {
- Beep=0;
- lay(m*2);
- Beep=1;
- lay(m*2);
- }
- }
- //=========================
- void Hc595Byte(unsigned char dat)
- { unsigned char a;
- SRCLK=0;
- RCLK=0;
- for(a=0;a<8;a++)
- {
- SER=dat>>7;
- dat<<=1;
- SRCLK=1;
- _nop_();
- _nop_();
- SRCLK=0;
- }
- RCLK=1;
- _nop_();
- _nop_();
- RCLK=0;
- }
- //**********************************************
- void zhifu(uchar q)
- {
- unsigned char tab,j;
- j=q-11;
- for(tab=0;tab<8;tab++)
- {
- Hc595Byte(0x00);
- LED= TAB[tab];
- Hc595Byte(CHARCODE[j][tab]);
- }
- }
- void lay(unsigned int i)
- {
- char j;
- for(i; i > 0; i--)
- for(j = 200; j > 0; j--);
- }
復(fù)制代碼
|
|