內(nèi)含清零和暫停功能
0.png (11.84 KB, 下載次數(shù): 19)
下載附件
2018-12-1 02:01 上傳
單片機源程序如下:
- #include<reg51.h>
- unsigned char seg_data[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf}; //8段數(shù)碼管顯示代碼,最后一位0xbf為橫線
- unsigned char position[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //選位端循環(huán)
- 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); //聲明調(diào)用函數(shù)‘延時’
- void diaplayscreen(unsigned int n); //聲明調(diào)用函數(shù)‘顯示’
-
- 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’調(diào)節(jié)1s時長(可加變量x調(diào)節(jié))
- { //顯示時間
- 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) //無限循環(huán)顯示
- {
- 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) //暫停結(jié)束
- break;
- }
- }
- if(pause==0)
- {s1=-1;s2=0;m1=0;m2=0;h1=0;h2=0;}
- }
- }
-
- void diaplayscreen(unsigned int n) //顯示函數(shù)
- {
- P2=position[sum_position];//先選位在輸入
- P0=seg_data[n];
- delaytime(1);
- sum_position++;
- }
- void delaytime(unsigned int x) //延時函數(shù),最小精度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, 下載次數(shù): 19)
2018-11-29 18:49 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|