<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AT89C2051+LCD1602+DS1302實(shí)時(shí)時(shí)鐘設(shè)計(jì)(c51)

          AT89C2051+LCD1602+DS1302實(shí)時(shí)時(shí)鐘設(shè)計(jì)(c51)

          作者: 時(shí)間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
          #include

          #include
          #define uchar unsigned char
          sbit rs=P3^0;//LCD1602控制腳
          sbit rw=P3^1;
          sbit e=P3^2;

          本文引用地址:http://www.ex-cimer.com/article/201611/317454.htm

          sbit io=P3^4;//DS1302 PCI總線引腳
          sbit rst=P3^5;
          sbit sclk=P3^3;

          uchar hour,minute,second,year,months,date,day;//顯示時(shí)間寄存器
          uchar whour,wminute,wsecond,wyear,wmonths,wdate,wday;//設(shè)制初始時(shí)間寄存器
          uchar code table1[]="0123456789-:w";
          uchar code table2[]="Date:20";
          uchar code table3[]="Time:";

          void delayms(uchar k)//1ms延時(shí)
          {
          uchar j,i;
          for(i=0;ifor(j=0;j<120;j++);
          }

          void delay2us() //2us延時(shí)
          {
          _nop_();
          _nop_();
          }

          void write1602_data(uchar dat)//1602寫數(shù)據(jù)子程序
          {
          rs=1;
          rw=0;
          P1=dat;
          delayms(2);
          e=0;
          e=1;
          e=0;
          }

          void write1602_com(uchar com)//1602寫命令子程序
          //
          {
          rs=0;
          rw=0;
          P1=com;
          delayms(5);
          e=0;
          e=1;
          e=0;
          }

          void init1602()//1602初始化子程序
          {
          write1602_com(0x38);//lcd1602 16*2顯示,5*7點(diǎn)陣,8bit數(shù)據(jù)接口
          write1602_com(0x0f);//開顯示,顯示光標(biāo),光標(biāo)閃爍
          write1602_com(0x06);//當(dāng)寫一字符后地址指針加一,光標(biāo)加一
          write1602_com(0x01);//清屏
          }

          void disptop() //1602第一行顯示子程序
          {
          uchar i;
          write1602_com(0x80);
          for(i=0;i<0x07;i++)
          {
          write1602_data(table2[i]);
          delayms(1);
          }
          }

          void dispbot() //1602第二行顯示子程序
          {
          uchar i;
          write1602_com(0xc0);
          for(i=0;i<0x05;i++)
          {
          write1602_data(table3[i]);
          delayms(1);
          }
          }

          /*******************************************************/

          void write1302byte(uchar dat)//1302寫入一個(gè)字節(jié)子程序
          {
          uchar i;
          sclk=0;
          for(i=0;i<8;i++)
          {
          io=(bit)(dat&0x01);
          delay2us();
          sclk=1;
          delay2us();
          sclk=0;
          dat=dat>>1;
          }
          }

          uchar read1302byte() //1302讀一個(gè)字節(jié)子程序
          {
          uchar i,dat;
          for(i=0;i<8;i++)
          {
          dat>>=1;
          if(io)
          dat|=0x80;
          delay2us();
          sclk=1;
          delay2us();
          sclk=0;
          delay2us();
          }
          return dat;
          }

          void writeset1302(uchar add,uchar dat)//1302讀子女程序
          {
          rst=0;
          delay2us();
          rst=1;
          write1302byte(add);
          write1302byte(dat);
          rst=0;
          }

          uchar readset1302(uchar add)//1302寫子程序
          {
          uchar dat;
          rst=0;
          delay2us();
          rst=1;
          write1302byte(add);
          dat=read1302byte();
          rst=0;
          return dat;
          }

          void init1302()//1302初始化子程序
          {
          uchar flag;
          flag=readset1302(0x81);//判斷1302是否有備用電池,若有則FLAG為0
          if(flag&0x80)
          {
          writeset1302(0x8e,0x00);
          writeset1302(0x80,((wsecond/10)<<4|(wsecond%10)));
          writeset1302(0x82,((wminute/10)<<4|(wminute%10)));
          writeset1302(0x84,((whour/10)<<4|(whour%10)));
          writeset1302(0x86,((wday/10)<<4|(wday%10)));
          writeset1302(0x88,((wmonths/10)<<4|(wmonths%10)));
          writeset1302(0x8a,((wdate/10)<<4|(wdate%10)));
          writeset1302(0x8c,((wyear/10)<<4|(wyear%10)));
          writeset1302(0x90,0xa5);
          writeset1302(0x8e,0x80);
          }
          }

          uchar readvalue(uchar value) //數(shù)據(jù)轉(zhuǎn)換
          {
          uchar a;
          a=((value&0x70)>>4)*10+(value&0x0f);
          return a;
          }

          void read1302() //讀取時(shí)間
          {
          uchar value;
          value=readset1302(0x81);
          second=readvalue(value);

          value=readset1302(0x83);
          minute=readvalue(value);

          value=readset1302(0x85);
          hour=readvalue(value);

          value=readset1302(0x87);
          day=readvalue(value);

          value=readset1302(0x89);
          months=readvalue(value);

          value=readset1302(0x8b);
          date=readvalue(value);

          value=readset1302(0x8d);
          year=((value&0xf0)>>4)*10+(value&0x0f);

          }

          void disptime() //時(shí)間顯示
          {
          write1602_com(0x87);
          write1602_data(table1[year/10]);
          write1602_data(table1[year%10]);
          write1602_data(table1[10]);
          write1602_data(table1[months/10]);
          write1602_data(table1[months%10]);
          write1602_data(table1[10]);
          write1602_data(table1[day/10]);
          write1602_data(table1[day%10]);
          write1602_com(0xc5);
          write1602_data(table1[hour/10]);
          write1602_data(table1[hour%10]);
          write1602_data(table1[11]);
          write1602_data(table1[minute/10]);
          write1602_data(table1[minute%10]);
          write1602_data(table1[11]);
          write1602_data(table1[second/10]);
          write1602_data(table1[second%10]);
          write1602_com(0xce);
          write1602_data(table1[12]);
          write1602_data(table1[date]);
          }

          void inittime() //初始時(shí)間設(shè)置
          {
          whour=23;
          wminute=59;
          wsecond=20;
          wyear=10;
          wmonths=12;
          wdate=3;
          wday=1;
          }

          void main()
          {
          inittime();
          init1602();
          disptop();
          dispbot();
          init1302();
          while(1)
          {
          read1302();
          disptime();
          }
          }



          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();