找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6391|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

這是一個(gè)51單片機(jī)倒計(jì)時(shí)程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:123409 發(fā)表于 2016-5-26 20:18 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
大家參考參考,可以提提寶貴建議

  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #define uchar unsigned char
  4. #define uint  unsigned int

  5. uint countb=0,counta=0;                // counta 為10ms計(jì)數(shù)器,countb為1s計(jì)數(shù)器,均為全局變量
  6. sbit s1 = P2^0;
  7. sbit s2 = P2^1;
  8. sbit s3 = P2^2;
  9. sbit s4 = P2^3;
  10. sbit en = P2^5;
  11. sbit speaker = P2^4;

  12. sbit k1=P3^2;
  13. sbit k2=P3^3;
  14.        
  15. uchar a1,a2,b1,b2;

  16. code ledseg7[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};           //0~9不帶小數(shù)點(diǎn)的段碼表
  17.             //   0    1    2    3    4    5    6    7    8    9
  18. code ledseg8[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
  19.             //   0    1    2    3    4    5    6    7    8    9            //0~9帶小數(shù)點(diǎn)的段碼表


  20. /********延時(shí)函數(shù)********/
  21. void Delay(uint xms)              //延時(shí)程序,xms是形式參數(shù)
  22. {
  23.         uint i,j;
  24.         for(i=xms;i>0;i--)      
  25.         for(j=100;j>0;j--);                //此處分號(hào)不可少
  26. }

  27. /*******顯示函數(shù)********/
  28. void display(uchar a,uchar b,uchar c,uchar d)
  29. {
  30.         en = 1;           //啟用數(shù)碼管信號(hào)

  31.         s1 = 0;
  32.         P0 = ledseg7[a];
  33.         Delay(5);     
  34.         s1 = 1;

  35.         s2 = 0;
  36.         P0 = ledseg8[b];
  37.         Delay(5);     
  38.         s2 = 1;

  39.         s3 = 0;
  40.         P0 = ledseg7[c];
  41.         Delay(5);      
  42.         s3 = 1;

  43.         s4 = 0;
  44.         P0 = ledseg7[d];
  45.         Delay(5);   
  46.         s4 = 1;
  47. }

  48. /********重置,暫停設(shè)置******/
  49. void key()
  50. {
  51.         if(k1==0)         //開始,暫停
  52.     {
  53.         Delay(10);
  54.                    if(k1==0)
  55.         {
  56.                      while(!k1);
  57.                          TR0=~TR0;
  58.         }
  59.     }
  60.         if(k2==0)          //復(fù)位
  61.         {
  62.            Delay(10);
  63.            if(k2==0)
  64.            {
  65.             TH0=0xDC; TL0=0x00;
  66.         countb=0;
  67.                 counta=0;
  68.         TR0=0;
  69.         while(k2!=0);
  70.     }
  71.     }
  72.   
  73. }

  74. /********定時(shí)器T0中斷函數(shù)*******/
  75. void timer0() interrupt  1
  76. {  
  77.         TH0=0xDC;TL0=0x00;
  78.         counta++;                            //計(jì)數(shù)值加1
  79.           if(counta==100)                //若counta為99,(100*10ms=1s)
  80.           {       
  81.                   counta=0;                        // counta清0
  82.                 countb++;           
  83.           }
  84.         if(countb==30)
  85.         {
  86.                 TR0=0;
  87.                 s3=1;
  88.                 counta=0,countb=0;
  89.                 speaker=0;
  90.                 Delay(500);
  91.                 speaker=1;
  92.                 Delay(500);
  93.                 speaker=0;
  94.                 Delay(500);
  95.                 speaker=1;
  96.         }
  97. }

  98. void main()
  99. {
  100.         P0=0xff;
  101.            TMOD=0x01;                         //定時(shí)器T0方式1
  102.            TH0=0xDC; TL0=0x00;              //10ms定時(shí)初值
  103.         TR0=0;
  104.            EA=1;
  105.            ET0=1;         //開總中斷,開定時(shí)器T0中斷,啟動(dòng)定時(shí)器T0
  106.            while(1)
  107.            {
  108.             key();
  109.                 b2=countb/10;      //取出記數(shù)秒數(shù)的十位
  110.                    b1=countb%10;      //取出計(jì)數(shù)秒的個(gè)位
  111.                 a2=counta/10;      //取出記數(shù)百分秒數(shù)的十位
  112.                    a1=counta%10;             //取出計(jì)數(shù)百分秒的個(gè)位
  113.                    display(b2,b1,a2,a1);  //調(diào)顯示函數(shù)
  114.            }
  115. }
復(fù)制代碼


clock.docx

13.44 KB, 下載次數(shù): 14, 下載積分: 黑幣 -5

程序代碼

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:214205 發(fā)表于 2017-6-24 12:01 | 只看該作者
很給力
回復(fù)

使用道具 舉報(bào)

板凳
ID:213173 發(fā)表于 2017-6-24 16:43 | 只看該作者
顯示函數(shù)執(zhí)行一次超過20ms,有可能閃爍,也無法正常反映十分秒和百分秒動(dòng)態(tài),按鍵程序里使用了Delay(10);延時(shí)和while(!k1);等待按鍵釋放,會(huì)明顯劣化顯示質(zhì)量。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表