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

QQ登錄

只需一步,快速開始

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

數(shù)碼管時(shí)鐘和按鍵結(jié)合的問題,有點(diǎn)小問題,請(qǐng)教下大家

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
最近學(xué)了數(shù)碼管的時(shí)鐘顯示和按鍵的控制,于是想到小時(shí)候戴過的幾塊錢的電子表,就想實(shí)現(xiàn)一個(gè),可以通過按鍵來手動(dòng)調(diào)時(shí)間!代碼寫好了,單片機(jī)也能跑起來,不按按鍵的時(shí)候,時(shí)鐘可以自動(dòng)實(shí)現(xiàn),但是有問題,就是當(dāng)我用按鍵實(shí)現(xiàn)對(duì)分鐘和時(shí)鐘的調(diào)整(這里是加1)后,這個(gè)時(shí)鐘就會(huì)停止運(yùn)行了!我的猜想是,按鍵實(shí)現(xiàn)后,程序沒有進(jìn)入到中斷里面了,所以時(shí)鐘停止!但不知道對(duì)不對(duì)!代碼如下
  1. #include <reg52.h>
  2. sbit S5=P3^7;//按鍵控制分鐘的調(diào)整
  3. sbit S4=P3^6;//按鍵控制時(shí)鐘的調(diào)整
  4. sbit D1=P1^0;
  5. sbit D2=P1^1;
  6. sbit WELA=P2^7;
  7. sbit DULA=P2^6;
  8. #define uint unsigned int
  9. uint num ,a,b,c,d,e,f,g,x,y,z;
  10. void delay(uint z);//延時(shí)
  11. void display(uint a,uint b,uint c,uint d, uint e,uint f);//數(shù)碼管顯示函數(shù)
  12. unsigned char code table1[]=
  13. {
  14.         0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F

  15. };
  16. unsigned char code table2[]=
  17. {
  18. 0XDF,0XEF,0XF7,0XFB,0XFD,0XFE

  19. };
  20. void main()
  21.         {
  22.         TMOD=0X00;
  23.         TH0=(8192-8000)/32;
  24.         TL0=(8192-8000)%32;
  25.         EA=1;
  26.         ET0=1;
  27.         TR0=1;

  28.         while(1)
  29.         {       
  30.                 if(num==1)
  31.                 {
  32.                         num=0;
  33.                         a++;
  34.                 }
  35.                 if(S5==0)
  36.                 {
  37.                                 delay(10);
  38.                                 if(S5==0)
  39.                         {
  40.                                         D1=0;
  41.                                         c++;
  42.                                         if(c==10)
  43.                                         {
  44.                                                 d++;
  45.                                                 c=0;
  46.                                                 if(d==6)
  47.                                                 {
  48.                                                         e++;
  49.                                                          d=0;
  50.                                                         if(e==24)
  51.                                                         {
  52.                                                                 e=0;
  53.                                                         }
  54.                                                 }
  55.                                         }
  56.                                 while(!S5);
  57.                                 delay(10);
  58.                                 while(!S5);
  59.                         }
  60.                 }
  61.                 if(S4==0)
  62.                 {
  63.                         delay(10);
  64.                         if(S4==0)
  65.                         {
  66.                                 D2=0;
  67.                                 e++;
  68.                                 if(e==24)
  69.                                 {
  70.                                         e=0;
  71.                                 }
  72.                                 while(!S4);
  73.                                 delay(10);
  74.                                 while(!S4);
  75.                         }
  76.                 }
  77.                         else
  78.                         {
  79.                                 D1=1;
  80.                                 D2=1;
  81.                         }
  82.                 if(a==10)
  83.                 {
  84.                         b++;
  85.                         a=0;
  86.                         if(b==6)
  87.                         {
  88.                                 c++;
  89.                                 b=0;
  90.                                 if(c==10)
  91.                                 {
  92.                                         d++;
  93.                                         c=0;
  94.                                         if(d==6)
  95.                                         {
  96.                                                 e++;
  97.                                                  d=0;
  98.                                                 if(e==24)
  99.                                                 {
  100.                                                         e=0;
  101.                                                 }
  102.                                         }
  103.                                 }
  104.                         }
  105.                 }
  106.                 g=e%10;
  107.                 f=(e-g)/10;       
  108.                 display(a,b,c,d,g,f);
  109.           }
  110. }
  111. void display(uint a,uint b,uint c,uint d,uint g,uint f)
  112.         {
  113.         WELA=1;
  114.         P0=table2[0];
  115.         WELA=0;
  116.         DULA=1;
  117.         P0=table1[a];
  118.         DULA=0;
  119.         delay(2);
  120.         WELA=1;
  121.         P0=table2[1];
  122.         WELA=0;
  123.         DULA=1;
  124.         P0=table1[b];
  125.         DULA=0;
  126.         delay(2);
  127.         WELA=1;
  128.         P0=table2[2];
  129.         WELA=0;
  130.         DULA=1;
  131.         P0=table1[c];
  132.         DULA=0;
  133.         delay(2);
  134.         WELA=1;
  135.         P0=table2[3];
  136.         WELA=0;
  137.         DULA=1;
  138.         P0=table1[d];
  139.         DULA=0;
  140.         delay(2);
  141.         WELA=1;
  142.         P0=table2[4];
  143.         WELA=0;
  144.         DULA=1;
  145.         P0=table1[g];
  146.         DULA=0;
  147.     delay(2);
  148.         WELA=1;
  149.         P0=table2[5];
  150.         WELA=0;
  151.         DULA=1;
  152.         P0=table1[f];
  153.         DULA=0;
  154.         delay(2);
  155.         }
  156. void delay(uint z)
  157.         {
  158.         uint x,y;
  159.         for(x=z;x>0;x--)
  160.         {
  161.         for(y=20;y>0;y--);
  162.         }
  163.         }
  164. void timero() interrupt 1
  165.         {

  166.         TH0=(8192-8000)/32;
  167.         TL0=(8192-8000)%32;
  168.         num++;
  169.                
  170.         }
