| 程序功能:計時器。兩位led顯示秒種,另兩位顯示分鐘,用小數(shù)點隔開。 最大量程1小時。按鍵一下開始計時,第二下停止計時,第三下清零。 程序源碼: #include<reg52.h> #define uchar unsigned char sbit s1 = P2^0; sbit s2 = P2^1; sbit s3 = P2^2; sbit s4 = P2^3; sbit en = P2^5; sbit Key1= P3^2;//對應(yīng)學習板S1按鍵 uchar second,minute,clock,number; //數(shù)碼管顯示 uchar dis[10]={ 0x84, // 0 0xBD, // 1 0xE0, // 2 0xB0, // 3 0x99, // 4 0x92, // 5 0x82, // 6 0xBC, // 7 0x80, // 8 0x90 // 9 }; void timer0_init() { EA=1; ET0=1; TMOD=0x01;//工作方式1 //定時50ms TL0=0x00; TH0=0x4C; TR0=0; } void timer0() interrupt 1 { TF0=0; TL0=0x00; TH0=0x4C; ++clock; } void Delay(int m) { while(--m); } void display() { s1=0; P0=dis[minute/10]; Delay(100); s1=1; s2=0; P0=(dis[minute%10]&0x7F);//顯示小數(shù)點,以區(qū)分分鐘和秒鐘 Delay(100); s2=1; s3=0; P0=dis[second/10]; Delay(100); s3=1; s4=0; P0=dis[second%10]; Delay(100); s4=1; } void main() { second=0; minute=0; clock=0; number=0; Key1=1; timer0_init(); en=0; while(1) { if(!Key1) { ++number; if(1==number) TR0=1; if(2==number) TR0=0; while(!Key1); } if(20==clock)//到一秒 { clock=0; ++second; } if(60==second)//到一分 { second=0; ++minute; } if((60==minute) (number>=3))//超出量程或者清零命令 { second=0; minute=0; clock=0; number=0; } display(); } }
文件名:計時器(led顯示).rar |