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

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

        DS1302電子時鐘1602液晶顯示

        作者:佚名   來源:本站原創   點擊數:  更新時間:2014年07月29日   【字體:
        #include<reg52.h>
        #include<intrins.h>
        #define uchar unsigned char
        #define uint unsigned int
        sbit DS1302_CLK=P1^2;
        sbit DS1302_IO=P1^1;
        sbit DS1302_RST=P1^0;
        sbit lcden=P2^2;
        sbit lcdwr=P2^1;
        sbit lcdrs=P2^0;
        unsigned char second,minute,hour,week,day,month,year;
        unsigned char table[]="0123456789";
        //unsigned char table1[]="          ";
        unsigned char table2[]="                 ";
        unsigned char table3[]="Time: ";
        unsigned char table4[]="Date: ";
        unsigned int time[]={
        0x09,0x12,0x27,0x03,0x23,0x59,0x40
        };
        uchar num,count,date,shi;
        void delay(uint z)
        {
          uint x,y;
          for(x=z;x>0;x--)
            for(y=110;y>0;y--);
        }
        //向DS1302送一個字節//
        void InputByte(unsigned char byte1)
        {
         char i;
         for(i=8;i>0;i--)
           {
             DS1302_IO=(bit)(byte1&0x01);
             DS1302_CLK=1;
             _nop_();
             DS1302_CLK=0;
             byte1>>=1;
           }
            return;
        }
        //讀DS1302一個字節//
        unsigned char outputbyte(void)
        {
           unsigned char i;
           unsigned char ucdat=0;
           for(i=8;i>0;i--)
            {
             DS1302_IO=1;
             ucdat>>=1;
             if(DS1302_IO)ucdat|=0x80;
             DS1302_CLK=1;
             _nop_();
             DS1302_CLK=0;
            }     
            return(ucdat);
        
        }
        //向DS1302某地址寫一個字節數據//
        void write_ds1302(unsigned char addr,unsigned char TDat)
        {
         DS1302_RST=0;
         _nop_();
         DS1302_CLK=0;
         _nop_();
         DS1302_RST=1;
         InputByte(addr);
         _nop_();
         InputByte(TDat);
         DS1302_CLK=1;
         _nop_();
         DS1302_RST=0;
        }
        //讀DS1302地址子程序//
        unsigned char read_ds1302(unsigned char addr)
        {
           unsigned char timedata;
           DS1302_RST=0;
           _nop_();
           DS1302_CLK=0;
           _nop_();
           DS1302_RST=1;
           InputByte(addr);
           timedata=outputbyte();
           DS1302_CLK=1;
           _nop_();
           DS1302_RST=0;
           return(timedata);
        }
        //DS1302的初始化//
        void initial_ds1302()
        {
          write_ds1302(0x8e,0x00);//寫保護寄存器
          write_ds1302(0x8c,time[0]);//年
          write_ds1302(0x88,time[1]);//月
          write_ds1302(0x86,time[2]);//日
          write_ds1302(0x8A,time[3]);//星期
          write_ds1302(0x84,time[4]);//時
          write_ds1302(0x82,time[5]);//分
          write_ds1302(0x80,time[6]);//秒
          write_ds1302(0x8e,0x80);//寫保護寄存器
        }
        //讀DS1302時間//
        void read_time()
        {
         second=read_ds1302(0x81);
         minute=read_ds1302(0x83);
         hour=read_ds1302(0x85);
         week=read_ds1302(0x8B);
         day=read_ds1302(0x87);
         month=read_ds1302(0x89);
         year=read_ds1302(0x8d);
        
        }
        
        //液晶顯示程序//
        void write_com(uchar com)//寫指令//
        {
          lcdrs=0;            
         lcdwr=0;
         P0=com;
         delay(5);
         lcden=1;
         delay(5);
         lcden=0;
        }
        void write_data(uchar date)   //寫數據//
        {
          lcdrs=1;
         lcdwr=0;
         P0=date;
         delay(5);
         lcden=1;
         delay(5);
         lcden=0;
        }
        void init()
        {
         
          lcden=0;
          write_com(0x38);    //置初值//
          write_com(0x0c);
          write_com(0x06);
          write_com(0x81);
        }
        void Display(void)
        {
            num=0;
            count=0;
            for(date=0;date<6;date++)
              {
                 write_data(table3[date]);
              }
            //write_com(0x81);
            write_data(table[hour/16]);   //寫第1行數據//
            write_data(table[hour%16]);
            write_data(':');
        
            write_data(table[minute/16]);
            write_data(table[minute%16]);
            write_data(':');
        
            write_data(table[second/16]);        
            write_data(table[second%16]);
            //write_com(0x01);  
            write_com(0x80+0x41);
            for(shi=0;shi<6;shi++)
              {
                 write_data(table4[shi]);
              }
            write_data(table[year/16]);
            write_data(table[year%16]);
            write_data('-');
            write_data(table[month/16]);
            write_data(table[month%16]);
            write_data('-');
            write_data(table[day/16]);
            write_data(table[day%16]);
          
            write_com(0x80);
            for(count=0;count<16;count++);
              {
                write_data(table2[count]);        
              }
           
        }
        int main(void)
        {
            initial_ds1302();
            init();
            for(;;)
              {
               read_time();
               Display();
              }
        }
        關閉窗口

        相關文章

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