單片機實現(xiàn)萬年歷程序_第1頁
單片機實現(xiàn)萬年歷程序_第2頁
單片機實現(xiàn)萬年歷程序_第3頁
單片機實現(xiàn)萬年歷程序_第4頁
單片機實現(xiàn)萬年歷程序_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、.51單片機實現(xiàn)萬年歷 利用AT89S52單片機的P0口來和另外幾個口來控制1602液晶的顯示和P1口還有其它口來控制ds12887時鐘芯片。設置四個按鍵,1個定義為時間設置功能鍵,一個定義為鬧鐘設置功能鍵,另外兩個用來調節(jié)時間的增減。 原理圖: pcb圖: 源程序: #include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit beep=P20;/蜂鳴器控制口 sbit timeadd=P21;/調節(jié)時間加 sbit timesub=P22;/調節(jié)時間減 sbit timeclk=P23;

2、/鬧鐘設定鍵 sbit timefun=P24;/時間設定鍵 sbit lcdrs=P25;/液晶的命令和數(shù)據(jù)控制口 sbit ledrw=P26;/液晶的寫數(shù)據(jù)口 sbit lcden=P27;/液晶顯示模塊的使能端 sbit timeds=P33;/時鐘芯片地址閘 sbit timerw=P34;/時鐘芯片讀寫 sbit timeas=P35;/時鐘芯片地址閘 sbit timeen=P36;/時鐘芯片片選 uchar code table="20  -  -   week " uchar code table

3、1="     :  :  "/要顯示的字符串 char num,shi,fen,miao,nian=2000,yue,ri,week,numfun,anumfun,flag,flag1; uchar leap; void delay(uint x)    uchar i,j;    for(i=x;i>0;i-)       for(j=110;j>0;j-); void didi(

4、)    beep=0;    delay(1000);    beep=1;    delay(1000); void write_com(uchar com)    lcdrs=0;    P0=com;    delay(5);    lcden=1;    delay(20);    lcden=0; void write_date(uchar date)    lcd

5、rs=1;    P0=date;    delay(5);    lcden=1;    delay(20);    lcden=0; void write_sfm(uchar add,uchar date)    uchar shi,ge;    shi=date/10;    ge=date%10;    write_com(0x80+0x40+add);    write_date(0x3

6、0+shi);    write_date(0x30+ge); void write_nyr(uchar add,uchar date)    uchar shi,ge;    shi=date/10;    ge=date%10;    write_com(0x80+add);    write_date(0x30+shi);    write_date(0x30+ge); void write_zhou(uchar add,uchar date) &#

7、160;  write_com(0x80+add);    write_date(0x30+date); void write_ds(uchar add,uchar date)    timeen=0;    timeas=1;    timeds=1;    timerw=1;    P1=add;    timeas=0;    timerw=0;    P1=date;    ti

8、merw=1;    timeas=1;    timeen=1;           uchar read_ds(uchar add)    uchar ds_date;    timeas=1;    timeds=1;    timerw=1;    timeen=0;    P1=add;    timeas=

9、0;    timeds=0;    P1=0xff;    ds_date=P1;    timeds=1;    timeas=1;    timeen=1;    return ds_date; void keyscan()    if(flag1=1)/用來關閉鬧鐘鈴聲               &

10、#160; if(timeadd=0)                              delay(5);                 if(ti

11、meadd=0)                                          while(!timeadd=0);      &

12、#160;                flag1=0;                                  &#

13、160;       if(timesub=0)                              delay(5);            

14、     if(timesub=0)                                          while(!timesub=

15、0);                       flag1=0;                           

16、0;               if(timefun=0)                 delay(5);          if(timefun=0)         &#

17、160;                    while(!timefun);                 flag=1;           

18、      numfun+;                 if(numfun=1)                          &

19、#160;               write_com(0x80+0x40+0x0a);                       write_com(0x0f);      

20、;                              if(numfun=2)                    &#

21、160;                     write_com(0x80+0x40+0x07);                       write_com(0

22、x0f);                                    if(numfun=3)             

23、0;                            write_com(0x80+0x40+0x04);                   

24、    write_com(0x0f);                                    if(numfun=4)        

25、;                                  write_com(0x80+0x0f);              

26、         write_com(0x0f);                                    if(numfun=5)   

27、;                                       write_com(0x80+0x09);         

28、              write_com(0x0f);                                   

29、 if(numfun=6)                                          write_com(0x80+0x06);    

30、                   write_com(0x0f);                              

31、      if(numfun=7)                                          write_com(

32、0x80+0x03);                       write_com(0x0f);                         

33、           if(numfun=8)                                      

34、0;   numfun=0;                       flag=0;                       

35、60;                   if(numfun!=0)                 if(timeadd=0)                

