内含清零和暂停功能
单片机源程序如下:
- #include<reg51.h>
- unsigned char seg_data[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf}; //8段数码管显示代码,最后一位0xbf为横线
- unsigned char position[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //选位端循环
- int sum_position=0;
- int text=0;
-
- int s1=0,s2=0,m1=0,m2=0,h1=0,h2=0; //定义秒、分、时
- sbit pause=P3^3; //清零键
- unsigned int sum;
- void delaytime(unsigned int x); //声明调用函数‘延时’
- void diaplayscreen(unsigned int n); //声明调用函数‘显示’
-
- void main()
- {
- //打开中断(暂停)
- EA=1;EX0=1;IT0=1;
- while(1)
- {
- if(s1==9&&s2==5&&m1==9&&m2==5&&h1==3&&h2==2)
- {s1=0;s2=0;m1=0;m2=0;h1=0;h2=0; } //清零
- s1++; //秒+1
- if(s1>9) //计时‘秒’
- {
- s1=0;
- s2++;
- }
- if(s2>5)
- {
- s2=0;
- m1++;
- }
- if(m1>9) //计时‘分’
- {
- m1=0;
- m2++;
- }
- if(m2>5)
- {
- m2=0;
- h1++;
- }
- if(h1>9) //计时‘时’
- {
- h1=0;
- h2++;
- }
- /******8位LED显示******/
- if(text==0)
- {
- for(sum=0;sum<62;sum++) //‘sum’调节1s时长(可加变量x调节)
- { //显示时间
- diaplayscreen(s1);
- diaplayscreen(s2);
- diaplayscreen(10);
- diaplayscreen(m1);
- diaplayscreen(m2);
- diaplayscreen(10);
- diaplayscreen(h1);
- diaplayscreen(h1);
-
-
- if(sum_position==8) //置零选位端
- sum_position=0;
- }
- }
- else
- {
- while(1) //无限循环显示
- {
- diaplayscreen(s1);
- diaplayscreen(s2);
- diaplayscreen(10);
- diaplayscreen(m1);
- diaplayscreen(m2);
- diaplayscreen(10);
- diaplayscreen(h1);
- diaplayscreen(h1);
-
-
- if(sum_position==8)
- sum_position=0;
- if(text==0) //暂停结束
- break;
- }
- }
- if(pause==0)
- {s1=-1;s2=0;m1=0;m2=0;h1=0;h2=0;}
- }
- }
-
- void diaplayscreen(unsigned int n) //显示函数
- {
- P2=position[sum_position];//先选位在输入
- P0=seg_data[n];
- delaytime(1);
- sum_position++;
- }
- void delaytime(unsigned int x) //延时函数,最小精度1毫秒
- {
- unsigned char j;
- while(x--)
- {
- for(j=0;j<200;j++)
- {;}
- }
- }
- void zero() interrupt 0 //暂停
- {
- //s1=0;s2=0;m1=0;m2=0;h1=0;h2=0;
- text=~text;
-
- }
复制代码
所有资料51hei提供下载:
24小时计时器.rar
(83.56 KB, 下载次数: 19)
|