秒表計(jì)時(shí)功能(模仿)單片機(jī)程序
下面這個(gè)仿真效果圖,大家可按上面的電路來制作
/**//模仿秒表計(jì)時(shí)功能,獨(dú)立按鍵可控制計(jì)時(shí)數(shù)字的“暫停/走動”。(程序沒有考慮計(jì)時(shí)精確度控制)/*P2.2~P2.4與138譯碼器輸入口連接/*P1.0連接獨(dú)立按鍵/*P0口輸出數(shù)據(jù)***/#includeAT89X51.H>code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //共陰數(shù)碼管 0-9 '-' '熄滅‘表unsigned int count,a,b,sec,min,hour; bit c_flag; // 暫停/計(jì)時(shí) 標(biāo)志 /*****T1初始化*****/void timer_1(void){TH1=0x0ec;TL1=0x078;TR1=1;}/*****數(shù)據(jù)計(jì)數(shù)******/void time_count(viod){if(hour==60){sec=0,min=0,hour=0;}else if(sec!=59) sec++;else if(min!=59) {sec=0,min++;}else {min=0,hour++;}}/******數(shù)字調(diào)整*******/void get_count(unsigned int x){a=x/10;b=x%10;}/*******按鍵檢測********/void key_tach(void){int i,j;j=P10x01;if(j==0){for(i=0;i=200;i++);j=P10x01;if(j==0) {if(c_flag==1) c_flag=0;else c_flag=1;}}}/*******顯示程序********/void display(viod){ int flag;P0=0x00;flag=count%8;switch(flag){case 0:P2=flag*4;get_count(hour);P0=tab[a];break;case 1:P2=flag*4;P0=tab[b];break;case 2:P2=flag*4;P0=tab[10];break;case 3:P2=flag*4;get_count(min);P0=tab[a];break;case 4:P2=flag*4;P0=tab[b];break;case 5:P2=flag*4;P0=tab[10];break;case 6:P2=flag*4;get_count(sec);P0=tab[a];break;case 7:P2=flag*4;P0=tab[b];break;default:return;}}/*******中斷服務(wù)程序********/void int1() interrupt 3{key_tach();if(c_flag==0){if(count==7) {count=0;time_count();}else count++;}else{if(count==7) {count=0;}else count++;} display();timer_1();}/******主程序*****/void main(){c_flag=0;count=0;sec=0;min=0;hour=0;TMOD=0x10;EA=1;ET1=1;timer_1();while(1);}
評論