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

QQ登錄

只需一步,快速開(kāi)始

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

帶時(shí)間自校準(zhǔn)功能單片機(jī)電子鐘電路與源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
帶時(shí)間自校準(zhǔn)功能電子鐘



單片機(jī)源程序如下:
  1. //電子鐘控制程序


  2. //---------------------------------------------------------------------------
  3. #include <AT89X51.H>

  4. #include "程序.h"


  5. //---------------------------------------------------------------------------
  6. //主控
  7. void main()
  8. {
  9.         ChuShiHua(); //初始化
  10.         for (;;)
  11.         {
  12.                 if (P1_0 == 0)
  13.                         SheShi(0); //設(shè)時(shí)
  14.                 if (P1_1 == 0)
  15.                         SheFen(0); //設(shè)分
  16.                 if (P1_2 == 0)
  17.                         SheMiao(0); //設(shè)秒
  18.                 P1_7 = INT0; //校準(zhǔn)指示燈
  19.         }
  20. }


  21. //---------------------------------------------------------------------------
  22. //初始化
  23. void ChuShiHua()
  24. {
  25.         shi = fen = miao = 0; //時(shí)、分、秒
  26.         PX0 = 1; //INT0高優(yōu)先級(jí)
  27.         IT0 = 1; //INT0邊沿觸發(fā)
  28.         TMOD = 2; //TIMER0方式2(8位自動(dòng)重新裝入計(jì)數(shù)器)
  29.         TH0 = TL0 = 56; //TIMER0計(jì)數(shù)周期0.1ms
  30.         IE = 0x83; //開(kāi)中斷
  31.         TR0 = 1; //開(kāi)TIMER0
  32. }


  33. //---------------------------------------------------------------------------
  34. //設(shè)時(shí)
  35. //入口:次數(shù),P1.0變成0時(shí)第0次、100ms后第1次
  36. void SheShi(const char ci_shu)
  37. {
  38.         static bit yaj = 0; //已按鍵
  39.         if (ci_shu == 0)
  40.                 yaj = 1;
  41.         else
  42.         {
  43.                 if (yaj == 1 && P1_0 == 0)
  44.                 {
  45.                         shi++;
  46.                         if (shi >= 24)
  47.                                 shi = 0;
  48.                         P1_0 = 1;
  49.                 }
  50.                 yaj = 0;
  51.         }
  52. }


  53. //---------------------------------------------------------------------------
  54. //設(shè)分
  55. //入口:次數(shù),P1.1變成0時(shí)第0次、100ms后第1次
  56. void SheFen(const char ci_shu)
  57. {
  58.         static bit yaj = 0; //已按鍵
  59.         if (ci_shu == 0)
  60.                 yaj = 1;
  61.         else
  62.         {
  63.                 if (yaj == 1 && P1_1 == 0)
  64.                 {
  65.                         fen++;
  66.                         if (fen >= 60)
  67.                                 fen = 0;
  68.                         P1_1 = 1;
  69.                 }
  70.                 yaj = 0;
  71.         }
  72. }


  73. //---------------------------------------------------------------------------
  74. //設(shè)秒
  75. //入口:次數(shù),P1.2變成0時(shí)第0次、100ms后第1次
  76. void SheMiao(const char ci_shu)
  77. {
  78.         static bit yaj = 0; //已按鍵
  79.         if (ci_shu == 0)
  80.                 yaj = 1;
  81.         else
  82.         {
  83.                 if (yaj == 1 && P1_2 == 0)
  84.                 {
  85.                         miao++;
  86.                         if (miao >= 24)
  87.                                 miao = 0;
  88.                         P1_2 = 1;
  89.                 }
  90.                 yaj = 0;
  91.         }
  92. }


  93. //---------------------------------------------------------------------------
  94. //掃描顯示
  95. void SaoMiaoXianShi()
  96. {
  97.         static char xian_shi_wei = 0; //顯示位
  98.         if (++xian_shi_wei > 5)
  99.                 xian_shi_wei = 0;
  100.         P0 = 0; //全關(guān)了
  101.         P2 = LED[xian_shi_wei];
  102.         switch (xian_shi_wei) //開(kāi)下一個(gè)
  103.         {
  104.         case 0:
  105.                 P0_0 = 1;
  106.                 break;
  107.         case 1:
  108.                 P0_1 = 1;
  109.                 break;
  110.         case 2:
  111.                 P0_2 = 1;
  112.                 break;
  113.         case 3:
  114.                 P0_3 = 1;
  115.                 break;
  116.         case 4:
  117.                 P0_4 = 1;
  118.                 break;
  119.         case 5:
  120.                 P0_5 = 1;
  121.         }
  122. }


  123. //---------------------------------------------------------------------------
  124. //計(jì)時(shí)
  125. void JiShi() interrupt 1 //TIMER0
  126. {
  127.         static char di_da0_1 = 0, di_da10 = 0, di_da100 = 0; //滴答
  128.         if (++di_da0_1 >= 100)
  129.         {
  130.                 SaoMiaoXianShi(); //掃描顯示
  131.                 di_da0_1 = 0;
  132.                 if (++di_da10 >= 10)
  133.                 {
  134.                         SheShi(1); //設(shè)時(shí)
  135.                         SheFen(1); //設(shè)分
  136.                         SheMiao(1); //設(shè)秒
  137.                         di_da10 = 0;
  138.                         if (++di_da100 >= 10)
  139.                         {
  140.                                 if (++miao >= 60) //秒
  141.                                 {
  142.                                         miao = 0;
  143.                                         if (++fen >= 60) //分
  144.                                         {
  145.                                                 fen = 0;
  146.                                                 if (++shi >= 24) //時(shí)
  147.                                                         shi = 0;
  148.                                         }
  149.                                 }
  150.                                 di_da100 = 0;
  151.                         }
  152.                 }
  153.         }
  154. }


  155. ……………………

  156. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
電路.rar (989.9 KB, 下載次數(shù): 17)
程序.rar (7.08 KB, 下載次數(shù): 14)






分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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