簡單計算器的設計方法_第1頁
簡單計算器的設計方法_第2頁
簡單計算器的設計方法_第3頁
簡單計算器的設計方法_第4頁
簡單計算器的設計方法_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、簡單計算器的設計#include <</font>reg51.h>#include <</font>intrins.h>#include <</font>ctype.h>            #include <</font>stdlib.h>             #define uchar unsigned char#define uint unsigned in

2、t uchar operand19, operand29;  uchar operator;                      void delay(uint);uchar keyscan();void disp(void);void buf(uint value);uint compute(uint va1,uint va2,uchar optor); uchar code table = 0xc0,0xf9,0xa4,0xb0,0x9

3、9,                0x92,0x82,0xf8,0x80,0x90,0xff;      uchar dbuf8 = 10,10,10,10,10,10,10,10;                     void delay(uint z)uint x,y;for(x=z;x>0;x-)    for(y=11

4、0;y>0;y-);  uchar keyscan()    uchar skey;                              P1 = 0xfe;    while(P1 & 0xf0) != 0xf0)                  

5、0; delay(3);                             while(P1 & 0xf0) != 0xf0)                        switch(P1)                 

6、;                           case 0xee: skey = '7' break;                case 0xde: skey = '8' break;                case 0xbe: skey = '

7、9' break;                case 0x7e: skey = '/' break;                                    default:   skey = '#'        &#

8、160;                while(P1 & 0xf0) != 0xf0)                 ;                  P1 = 0xfd;        while(P1 & 0xf0) != 0xf0)   

9、         delay(3);         while(P1 & 0xf0) != 0xf0)                    switch(P1)                            case 0xed: skey = 

10、9;4' break;                case 0xdd: skey = '5' break;                case 0xbd: skey = '6' break;                case 0x7d: skey = '*' break;  &#

11、160;                                 default:   skey = '#'                         while(P1 & 0xf0) != 0xf0)      

12、0;         ;                   P1 = 0xfb;    while(P1 & 0xf0) != 0xf0)            delay(3);                while(P1 & 0xf0) != 0xf0)

13、                    switch(P1)                            case 0xeb: skey = '1' break;                case 0xdb: skey = '2'

14、break;                case 0xbb: skey = '3' break;                case 0x7b: skey = '-' break;                             

15、      default: skey = '#'                         while(P1 & 0xf0) != 0xf0)                ;                   P1

16、 = 0xf7;    while(P1 & 0xf0) != 0xf0)            delay(3);         while(P1 & 0xf0) != 0xf0)                    switch(P1)               

17、             case 0xe7: skey = '$' break;                case 0xd7: skey = '0' break;                case 0xb7: skey = '=' break;          &#

18、160;     case 0x77: skey = '+' break;                                    default:   skey = '#'                     

19、0;   while(P1 & 0xf0) != 0xf0)                ;                 return skey; void main()        uint value1, value2, value;               uch

20、ar ckey, cut1 = 0, cut2 = 0;        uchar operator;                           uchar i, bool = 0; init:                             

21、0;          buf(0);                                disp();    value = 0;    cut1 = cut2 = 0;    bool = 0;    for(i = 0;i <</font> 9;i+) 

22、           operand1i = '0'        operand2i = '0'                                          while(1)        &#