36、;              delay(5);                 while(!timeadd);                &#

37、160;if(numfun=1)                                          miao+;       

38、;                if(miao=60)                          miao=0;       &#

39、160;               write_sfm(9,miao);                       write_com(0x80+0x40+9);      &

40、#160;                write_ds(0,miao);                                

41、    if(numfun=2)                                          fen+;   

42、0;                   if(fen=60)                          fen=0;    

43、60;                  write_sfm(6,fen);                       write_com(0x80+0x40+6);   

44、60;                   write_ds(2,fen);                             

45、60;      if(numfun=3)                                          shi+; &

46、#160;                     if(shi=24)                          shi=0;  

47、                     write_sfm(3,shi);                       write_com(0x80+0x40+3);

48、                      write_ds(4,shi);                           

49、         if(numfun=4)                                        

50、0; week+;                       if(week=8)                        

51、0; week=1;                       write_zhou(0x0f,week);                       wr

52、ite_com(0x80+0x0f);                       write_ds(6,week);                       

53、60;            if(numfun=5)                                     &

54、#160;    if(nian=0)                                             &

55、#160;     switch(yue)                             case 1:ri+;if(ri=32) ri=1;break;          

56、60;                  case 2:ri+;if(ri=30) ri=1;break;                           

57、  case 3:ri+;if(ri=32) ri=1;break;                             case 4:ri+;if(ri=31) ri=1;break;          

58、                   case 5:ri+;if(ri=32) ri=1;break;                          

59、60;  case 6:ri+;if(ri=31) ri=1;break;                             case 7:ri+;if(ri=32) ri=1;break;         

60、60;                   case 8:ri+;if(ri=32) ri=1;break;                          

61、   case 9:ri+;if(ri=31) ri=1;break;                             case 10:ri+;if(ri=32) ri=1;break;         

62、;                    case 11:ri+;if(ri=31) ri=1;break;                         &

63、#160;   case 12:ri+;if(ri=32) ri=1;break;                                          &#

64、160;                                    else if(leap&&yue=2)          &#

65、160;                                           ri+;       &#

66、160;                     if(ri=30)                           

67、0;    ri=1;                                               &#

68、160;else if(yue=2&&leap=0)                                              

69、        ri+;                             if(ri=29)            

70、60;                   ri=1;                                &

71、#160;               else                       switch(yue)          &#

72、160;               case 1:ri+;if(ri=32) ri=1;break;                          case 3:ri+;if(ri=32) ri=1;break;

73、                         case 4:ri+;if(ri=31) ri=1;break;                     &#

74、160;    case 5:ri+;if(ri=32) ri=1;break;                          case 6:ri+;if(ri=31) ri=1;break;            

75、              case 7:ri+;if(ri=32) ri=1;break;                          case 8:ri+;if(ri=32) ri=1;break;  

76、0;                       case 9:ri+;if(ri=31) ri=1;break;                       

77、   case 10:ri+;if(ri=32) ri=1;break;                          case 11:ri+;if(ri=31) ri=1;break;             &#

78、160;            case 12:ri+;if(ri=32) ri=1;break;                                  &#

79、160;                   write_nyr(8,ri);                       write_com(0x80+0x08);   

80、;                    write_ds(7,ri);                             

81、       if(numfun=6)                                          yue+

82、;                       if(yue=13)                          yue=0; 

83、60;                     write_nyr(5,yue);                       write_com(0x80+0x05);

84、                       write_ds(8,yue);                           

85、;         if(numfun=7)                                        

86、60; nian+;                       if(nian=100)                        &

87、#160; nian=0;                       leap=nian%4=0&&nian%100!=0;                    

88、0;  write_nyr(2,nian);                       write_com(0x80+2);                    &#

89、160;  write_ds(9,nian);                                          if(timesub=0)    

90、60;                         delay(5);                 while(!timesub);      

91、;           if(numfun=1)                                      

92、60;   miao-;                       if(miao=-1)                      &#

93、160;   miao=59;                       write_sfm(9,miao);                     

94、60; write_com(0x80+0x40+9);                       write_ds(0,miao);                     &#

95、160;              if(numfun=2)                                   

96、       fen-;                       if(fen=-1)                   &

97、#160;      fen=59;                       write_sfm(6,fen);                  

98、0;    write_com(0x80+0x40+6);                      write_ds(2,fen);                    

99、60;               if(numfun=3)                                  &

100、#160;       shi-;                       if(shi=-1)                  &#

101、160;       shi=23;                       write_sfm(3,shi);                  

102、;     write_com(0x80+0x40+3);                       write_ds(4,shi);                 

103、                   if(numfun=4)                               

104、60;          week-;                       if(week=0)               

105、60;          week=7;                       write_zhou(0x0f,week);              &

