51单片机实现万年历程序_第1页
51单片机实现万年历程序_第2页
51单片机实现万年历程序_第3页
51单片机实现万年历程序_第4页
51单片机实现万年历程序_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、51单片机实现万年历 利用at89s52单片机的p0口来和另外几个口来控制1602液晶的显示和p1口还有其它口来控制ds12887时钟芯片。设置四个按键,1个定义为时间设置功能键,一个定义为闹钟设置功能键,另外两个用来调节时间的增减。 原理图: pcb图: 源程序: #include<reg52.h> #define uint unsigned int #define uchar unsigned char sbit beep=p20;/蜂鸣器控制口 sbit timeadd=p21;/调节时间加 sbit timesub=p22;/调节时间减 sbit timeclk=p23;/

2、闹钟设定键 sbit timefun=p24;/时间设定键 sbit lcdrs=p25;/液晶的命令和数据控制口 sbit ledrw=p26;/液晶的写数据口 sbit lcden=p27;/液晶显示模块的使能端 sbit timeds=p33;/时钟芯片地址闸 sbit timerw=p34;/时钟芯片读写 sbit timeas=p35;/时钟芯片地址闸 sbit timeen=p36;/时钟芯片片选 uchar code table="20  -  -   week " uchar code table1

3、="     :  :  "/要显示的字符串 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)    lcdr

5、s=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(0x30

6、+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、60;  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;    tim

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

9、;    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(tim

11、eadd=0)                                          while(!timeadd=0);      &#

12、160;                flag1=0;                                  

13、60;       if(timesub=0)                              delay(5);            &

14、#160;    if(timesub=0)                                          while(!timesub=0

15、);                       flag1=0;                            

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

17、60;                    while(!timefun);                 flag=1;           &

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

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

20、                              if(numfun=2)                    

21、60;                     write_com(0x80+0x40+0x07);                       write_com(0x

22、0f);                                    if(numfun=3)              

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

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

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

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

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

28、#160;             write_com(0x0f);                                   &

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

30、#160;                  write_com(0x0f);                              &

31、#160;     if(numfun=7)                                          write_com(0

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

33、#160;          if(numfun=8)                                       

34、;   numfun=0;                       flag=0;                       

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

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

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

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

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

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

41、#160;   if(numfun=2)                                          fen+;    

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

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

44、0;                   write_ds(2,fen);                             

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

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

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

48、#160;                     write_ds(4,shi);                           &

49、#160;        if(numfun=4)                                         

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

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

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

53、0;            if(numfun=5)                                     &#

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

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

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

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

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

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

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

61、#160;  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、60;                                    else if(leap&&yue=2)          

65、60;                                           ri+;       

66、60;                     if(ri=30)                            

67、;    ri=1;                                               

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

69、#160;       ri+;                             if(ri=29)            

70、0;                   ri=1;                                &#

71、160;               else                       switch(yue)          

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

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

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

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

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

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

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

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

80、                    write_ds(7,ri);                             &

81、#160;      if(numfun=6)                                          yue+;

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

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

84、                      write_ds(8,yue);                           

85、         if(numfun=7)                                        

86、0; nian+;                       if(nian=100)                        &#

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

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

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

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

91、           if(numfun=1)                                      

92、0;   miao-;                       if(miao=-1)                      

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

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

95、60;              if(numfun=2)                                   &

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

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

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

99、0;               if(numfun=3)                                  &#

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

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

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

103、#160;                  if(numfun=4)                               

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

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

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

107、0;                      if(numfun=5)                            &

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

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

110、60;                           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、60;           case 6:ri-;if(ri=0) ri=30;break;                             case 7:ri-;if(ri=0) ri=31;brea

114、k;                             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、#160;            case 12:ri-;if(ri=0) ri=31;break;                                 &#

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

119、160;                                                  &

120、#160; ri-;                             if(ri=0)                   

121、;             ri=29;                                      &#

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

123、                 ri-;                             if(ri=0)   

124、0;                            ri=28;                      &#

125、160;                         else                       switch(yue) &#

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

127、0;    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、60;                             write_nyr(8,ri);                   

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

135、                if(numfun=6)                                 

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

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

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

139、;                   if(numfun=7)                               &#

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

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

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

143、#160;           write_ds(9,nian);                                      

144、;  void set_alarm() uchar ashi,afen,amiao; if(timeclk=0)/判断是否按下闹钟设定键          delay(5);/延时消抖       if(timeclk=0)                    

145、60; while(!timeclk);             anumfun+;/按键次数加1             if(anumfun=1)                  &#

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

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

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

149、40+0x07);                    write_com(0x0f);                             &

150、#160;if(anumfun=3)                                     flag=1;            &

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

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

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

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

155、0;        if(anumfun=1) /设定秒                                     amiao+;    

156、;                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、60;                      afen+;                    if(afen=60)         

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

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

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

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

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

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

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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论