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

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

        51單片機帶閏年補償的一個時鐘程序

        作者:我行天下   來源:本站原創   點擊數:  更新時間:2014年03月31日   【字體:

        這是我教的學生寫的帶閏年補償的一個時鐘,比我自己寫成的還有成就感


        #include <reg51.h>
        #include<delay.h>
        #define uchar unsigned char
        #define uint unsigned int
        uchar S1num;
        sbit RS=P1^0;
        sbit RW=P1^1;
        sbit EN=P1^2;
        sbit GND=P2^4;
        sbit K2=P2^3;
        sbit K4=P2^2;
        sbit K6=P2^1;
        uchar x,num,shi,fen,miao,day,mon,year,week,count;
        void display();
        void write_com(uchar com)
         {
          RS=0;
          EN=0;
          P0=com;
          delay(5);
          //RW=0;
          EN=1;
          delay(5);
           EN=0;
         
         }
        void write_dat(uchar dat)
         {
          RS=1;
          EN=0;
          P0=dat;
          delay(5);
           //RW=0;
          EN=1;
          delay(5);
          EN=0;
         
         }
        void timer0()interrupt 1
         {
           TH0=(65536-45872)/256;//45872這個值是11.0592MHz產生一次中斷,最接近50Ms,如果是12MHz請用50000
           TL0=(65536-45872)%256;
           count++;
         }
        write_sfm(uchar add,uchar date)
         {
           uchar shi,ge;
           write_com(0x80+0x40+add);
           shi=date/10;
           ge=date%10;
           write_dat(0x30+shi);
           write_dat(0x30+ge);
         }
        write_ymd(uchar add,uchar date)
         {
           uchar shi,ge;
           write_com(0x80+add);
           shi=date/10;
           ge=date%10;
           write_dat(0x30+shi);
           write_dat(0x30+ge);
         }
        write_week(week)
         {
           switch(week)
           {
             case 1:write_dat('M');
                 write_dat('O');
              write_dat('N');
              break;
             case 2:write_dat('T');
                 write_dat('U');
              write_dat('E');
              break;
             case 3:write_dat('W');
                 write_dat('E');
              write_dat('N');
              break;
             case 4:write_dat('T');
                 write_dat('H');
              write_dat('U');
              break;
          case 5:write_dat('F');
                 write_dat('R');
              write_dat('I');
              break;
             case 6:write_dat('S');
                 write_dat('A');
              write_dat('T');
              break;
          case 7:write_dat('S');
                 write_dat('U');
              write_dat('N');
              break;
           }
         }
        void init()
         {
          EN=0;
          RW=0;
          write_com(0x38);//顯示模式
          delay(5);
          write_com(0x0c);//開顯示
          write_com(0x06);//光標自加
          write_com(0x80);//指針左一
          write_com(0x01);//清屏
         
          TMOD=0x01;//定時器0的工作方式1
          TH0=(65536-50000)/256;
          TL0=(65536-50000)%256;
         
         EA=1;//開中斷
         ET0=1;
         TR0=1;//開定時器
         display();
         }
        void display()
         {
           write_sfm(10,0);
           write_com(0x80+0x40+11);
           write_com(0x80+0x40+9);
           write_dat(':');
           write_sfm(7,0);
           write_com(0x80+0x40+8);
           write_com(0x80+0x40+6);
           write_dat(':');
           write_sfm(4,0);
           write_com(0x80+0x40+5);
           write_ymd(10,29);
           write_com(0x80+11);
           write_com(0x80+9);
           write_dat('-');
           write_ymd(7,3);
           write_com(0x80+8);
           write_com(0x80+6);
           write_dat('-');
           write_com(0x80+2);
           write_dat('2');
           write_dat('0');
           write_ymd(4,12);
           write_com(0x80+5);
           write_com(0x80+13);
           write_week(4);
         
         }
        void keyscan()
        {
          if(K2==0)
          {
            delay(5);
             if(K2==0);//防止誤動作
          {
             S1num++;
             while(!K2);
             switch(S1num)
             {
              case 1:
                TR0=0;
             write_com(0x80+0x40+11);
             write_com(0x0f);
             break;
              case 2:
               write_com(0x80+0x40+8);
               break;
              case 3:
               write_com(0x80+0x40+5);
               break;
              case 4:
               write_com(0x80+11);
               break;
              case 5:
               write_com(0x80+8);
               break;
              case 6:
               write_com(0x80+5);
               break;
              case 7:
               write_com(0x80+13);
               break;
              case 8:
               S1num=0;
               TR0=1;
            write_com(0x0c);
               break;
           }
          }
          }
          if(S1num!=0)
          {
            if(K4==0)
            {
             delay(5);
          while(!K4);
          switch(S1num)
          {
           case 1:
            miao++;
            if(miao==60)miao=0;
            write_sfm(10,miao);
            write_com(0x80+0x40+11);
            break;
           case 2:
            fen++;
            if(fen==60)fen=0;
            write_sfm(7,fen);
            write_com(0x80+0x40+8);
            break;
           case 3:
            shi++;
            if(shi==24)shi=0;
            write_sfm(4,shi);
            write_com(0x80+0x40+5);
            break;
           case 4:
            day++;
            switch(mon)
             {
               case 1:if(day==32)day=1;break;
            case 3:if(day==32)day=1;break;
            case 5:if(day==32)day=1;break;
            case 7:if(day==32)day=1;break;
            case 8:if(day==32)day=1;break;
            case 10:if(day==32)day=1;break;
            case 12:if(day==32)day=1;break;
            case 4:if(day==31)day=1;break;
            case 6:if(day==31)day=1;break;
            case 9:if(day==31)day=1;break;
            case 11:if(day==31)day=1;break;
            case 2:x=20*100+year;
                   if((x%4==0 && x%100!=0) || x%400==0)
                   {
                  if(day==30)
               {
                 day=1;break;
               }
             
                }
               else if(day==29)
               {
                 day=1;break;
               }
           }
            write_ymd(10,day);
            write_com(0x80+11);
            break;
             case 5:
            mon++;
            if(mon==13)mon=1;
            write_ymd(7,mon);
            write_com(0x80+8);
            break;
             case 6:
            year++;
            write_ymd(4,year);
            write_com(0x80+5);
            break;
             case 7:
            week++;
            if(week==8)week=1;
            write_week(week);
            write_com(0x80+13);
            break;
             }
            }
            if(K6==0)
            {
             delay(5);
          while(!K6);
          switch(S1num)
          {
           case 1:
            miao--;
            if(miao==-1)miao=59;
            write_sfm(10,miao);
            write_com(0x80+0x40+11);
            break;
           case 2:
            fen--;
            if(fen==-1)fen=59;
            write_sfm(7,fen);
            write_com(0x80+0x40+8);
            break;
           case 3:
            shi--;
            if(shi==-1)shi=23;
            write_sfm(4,shi);
            write_com(0x80+0x40+5);
            break;
           case 4:
            day--;
            switch(mon)
             {
               case 1:if(day==0)day=31;break;
            case 3:if(day==0)day=31;break;
            case 5:if(day==0)day=31;break;
            case 7:if(day==0)day=31;break;
            case 8:if(day==0)day=31;break;
            case 10:if(day==0)day=31;break;
            case 12:if(day==0)day=31;break;
            case 4:if(day==0)day=30;break;
            case 6:if(day==0)day=30;break;
            case 9:if(day==0)day=30;break;
            case 11:if(day==0)day=30;break;
            case 2:x=20*100+year;
                   if((x%4==0 && x%100!=0) || x%400==0)
                   {
                  if(day==0)
               {
                 day=29;break;
               }
             
                }
               else if(day==0)
               {
                 day=28;break;
               }
             }
            write_ymd(10,day);
            write_com(0x80+11);
            break;
             case 5:
            mon--;
            if(mon==0)mon=12;
            write_ymd(7,mon);
            write_com(0x80+8);
            break;
             case 6:
            year--;
            write_ymd(4,year);
            write_com(0x80+5);
            break;
             case 7:
            week--;
            if(week==-1)week=7;
            write_week(week);
            write_com(0x80+13);
            break;
          }
            }
          }
        }
        void main()
         {
          init();
          GND=0;
         
           while(1)
           {
             keyscan();
             if(count==20)
             {
             count=0;
             miao++;
              if(miao==60)
              {
            miao=0;
            fen++;
             if(fen==60)
             {
              fen=0;
              shi++;
               if(shi==24)
               {
                shi=0;
                day++;
                week++;
                 switch(mon)
                 {
                   case 1:if(day==32)day=1;mon++;break;
                case 3:if(day==32)day=1;mon++;break;
                case 5:if(day==32)day=1;mon++;break;
                case 7:if(day==32)day=1;mon++;break;
                case 8:if(day==32)day=1;mon++;break;
                case 10:if(day==32)day=1;mon++;break;
                case 12:if(day==32)day=1;mon++;break;
                case 4:if(day==31)day=1;mon++;break;
                case 6:if(day==31)day=1;mon++;break;
                case 9:if(day==31)day=1;mon++;break;
                case 11:if(day==31)day=1;mon++;break;
                case 2:x=20*100+year;
                       if((x%4==0 && x%100!=0) || x%400==0)
                       {
                      if(day==30)
                   {
                     day=1;mon++;break;
                   }
                 
                    }
                   else if(day==29)
                   {
                     day=1;mon++;break;
                   }
                 }
                 if(mon==13)
                 {
                  mon=1;
                  year++;
                  write_ymd(4,year);
                 }
                 write_ymd(10,day);
                 write_com(0x80+13);
                 write_week(week);
                 write_ymd(7,mon);
               }
               write_sfm(4,shi);
             }
             write_sfm(7,fen);
              }
              write_sfm(10,miao);
             }
           }
         
         }
         

        關閉窗口

        相關文章

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