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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 用定時(shí)器中斷設(shè)計(jì)秒表 用數(shù)碼管顯示

          用定時(shí)器中斷設(shè)計(jì)秒表 用數(shù)碼管顯示

          作者: 時(shí)間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
          //此秒表有時(shí)分秒和毫秒位,最多可以記小時(shí),有暫停和繼續(xù)計(jì)時(shí)功能,獨(dú)立鍵盤上key1為暫停和繼續(xù)鍵,key3為復(fù)位和開始計(jì)時(shí)鍵

          //由于ms中斷時(shí)間很短,所以如果中斷和顯示延遲關(guān)系處理不好,秒表走時(shí)不準(zhǔn),應(yīng)注意

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

          #include

          #defineucharunsignedchar

          #defineuintunsignedint

          uchar code table[]={0x 3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

          uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};//時(shí)分秒的個(gè)位顯示后帶小數(shù)點(diǎn)

          uchar ms,s,m,h,count,count1;

          sbit k1=P3^0;

          sbit k3=P3^2;

          voiddelay(uint z)

          {

          uint x,y;

          for(x=z;x>0;x--)

          for(y=110;y>0;y--);

          }

          voiddisplays(uchar temp)//數(shù)碼管動(dòng)態(tài)顯示秒位函數(shù)

          {

          uchar shi,ge,i;

          i=0;

          shi=temp/10;

          ge=temp;

          P0=0xef;

          P1=table[shi];

          delay(1);//必須要有延遲,動(dòng)態(tài)掃描,為了不影響整個(gè)秒表八位數(shù)的掃描速率提高顯示效果,延遲又不要太高,ms比較合適

          P0=0xdf;

          P1=table1[ge];

          i=0;

          delay(1);

          }

          voiddisplayms(uchar temp)//數(shù)碼管動(dòng)態(tài)顯示毫秒位函數(shù)

          {

          uchar shi,ge,i;

          i=0;

          shi=temp/10;

          ge=temp;

          P0=0xbf;

          P1=table[shi];

          delay(1);

          P0=0x7f;

          P1=table[ge];

          i=0;

          delay(1);

          }

          voiddisplaym(uchar temp)//數(shù)碼管動(dòng)態(tài)顯示分位函數(shù)

          {

          uchar shi,ge,i;

          i=0;

          shi=temp/10;

          ge=temp;

          P0=0xfb;

          P1=table[shi];

          delay(1);

          P0=0xf7;

          P1=table1[ge];

          i=0;

          delay(1);

          }

          voiddisplayh(uchar temp)//數(shù)碼管動(dòng)態(tài)顯示小時(shí)位函數(shù)

          {

          uchar shi,ge,i;

          i=0;

          shi=temp/10;

          ge=temp;

          P0=0xfe;

          P1=table[shi];

          delay(1);

          P0=0xfd;

          P1=table1[ge];

          i=0;

          delay(1);

          }

          voidkeyscan()//鍵盤掃描函數(shù)

          {

          if(k1==0)

          {

          delay(5);

          if(k1==0)//檢測k1確實(shí)被按下防抖動(dòng)

          {

          count++;

          while(!k1);//檢測松手

          delay(1);//檢測確實(shí)松手

          while(!k1);

          if(count==1)

          TR0=0;//暫停定時(shí)器

          if(count==2)

          {

          TR0=1;//定時(shí)器繼續(xù)計(jì)時(shí)

          count=0;

          }

          }

          }

          if(k3==0)

          {

          delay(5);

          if(k3==0)

          {

          count1++;

          while(!k3);

          delay(1);

          while(!k3);

          if(count1==1)//復(fù)位秒表

          {

          TR0=0;

          ms=0;

          s=0;

          m=0;

          h=0;

          }

          if(count1==2)//重新開始計(jì)時(shí)

          {

          TR0=1;

          count1=0;

          }

          }

          }

          }

          voidmain()

          {

          TMOD=0x01;

          EA=1;

          ET0=1;

          TR0=1;

          TH0=(65536-10000)/256;//設(shè)定定時(shí)器初值

          TL0=(65536-10000)%6;//12M晶振時(shí)ms數(shù)為

          while(1)

          {

          keyscan();

          displays(s);//數(shù)碼管動(dòng)態(tài)掃描秒位顯示

          displayms(ms);//數(shù)碼管動(dòng)態(tài)掃描毫秒位顯示

          displaym(m);//數(shù)碼管動(dòng)態(tài)掃描秒分顯示

          displayh(h);//數(shù)碼管動(dòng)態(tài)掃描秒小時(shí)顯示

          }

          }

          voidtimer0() interrupt 1//中斷服務(wù)程序

          {

          TH0=(65536-10000)/256;

          TL0=(65536-10000)%6;

          ms++;

          if(ms==100)//定時(shí)器中斷次為s

          {//把這部分放在中斷中,能減少程序執(zhí)行時(shí)間對中斷時(shí)間的影響

          ms=0;

          s++;

          if(s==60)

          {

          s=0;

          m++;

          }

          if(m==60)

          {

          m=0;

          h++;

          }

          if(h==24)

          {

          h=0;

          }

          }

          }



          評論


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