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

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于單片機(jī)定時(shí)器的可調(diào)時(shí)鐘程序

          基于單片機(jī)定時(shí)器的可調(diào)時(shí)鐘程序

          作者: 時(shí)間:2012-08-30 來(lái)源:網(wǎng)絡(luò) 收藏

          此時(shí)間沒(méi)有用到ds1302等芯片,而是用自帶的制作的,并且整時(shí)間。請(qǐng)大家多多指教
          #includereg52.h
          #include intrins.h
          #define uchar unsigned char
          #define uint unsigned int

          uint schedule[6];
          uint count,b; //計(jì)數(shù):以秒為單位
          uint number[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //對(duì)應(yīng)數(shù)碼管的0-9
          uint a[4]={0xfe,0xfd,0xfb,0xf7}; //對(duì)應(yīng)的四個(gè)數(shù)碼管
          uint minute[2],second[2] ;
          sbit P30=P3^0; //P3口作為鍵盤控制端
          sbit P31=P3^1;
          sbit P36=P3^6;
          sbit P35=P3^5;
          sbit P34=P3^4;
          sbit P33=P3^3;
          sbit P32=P3^2;
          sbit P37=P3^7;
          void assign();
          void show();

          void delay(unsigned n);
          void main()
          {
          uint i ;
          EA=1;//總中斷打開
          IT0=1;//外部中斷打開
          EX0=1; //
          count=43200;
          TMOD=0x10;
          P30=0;
          P31=1;
          P32=1;
          P33=1;
          P34=0;
          P35=1;
          P36=1;
          P37=1;
          while(1)
          {
          for(i=0;i=19;i++)//每循環(huán)一次用時(shí)50ms.
          {
          TR1=1;//選擇計(jì)數(shù)器1
          TH1= 0x3c;
          TL1=0xb0;//賦初值
          show(); //顯示函數(shù)
          TF1=0; //恢復(fù)標(biāo)志

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

          }
          count++;


          }

          }
          void assign() //給小時(shí),分鐘,秒賦值
          {

          if(count>=46800) //滿了13個(gè)小時(shí)轉(zhuǎn)化為1點(diǎn)鐘
          count=count-43200;
          if(count>=36000) //計(jì)算小時(shí) 前一位 減少算法
          schedule[0]=minute[0]=1;
          else
          schedule[0]=minute[0]=0;
          schedule[1]=minute[1]=count/3600%10;//計(jì)算小時(shí)的后一位
          schedule[2]=second[0]= count%3600/600; //計(jì)算分鐘的前一位
          schedule[3]=second[1]= count%3600/60%10;//計(jì)算分鐘的后一位

          }
          void show() //顯示函數(shù)
          {
          uint i;

          while(1)
          {

          assign();
          for(i=0;i4;i++)
          {
          P1=a[i]; //亮那個(gè)數(shù)碼管
          P2=number[schedule[i]]; //亮一個(gè)什么樣的數(shù)字
          if(i==1 )
          P2=P2-0x80; //亮小數(shù)點(diǎn)便于好看
          delay(50);//延時(shí)函數(shù),便于亮的清晰



          }
          if(TF1==1) //已經(jīng)有50ms
          break;
          }
          }
          void delay(unsigned n) //延時(shí)
          {
          uint i;
          for(i=1;i=n;i++)
          {

          if(TF1==1) //已經(jīng)有50ms
          break;
          if(P31==0) //調(diào)表
          {

          for(b=0;b3000;b++) //放抖動(dòng):按的時(shí)間太短
          {

          }
          if(P31==1)
          goto back5;



          if(P31==0)
          {
          while(1)
          {

          show();
          TF1=0; //恢復(fù)標(biāo)志
          if(P32==0) //加一分鐘
          {
          for(b=0;b3000;b++)
          {}
          if(P32==1)
          goto back1;
          count=count+60;
          if(P32==0)

          {
          while(1)
          {
          show();
          TF1=0; //恢復(fù)標(biāo)志
          if(P32==1)
          break;
          }
          }

          }
          back1: show();
          if(P33==0) //減一分鐘
          {
          for(b=0;b3000;b++)
          {}
          if(P33==1)
          goto back2;
          count=count-60;

          if(P33==0)
          {
          while(1)
          {
          show();TF1=0; //恢復(fù)標(biāo)志
          if(P33==1)
          break;
          }
          }

          }

          back2:show();
          if(P36==0) //加一個(gè)小時(shí)
          {
          for(b=0;b3000;b++)
          {}
          if(P36==1)
          goto back3;
          count=count+3600;

          if(P36==0)
          {
          while(1)
          {
          show(); TF1=0; //恢復(fù)標(biāo)志
          if(P36==1)
          break;
          }
          }

          }

          back3:show();
          if(P37==0) //減一個(gè)小時(shí)
          {
          for(b=0;b3000;b++)
          {}
          if(P37==1)
          goto back4;
          if(schedule[0]==0schedule[1]==1) //處理1點(diǎn)鐘到12點(diǎn)
          count=count+39600;
          else
          count=count-3600;

          if(P37==0)
          {
          while(1)
          {
          show(); TF1=0; //恢復(fù)標(biāo)志
          if(P37==1)
          break;
          }
          }

          }

          back4:show();
          if(P35==0)//沒(méi)有必要防抖
          break;

          }
          }
          back5: show(); //按的時(shí)間太duan

          }

          }


          }


          有幾個(gè)缺點(diǎn):一,調(diào)時(shí)間的時(shí)候有點(diǎn)暗。二,算法比較差。三,代碼比較難看。四,當(dāng)時(shí)我想從0到24小時(shí)計(jì)時(shí),但count為int溢出了,后面我不用count計(jì)時(shí),可是可以但是調(diào)時(shí)間的話有比較難寫,不想亂費(fèi)時(shí)間了,改成長(zhǎng)整型有不知道那個(gè)地方出問(wèn)題。希望高手指點(diǎn)。
          分享到:



          評(píng)論


          相關(guān)推薦

          技術(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); })();