23、160;   ckey = keyscan();                     if(ckey != '#')                     if(isdigit(ckey)                     

24、0;               switch(bool)                                        case 0:                         

25、;   operand1cut1 = ckey;                            operand1cut1+1 = '0'                            value1 = atoi(operand1);      &#

26、160;                       cut1+;                            buf(value1);                            disp

27、();                            break;                    case 1:                            operand2cut2 = cke

28、y;                            operand2cut2+1 = '0'                            value2 = atoi(operand2);              &

29、#160;             cut2+;                            buf(value2);                            disp();        

30、60;                   break;                                                default: break;          

31、0;                             else if(ckey='+'|ckey='-'|ckey='*'|ckey='/')                            bool = 1;    &#

32、160;               operator = ckey;                buf(0);                dbuf7 = 10;                disp();          &#

33、160;             else if(ckey = '=')                            value = compute(value1,value2,operator);                buf(value);     

34、          disp();                while(1)                                                      

35、   ckey = keyscan();                    if(ckey = '$')                                  goto init;               

36、;     else                                                    buf(value);                        

37、60;   disp();                                                                else if(ckey = '$')       

38、0;        goto init;                disp();       uint compute(uint va1,uint va2,uchar optor)    uint value;     switch(optor)            case '+' : val

39、ue = va1+va2;    break;        case '-' : value = va1-va2;    break;        case '*' : value = va1*va2;    break;        case '/' : value = va1/va2;    break;    

40、0;           default :  break;        return value;  void buf(uint val)    uchar i;    if(val = 0)            dbuf7 = 0;        i = 6;       

41、0;    else        for(i = 7; val > 0; i-)                    dbufi = val % 10;            val /= 10;                for( ; i > 0; i-) 

42、       dbufi = 10;  void disp(void)    uchar bsel, n;        bsel=0x01;    for(n=0;n<</font>8;n+)             P2=bsel;        P0=tabledbufn;       

43、; bsel=_crol_(bsel,1);        delay(3);        P0=0xff;    音樂播放器  /*  歌曲選擇時應按住設置間3秒以上,然后按上或下鍵進行選擇,然后再按設置鍵退出 */ #include<reg52.h> sbit spek=P37;  /聲音輸出端 sbit set=P32;  /設定及退出鍵

44、,按奇數(shù)次為設定功能;偶數(shù)次為退出功能 sbit up=P31;  /上一曲 sbit down=P30;  /下一曲 bit flag1,flag2;  /按鍵掃描標志位,新鍵值標志位 unsigned char f_t,n,i,key,k_max; /音符頻率對應的二分之一周期值,節(jié)拍常數(shù)值,查表用計數(shù)器,鍵值,最大鍵值 unsigned char *p_music;  /指向音樂數(shù)

45、據(jù)數(shù)組的指針 void timer_ms(unsigned char tim);  /毫秒級延時函數(shù)聲明 void timer_us(unsigned char t);  /微秒級延時函數(shù) void key_scan(void);  /按鍵掃描函數(shù) void key_manage();  /鍵值處理函數(shù)  /格式為:頻率常數(shù),節(jié)拍常數(shù),頻率常數(shù),節(jié)拍常數(shù) unsign

46、ed char code music_table1=0x18,0x30,0x1c,0x10,0x20,0x40,0x1c,0x10,0x18, 0x10,0x20,0x10,0x1c,0x10,0x18,0x40,0x1c,0x20,0x20,0x20,0x1c,0x20,0x18,0x20,0x20,0x80, 0xff,0x20,0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20,0x20,0x20,0x26,0x40,0x20,0x20, 0x2b,0x20,0x26,0x20,0x20,0

47、x20,0x30,0x80,0xff,0x20,0x20,0x1c,0x10,0x18,0x10,0x20,0x20, 0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,0x20,0x1c,0x10,0x18,0x10,0x20,0x20,0x26, 0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20, 0x20,0x20,0x26,0x40,0x20,0x20,0x2b,0x20,0x26,0x20,0x2

48、0,0x20,0x30,0x80,0x20,0x30,0x1c, 0x10,0x20,0x10,0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20,0x15, 0x1f,0x05,0x20,0x10,0x1c,0x10,0x20,0x20,0x26,0x20,0x2b,0x20,0x30,0x20,0x2b,0x40,0x20, 0x30,0x1c,0x10,0x18,0x20,0x15,0x20,0x1c,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2b,

49、0x20, 0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1c,0x10,0x18,0x40,0x1c,0x20,0x20,0x20,0x26, 0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00; /八月桂花香 unsigned char code music_table2=0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80, 0x26,0x

50、20,0x30,0x20,0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,0x30,0x20,0x30,0x20, 0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,0x30,0x20,0x30,0x20,0x39,0x10,0x26,

51、0x10, 0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20, 0x2b,0x40,0x40,0x20,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x

52、20,0x20,0x80,0x1c,0x20, 0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10, 0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10, 0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80

53、,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,0x20,0x10,0x2b,0x10, 0x26,0x30,0x30,0x80,0x00;  /祝你平安 unsigned char code music_table3=0x20,40,0x26,20,0x20,20,0x17,80,0x1c,40,0x17,40,0x20

54、,80,0x20, 40,0x2f,20,0x2a,20,0x26,40,0x2a,20,0x2f,20,0x2a,80,0xff,0x20,40,0x26,20,0x20,20,0x17,60,0x19,20, 0x1c,40,0x17,40,0x20,80,0x20,40,0x2a,20,0x26,20,0x24,60,0x32,20,0x2f,80,0xff,0x1c,40,0x17,40,0x17, 80,0x19,40,0x1c,20,0x19,20,0x17,80,0x1c,20,0x19,20,0x17,20,0x1c,20,0x1c,20,0x20

55、,20,0x26,20,0x2f,20, 0x2a,80,0xff,0x20,40,0x26,20,0x20,20,0x17,60,0x19,20,0x1c,40,0x17,40,0x20,80,0x20,40,0x2a,20,0x26, 20,0x24,60,0x32,20,0x2f,80,0xff,0x20,40,0x26,20,0x20,20,0x17,80,0x1c,40,0x17,40,0x20,80,0x20,40, 0x2f,20,0x2a,20,0x26,40,0x2a,20,0x2f,20,0x2a,80,0xff,0x20,40,0x26,20

56、,0x20,20,0x17,60,0x19,20,0x1c, 40,0x17,40,0x20,80,0x20,40,0x2a,20,0x26,20,0x24,60,0x32,20,0x2f,80,0;  /送別  main()      spek=0;     P3=0x7f;     TMOD=0x01;  /T0工作于方式1,即16位定時器模式 

57、0;   IE=0x82;  /開總中斷,T0中斷     flag2=0;  /     flag1=0;     key=1;       k_max=3;  /最大鍵值(取值范圍1254)     n=0;  &#

58、160;  i=0;  p_music=music_table1;  /指針賦初值     while (1)              switch (p_musici)             &

59、#160;      case 0x00: i=0;  /歌曲完,則停止0.5S后重新開始             timer_ms(250);  /延時0.25ms             timer_ms(250);

60、             break;         case 0xff: i+;  /遇到休止符則停止100us             timer_ms(250);   &#

61、160;timer_ms(250);             break;         default:   f_t=p_musici+;             n=p_musici+;  &#

62、160;          spek=1;             TR0=1;             while (n!=0)        &#

63、160;                     spek=spek;                 timer_us(4*f_t);  /改變f_t前面的系數(shù)可以調整音符頻率的高低 &#

64、160;                     TR0=0;             spek=0;    if(set=0)       

65、0; key_scan();     key_manage();                     /* 函數(shù)名稱: 定時器0的中斷處理程序 函數(shù)功能: 完成10毫秒定時 入口參數(shù):  出口參數(shù):  其  

66、0;   它:  */ void timer0() interrupt 1 using 2      TH0=(65536-10000)/256;  /定時10毫秒     TL0=(65536-10000)%256;     n-;   /* 函數(shù)名稱: 延

67、時程序 函數(shù)功能: 完成微秒級的延時 入口參數(shù): t為需延時的時間(取值范圍1255;延時時間51275us) 出口參數(shù):  其      它:  */ void timer_us(unsigned char t)      while (-t);  /本語句執(zhí)行一次需5us的時間   /

68、* 函數(shù)名稱: 按鍵掃描函數(shù) 函數(shù)功能: 完成按鍵掃描處理 入口參數(shù): 應為在中斷函數(shù)中被調用,所以不能有參數(shù)傳遞 出口參數(shù):  其      它:  */ void key_scan(void)      timer_ms(10);  /延時10ms等待按鍵狀態(tài)穩(wěn)定     i

69、f (set=0)  /再次確認                 while (set=0);  /等待按鍵松開         flag1=1;  /該位為1,表示進入按鍵掃描處理;為0,表示本次按鍵掃描處理完成    

70、;     while (flag1)                         if (up=0)  /加一鍵            

71、;                  timer_ms(10);                 if (up=0)          &#

72、160;                           while (!up);                   

73、60; key+;                     if (key>k_max) key=1;                     

74、                      if (down=0)  /減一鍵                      

75、0;       timer_ms(10);                 if (down=0)                     

76、;                 while (!down);                     key-;        

77、;             if (key<1) key=k_max;                               &

78、#160;  if (set=0)  /退出鍵                              timer_ms(10);          

79、;       if (set=0)                                      while (se

80、t=0);                     flag1=0;  /表示本次按鍵掃描處理完成                     flag2=1;&#

81、160; /該位置1,表示有新鍵值要處理                              /以上鍵值處理的范圍為1到254個鍵值            

82、0;    /* 函數(shù)名稱: 鍵值處理函數(shù) 函數(shù)功能: 完成按鍵鍵值的處理 入口參數(shù): 應為在中斷函數(shù)中被調用,所以不能有參數(shù)傳遞 出口參數(shù):  其      它:  */void key_manage()      flag2=0;     switch (

83、key)          case 1: p_music=music_table1;  /鍵值為1 播放第一首樂曲             goto kh1;     case 2: p_music=&music_table20;

84、  /鍵值為2 播放第2首樂曲             goto kh1;  case 3: p_music=&music_table30;  /鍵值為3 播放第3首樂曲             goto kh1

85、;       default:  key=1;              p_music=music_table1; kh1: TH0=(65536-10000)/256;      TL0=(65536-10000)%256;      break;       n=0;     i=0; &#

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論