復(fù)制代碼
代碼有點(diǎn)長(zhǎng),但邏輯上很簡(jiǎn)單,就是一個(gè)時(shí)鐘(時(shí)分秒的顯示),然后加上按鍵,出現(xiàn)的問題描述如上!請(qǐng)大家?guī)臀铱纯,感激不盡
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:111634 發(fā)表于 2016-12-4 18:23 | 只看該作者
本帖最后由 zl2168 于 2016-12-4 18:26 編輯

校正全期間時(shí)鐘不停運(yùn)行的2個(gè)案例。
實(shí)例91 具有校正功能的時(shí)鐘1302LCD1602顯示)
先Proteus仿真,確認(rèn)有效
實(shí)例91 帶校正時(shí)鐘1302(LCD1602顯示).rar (52.74 KB, 下載次數(shù): 5)

以上摘自張志良編著《80C51單片機(jī)仿真設(shè)計(jì)實(shí)例教程——基于Keil C和Proteus》清華大學(xué)出版社ISBN 978-7-302-41682-1,書中電路和程序設(shè)計(jì)有詳細(xì)說明,程序語(yǔ)句條條有注解。
回復(fù)

使用道具 舉報(bào)

板凳
ID:111634 發(fā)表于 2016-12-4 18:27 | 只看該作者
本帖最后由 zl2168 于 2016-12-4 18:28 編輯

實(shí)例93  具有校正功能的時(shí)鐘1302(LED數(shù)碼管顯示)


Proteus仿真一下,確認(rèn)有效。
實(shí)例93 帶校正時(shí)分秒的時(shí)鐘1302(6位LED數(shù)碼管顯示).rar (732.75 KB, 下載次數(shù): 10)
以上摘自張志良編著《80C51單片機(jī)仿真設(shè)計(jì)實(shí)例教程——基于Keil CProteus》清華大學(xué)出版社ISBN 978-7-302-41682-1內(nèi)有常用的單片機(jī)應(yīng)用100案例,用于仿真實(shí)驗(yàn)操作,電路與程序真實(shí)可靠可信可行。


回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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