找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2675|回复: 1
打印 上一主题 下一主题
收起左侧

基于51单片机的按键数码管时钟仿真+代码,显示18-11-18

[复制链接]
跳转到指定楼层
楼主
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. #include<reg51.h>

  2. typedef unsigned int u16;          //对数据类型进行声明定义
  3. typedef unsigned char u8;

  4. u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
  5.                                         0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//显示0~F的值

  6. sbit Key1 = P3^7;   //计时停止
  7. sbit Key2 = P3^6;        //调位
  8. sbit Key3 = P3^0;        //加一       
  9. sbit Key4 = P3^2;   //切换
  10. sbit LED1 = P1^7;
  11. sbit LSA  = P2^5;
  12. sbit LSB  = P2^6;
  13. sbit LSC  = P2^7;

  14. u8 second=00,minute=00,hour=00,count;

  15. void Delayms(u16 t)
  16. {
  17. u16 i,j;
  18. for(i=0;i<t;i++)
  19. for(j=0;j<120;j++);
  20. }



  21. void DigDisplay()
  22. {
  23.         u8 i;
  24.         for(i=0;i<8;i++)
  25.         {
  26.                 switch(i)         //位选,选择点亮的数码管,
  27.                 {
  28.                         case(0):
  29.                                 LSA=0;LSB=0;LSC=0;                 P0=smgduan[second%10];break;//显示第0位
  30.                         case(1):
  31.                                    LSA=0;LSB=0;LSC=1;      P0=smgduan[second/10];break;//显示第1位
  32.                         case(2):
  33.                                 LSA=0;LSB=1;LSC=0;                 P0=0x40;break;//显示第2位
  34.                         case(3):
  35.                                 LSA=0;LSB=1;LSC=1;                 P0=smgduan[minute%10];break;//显示第3位
  36.                         case(4):
  37.                                 LSA=1;LSB=0;LSC=0;                 P0=smgduan[minute/10];break;//显示第4位
  38.                         case(5):
  39.                                 LSA=1;LSB=0;LSC=1;                 P0=0x40;break;//显示第5位
  40.                         case(6):
  41.                            LSA=1;LSB=1;LSC=0;
  42.                                         P0=smgduan[hour%10];break;//显示第6位
  43.                         case(7):
  44.                                 LSA=1;LSB=1;LSC=1;                 P0=smgduan[hour/10];break;//显示第7位       
  45.                 }

  46.                 Delayms(1); //间隔一段时间扫描       
  47.                 P0=0x00;//消隐
  48.         }
  49. }




  50. void Keyscan1()
  51. {
  52. static        u8 i=0,j=0;       
  53. if(Key1==0)       
  54. {
  55. Delayms(10); //消抖       
  56. if(Key1==0)       
  57. while(!Key1); //等待按键弹起
  58. i++;
  59. }
  60. if(i%2==1)       
  61. {
  62. LED1=0;
  63. TR0=0;
  64. }
  65. if(i%2==0)
  66. {
  67. LED1=1;
  68. TR0=1;       
  69. }
  70. if(Key2==0)       
  71. {
  72. Delayms(10);       
  73. if(Key2==0)
  74. while(!Key2);
  75. j++;       
  76. }
  77. if(j%4==1)
  78. {
  79. if(Key3==0)
  80. {
  81. Delayms(10);       
  82. if(Key3==0)
  83. while(!Key3);
  84. second++;
  85. if(second==60)
  86. second=0;
  87. }
  88. }
  89. if(j%4==2)
  90. {
  91. if(Key3==0)
  92. {
  93. Delayms(10);       
  94. if(Key3==0)
  95. while(!Key3);
  96. minute++;
  97. if(minute==60)
  98. minute=0;       
  99. }
  100. }
  101. if(j%4==3)
  102. {
  103. if(Key3==0)
  104. {       
  105. Delayms(10);       
  106. if(Key3==0)
  107. while(!Key3);
  108. hour++;
  109. if(hour==24)
  110. hour=0;       
  111. }
  112. }
  113. }


  114. void main()
  115. {

  116. TMOD=0x01;
  117. TH0=(65536-10000)/256;
  118. TL0=(65536-10000)%256;
  119. EA=1;
  120. ET0=1;
  121. TR0=1;
  122. while(1)
  123. {
  124. static        u8 h=0;       
  125. if(Key4==0)       
  126. {
  127. Delayms(10);       
  128. if(Key4==0)
  129. while(!Key4);
  130. h++;
  131. }

  132. if(h%2==0)
  133. {
  134. DigDisplay();
  135. Keyscan1();       
  136. }

  137. }
  138. }

  139. void time0_int(void) interrupt 1
  140. {
  141. TH0=(65536-10000)/256;
  142. TL0=(65536-10000)%256;
  143. count++;
  144. if(count==100)
  145. {
  146. count=0;
  147. second++;
  148. if(second==60)
  149. {
  150. second=0;
  151. minute++;
  152. if(minute==60)
  153. {
  154. minute=0;
  155. hour++;
  156. if(hour==24)
  157. {
  158. hour=0;
  159. }
  160. }
  161. }
  162. }
  163. }
复制代码

所有资料51hei提供下载:
18-11-18按键数码管时钟.rar (64.49 KB, 下载次数: 39)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:734001 发表于 2020-4-22 11:13 | 只看该作者
为什么秒到10就进一分钟了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表