找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2030|回复: 1
收起左侧

基于51单片机做的一个24小时时钟程序 内含清零和暂停功能

[复制链接]
ID:435958 发表于 2018-11-29 18:50 | 显示全部楼层 |阅读模式
内含清零和暂停功能
0.png
单片机源程序如下:
  1. #include<reg51.h>
  2. unsigned char seg_data[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};  //8段数码管显示代码,最后一位0xbf为横线

  3. unsigned char position[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};                                          //选位端循环
  4. int sum_position=0;       

  5. int text=0;

  6. int s1=0,s2=0,m1=0,m2=0,h1=0,h2=0;                                                                                        //定义秒、分、时
  7. sbit pause=P3^3;                                                                                                                        //清零键
  8. unsigned int sum;

  9. void delaytime(unsigned int x);                                                                                                          //声明调用函数‘延时’
  10. void diaplayscreen(unsigned int n);                                                                                                         //声明调用函数‘显示’
  11.                                                                                                  
  12. void main()
  13. {
  14. //打开中断(暂停)
  15. EA=1;EX0=1;IT0=1;


  16. while(1)
  17. {
  18.         if(s1==9&&s2==5&&m1==9&&m2==5&&h1==3&&h2==2)
  19.         {s1=0;s2=0;m1=0;m2=0;h1=0;h2=0;        }                                                                                                //清零

  20.         s1++;                                                                                                                                                        //秒+1
  21.         if(s1>9)                                                                                                                                                //计时‘秒’
  22.         {
  23.                 s1=0;
  24.                 s2++;
  25.         }
  26.         if(s2>5)
  27.         {
  28.                 s2=0;
  29.                 m1++;
  30.         }

  31.         if(m1>9)                                                                                                                                                //计时‘分’
  32.         {
  33.                 m1=0;
  34.                 m2++;
  35.         }
  36.         if(m2>5)
  37.         {
  38.                 m2=0;
  39.                 h1++;
  40.         }

  41.         if(h1>9)                                                                                                                                                //计时‘时’
  42.         {
  43.                 h1=0;
  44.                 h2++;
  45.         }
  46.         /******8位LED显示******/
  47.         if(text==0)
  48.         {
  49.                 for(sum=0;sum<62;sum++)                                                                         //‘sum’调节1s时长(可加变量x调节)
  50.                 {                                                                                                                 //显示时间
  51.                         diaplayscreen(s1);
  52.                         diaplayscreen(s2);
  53.                         diaplayscreen(10);
  54.                         diaplayscreen(m1);
  55.                         diaplayscreen(m2);
  56.                         diaplayscreen(10);
  57.                         diaplayscreen(h1);
  58.                         diaplayscreen(h1);
  59.                
  60.                        
  61.                         if(sum_position==8)                                                                  //置零选位端
  62.                         sum_position=0;
  63.                 }
  64.         }
  65.         else
  66.         {
  67.                 while(1)                                        //无限循环显示
  68.                 {       
  69.                         diaplayscreen(s1);
  70.                         diaplayscreen(s2);
  71.                         diaplayscreen(10);
  72.                         diaplayscreen(m1);
  73.                         diaplayscreen(m2);
  74.                         diaplayscreen(10);
  75.                         diaplayscreen(h1);
  76.                         diaplayscreen(h1);
  77.                
  78.                        
  79.                         if(sum_position==8)
  80.                         sum_position=0;
  81.                         if(text==0)                                 //暂停结束
  82.                         break;                                         
  83.                 }
  84.         }
  85.         if(pause==0)
  86.         {s1=-1;s2=0;m1=0;m2=0;h1=0;h2=0;}

  87.         }
  88. }

  89.                      
  90. void diaplayscreen(unsigned int n)                                                                           //显示函数
  91. {         
  92.          P2=position[sum_position];//先选位在输入
  93.          P0=seg_data[n];
  94.          delaytime(1);
  95.          sum_position++;
  96. }



  97. void delaytime(unsigned int x)                                                           //延时函数,最小精度1毫秒
  98. {
  99. unsigned char j;
  100. while(x--)
  101. {
  102.   for(j=0;j<200;j++)
  103.   {;}
  104. }
  105. }

  106. void zero() interrupt 0                                                                                                        //暂停
  107. {
  108. //s1=0;s2=0;m1=0;m2=0;h1=0;h2=0;
  109. text=~text;
  110.        
  111. }
复制代码

所有资料51hei提供下载:
24小时计时器.rar (83.56 KB, 下载次数: 19)
回复

使用道具 举报

ID:575093 发表于 2019-7-3 12:21 来自手机 | 显示全部楼层
挺好啊
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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