106、#160;        write_com(0x80+0x0f);                       write_ds(6,week);             

107、60;                      if(numfun=5)                            

108、              if(nian=0)                                    

109、               switch(yue)                             case 1:ri-;if(ri=0) ri=31;break; &#

110、160;                           case 2:ri-;if(ri=0) ri=29;break;                  

111、;           case 3:ri-;if(ri=0) ri=31;break;                             case 4:ri-;if(ri=0) ri=30;break;

112、                             case 5:ri-;if(ri=0) ri=31;break;                 &#

113、160;           case 6:ri-;if(ri=0) ri=30;break;                             case 7:ri-;if(ri=0) ri=31;bre

114、ak;                             case 8:ri-;if(ri=0) ri=31;break;                 

115、;            case 9:ri-;if(ri=0) ri=30;break;                             case 10:ri-;if(ri=0) ri=31

116、;break;                             case 11:ri-;if(ri=0) ri=30;break;                

117、             case 12:ri-;if(ri=0) ri=31;break;                                 &

118、#160;                                             else if(leap&&yue=2) &

119、#160;                                                  

120、  ri-;                             if(ri=0)                  

121、0;             ri=29;                                      &

122、#160;         else if(yue=2&&leap=0)                                     

123、;                 ri-;                             if(ri=0)   

124、60;                            ri=28;                      &

125、#160;                         else                       switch(yue) &

126、#160;                        case 1:ri-;if(ri=0) ri=31;break;                     

127、60;    case 3:ri-;if(ri=0) ri=31;break;                          case 4:ri-;if(ri=0) ri=30;break;            &

128、#160;             case 5:ri-;if(ri=0) ri=31;break;                          case 6:ri-;if(ri=0) ri=30;break;   

129、;                       case 7:ri-;if(ri=0) ri=31;break;                       &

130、#160;  case 8:ri-;if(ri=0) ri=31;break;                          case 9:ri-;if(ri=0) ri=30;break;              

131、;            case 10:ri-;if(ri=0) ri=31;break;                          case 11:ri-;if(ri=0) ri=30;break;    &

132、#160;                     case 12:ri-;if(ri=0) ri=31;break;                        &#

133、160;                             write_nyr(8,ri);                   &#

134、160;   write_com(0x80+0x08);                       write_ds(7,ri);                   

135、                 if(numfun=6)                                 

136、60;        yue-;                       if(yue=-1)                 

137、0;        yue=12;                       write_nyr(5,yue);                 &

138、#160;     write_com(0x80+0x05);                       write_ds(8,yue);                

139、0;                   if(numfun=7)                               &

140、#160;          nian-;                       if(nian=-1)               

141、           nian=99;                       leap=nian%4=0&&nian%100!=0;           &#

142、160;           write_nyr(2,nian);                       write_com(0x80+2);           

143、            write_ds(9,nian);                                     

144、0;  void set_alarm() uchar ashi,afen,amiao; if(timeclk=0)/判斷是否按下鬧鐘設定鍵          delay(5);/延時消抖       if(timeclk=0)                    &#

145、160; while(!timeclk);             anumfun+;/按鍵次數(shù)加1             if(anumfun=1)                  &

146、#160;                  flag=1;                    write_com(0x80+0x40+0x0a);         

147、           write_com(0x0f);                              if(anumfun=2)       

148、60;                             flag=1;                    write_com(0x80+0

149、x40+0x07);                    write_com(0x0f);                             

150、 if(anumfun=3)                                     flag=1;            

151、        write_com(0x80+0x40+0x04);                    write_com(0x0f);                   &

152、#160;          if(anumfun=4)                                     flag=0;  &

153、#160;                 anumfun=0;                              if(anumfun!=0)   

154、      if(timeadd=0)/如果加功能鍵按下                      delay(5);             while(!timeadd);    

155、60;        if(anumfun=1) /設定秒                                     amiao+;   

156、0;                if(amiao=60)                       amiao=0;          

157、;          write_sfm(9,amiao);                    write_com(0x80+0x40+9);                &

158、#160;   write_ds(1,amiao);                              if(anumfun=2)              &#

159、160;                      afen+;                    if(afen=60)        

160、0;              afen=0;                    write_sfm(6,afen);              

161、      write_com(0x80+0x40+6);                    write_ds(3,afen);                     

162、60;        if(anumfun=3)                                     ashi+;    

163、0;               if(ashi=24)                       ashi=0;           &

164、#160;        write_sfm(3,ashi);                    write_com(0x80+0x40+3);                 

165、0;  write_ds(5,ashi);                              if(anumfun!=0)          if(timesub=0)       

166、               delay(5);             while(!timesub);             if(anumfun=1)                                     amiao-;         &

溫馨提示

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

評論

0/150

提交評論