用AT89C52芯片作為主控芯片實現計時/定時的功能,使用單片機內置的定時器以中斷的方式產生以秒為單位的時間信號,產生的時間數據通過計算分成四位在數碼管上以逐位掃描的方式顯示出來,由于掃描速度足夠快,因此看上去四位數字是同時顯示的,另外還用了七個io口對應七個功能按鍵,按下去時可以拉低引腳電平,通過檢測電平變化來判斷按鍵是否被按下,七個按鍵可以實現時間的調整以及計時暫停繼續清零等功能。
QQ截圖20171104161601.png (266.18 KB, 下載次數: 54)
下載附件
2017-11-4 16:17 上傳
QQ截圖20171104161638.png (108.48 KB, 下載次數: 35)
下載附件
2017-11-4 16:17 上傳
- #include<reg52.h>
- sbit w1 = P3 ^ 6;
- sbit w2 = P1 ^ 5;
- sbit w3 = P1 ^ 4;
- sbit w4 = P1 ^ 3;
- sbit a = P3 ^ 5;
- sbit b = P1 ^ 2;
- sbit c = P1 ^ 7;
- sbit d = P3 ^ 2;
- sbit e = P3 ^ 3;
- sbit f = P3 ^ 4;
- sbit g = P1 ^ 6;
- sbit h = P3 ^ 1;
- sbit set = P2 ^ 5;
- sbit stop = P2 ^ 7;
- sbit clear = P2 ^ 6;
- sbit up = P2 ^ 2;
- sbit down = P2 ^ 1;
- sbit right = P2 ^ 3;
- sbit left = P2 ^ 0;
- sbit led = P3 ^ 7; //各io口設置
- bit settime = 0; //是否進入時間設定模式的判斷變量
- bit pause = 0; //是否暫停的標志變量
- int time = 3599; //時間數據,初始值為59:59
- unsigned char num; //定時器中斷次數記錄變量
- unsigned char bj1 = 0, bj2 = 0, bj3 = 1,bj4=0; //標記變量
- void xianshi( int i ) { //數碼管位選函數,顯示0~9
- switch ( i ) {
- case 0:
- a = b = c = d = e = f = g = h = 0;
- a = b = c = e = d = f = 1;
- break;
- case 1:
- a = b = c = d = e = f = g = h = 0;
- b = c = 1;
- break;
- case 2:
- a = b = c = d = e = f = g = h = 0;
- a = b = g = e = d = 1;
- break;
- case 3:
- a = b = c = d = e = f = g = h = 0;
- a = b = g = c = d = 1;
- break;
- case 4:
- a = b = c = d = e = f = g = h = 0;
- f = g = b = c = 1;
- break;
- case 5:
- a = b = c = d = e = f = g = h = 0;
- a = f = g = c = d = 1;
- break;
- case 6:
- a = b = c = d = e = f = g = h = 0;
- a = f = e = g = c = d = 1;
- break;
- case 7:
- a = b = c = d = e = f = g = h = 0;
- a = b = c = 1;
- break;
- case 8:
- a = b = c = d = e = f = g = h = 0;
- a = b = c = d = e = f = g = 1;
- break;
- case 9:
- a = b = c = d = e = f = g = h = 0;
- a = b = c = d = g = f = 1;
- break;
- }
- }
- void delay( int i ) //延時函數大概延時ims
- {
- int j;
- for ( i; i >= 0; i-- ) {
- for ( j = 110; j >= 0; j-- );
- }
- }
- void zijiang( void ) //數碼管自檢函數
- {
- int i;
- w1 = w2 = w3 = w4 = 0;
- a = b = c = d = e = f = g = h = 1;
- delay( 800 );
- a = b = c = d = e = f = g = h = 0;
- for ( i = 0; i <= 9; i++ ) {
- w1 = w2 = w3 = w4 = 0;
- xianshi( i );
- delay( 500 );
- }
- w1 = w2 = w3 = w4 = 1;
- bj3 = 0;
- }
- void display( int i ) //數碼管時間顯示函數
- {
- int w, x, y, z;
- w = ( ( i / 60 ) / 10 );
- x = ( ( i / 60 ) % 10 );
- y = ( ( i % 60 ) / 10 );
- z = ( ( i % 60 ) % 10 );
- w1 = w2 = w3 = w4 = 1;
- w1 = 0;
- xianshi( w );
- delay( 5 );
- w1 = 1;
- w2 = 0;
- xianshi( z );
- delay( 5 );
- w2 = 1;
- w3 = 0;
- xianshi( x );
- delay( 5 );
- w3 = 1;
- w4 = 0;
- xianshi( y );
- delay( 5 );
- w4 = 1;
- }
- void setdisplay( int i,int j ) //設置時間時數碼管時間顯示函數,帶有閃爍功能
- {
- int w, x, y, z;
- w = ( ( i / 60 ) / 10 );
- x = ( ( i / 60 ) % 10 );
- y = ( ( i % 60 ) / 10 );
- z = ( ( i % 60 ) % 10 );
- w1 = w2 = w3 = w4 = 1;
- w1 = 0;
- if(j==1)
- {
- bj4++;
- if(bj4>=50)
- {
- w1=1;
- if(bj4>=100)
- bj4=0;
- }
- }
- xianshi( w );
- delay( 5 );
- w1 = 1;
- w2 = 0;
- if(j==4)
- {
- bj4++;
- if(bj4>=50)
- {
- w2=1;
- if(bj4>=100)
- bj4=0;
- }
- }
- xianshi( z );
- delay( 5 );
- w2 = 1;
- w3 = 0;
- if(j==2)
- {
- bj4++;
- if(bj4>=50)
- {
- w3=1;
- if(bj4>=100)
- bj4=0;
- }
- }
- xianshi( x );
- delay( 5 );
- w3 = 1;
- w4 = 0;
- if(j==3)
- {
- bj4++;
- if(bj4>=50)
- {
- w4=1;
- if(bj4>=100)
- bj4=0;
- }
- }
- xianshi( y );
- delay( 5 );
- w4 = 1;
- }
- void T0_time()interrupt 1 //中斷產生時間信號
- {
- TH0 = 59;
- TL0 = 114; //重裝初值
- num++;
- }
- void main()
- {
- int j = 1;
- w1 = w2 = w3 = w4 = 1;
- TH0 = 59;
- TL0 = 146; //定時器設置及裝填初值
- EA = 1;
- ET0 = 1;
- led=0;
- while ( 1 )
- {
- if ( bj3 == 1 )
- zijiang(); //自檢函數,只運行一次
- while(bj2==1)
- {
- if ( set == 0 ) //設置函數,按下設置鍵進入函數
- {
- delay( 10 );
- if ( set == 0 )
- {
- while(!set); //按鍵防抖,并且防止按一次重復多次
- settime = 1;
- }
- }
- if ( settime == 1 )
- {
- setdisplay( time,j );
- if ( right == 0 ) //時間設置右移
- {
- delay( 10 );
- if ( right == 0 )
- {
- while ( !right );
- j++;
- if ( j == 5 )
- j = 1;
- }
- }
- setdisplay( time,j );
- if ( left == 0 ) //時間設置左移
- {
- delay( 10 );
- if ( left == 0 )
- {
- while ( !left );
- j--;
- if ( j == 0 )
- j = 4;
- }
- }
- setdisplay( time,j );
- if ( up == 0 ) //時間加
- delay( 10 );
- {
- if ( up == 0 )
- {
- while ( !up );
- switch ( j )
- {
- case 1:
- time = time + 600;
- if ( ( time / 600 ) > 5 )
- time = time - 3600;
- break;
- case 2:
- time = time + 60;
- if ( ( ( time / 60 ) % 10 ) == 0 )
- time = time - 600;
- break;
- case 3:
- time = time + 10;
- if ( ( ( time % 60 ) / 10 ) > 5 )
- time = time - 60;
- break;
- case 4:
- time++;
- if ( ( ( time % 60 ) / 10 ) == 0 )
- time = time - 10;
- break;
- }
- }
- }
- setdisplay( time,j );
- if ( down == 0 ) //時間減
- {
- delay( 10 );
- if ( down == 0 )
- {
- while ( !down );
- switch ( j )
- {
- case 1:
- if ( time / 600 == 0 )
- time = time + 3600;
- time = time - 600;
- break;
- case 2:
- if ( ( ( time / 60 ) % 10 ) == 0 )
- time = time + 600;
- time = time - 60;
- break;
- case 3:
- if ( ( ( time % 60 ) / 10 ) == 0 )
- time = time + 60;
- time = time - 10;
- break;
- case 4:
- if ( ( ( time % 60 ) % 10 ) == 0 )
- time = time + 10;
- time--;
- break;
- }
- }
- }
- setdisplay( time,j );
- if ( stop == 0 ) //開始按鈕,按下開始計時
- {
- delay( 10 );
- if ( stop == 0 )
- while ( !stop );
- bj2 = 0;
- }
- if ( clear == 0 ) //清零按鈕,按下時間清零
- {
- delay( 10 );
- if ( clear == 0 )
- {
- while ( !clear );
- time = 0;
- }
- }
- }
- }
- while ( bj2 == 0 ) //計時部分從59:59開始倒計時
- {
- TR0 = 1;
- display( time );
- if ( num >= 160 )
- {
- num = 0;
- time--;
- }
- if(time==0) //判斷即使是否結束
- led=1;
- TR0=0;
- if ( set == 0 ) //設置按鈕,按下進入設置模式
- {
- delay(10);
- if ( set == 0 )
- {
- while ( !set );
- bj2 = 1;
- TR0 = 0;
- }
- }
- if ( stop == 0 ) //暫停/開始按鈕,按下暫停或者開始計時
- {
- delay( 10 );
- if ( stop == 0 )
- { while(!stop);
- bj2=1-bj2;
- }
- }
- if ( bj2 == 0 )
- TR0 = 1;
- else
- TR0 = 0;
- }
- }
- }
復制代碼
|