51單片機時鐘秒表課程設(shè)計
void Delayms(int ms)
{
while(ms)
{
unsigned char i;
ms--;
for (i=0; i<240; i++);
}
}
void display()
{
char v,c;
P3=0x11;
v=Leds[Ledc];
c=LEDTAB[v & 0x0f];
if (v&0x10) c|=0x80;
P2=c;
P3=Ledc;
Ledc++;
if (Ledc>=8) Ledc=0;
}
void readkey() //讀按鍵
{
if(start==0) isStart = 1; //真為開始
if(stop==0) isStart = 0; //假為停止
if(key0 == 0) num_count = stu_num0; //選擇曾路榮
if(key1 == 0) num_count = stu_num1;//選擇張洪榮
if(key2 == 0) num_count = stu_num2;
if(key3 == 0) num_count = stu_num3;
if(key4 == 0) num_count = stu_num4;
if(key5 == 0) num_count = stu_num5;
}
void t0int() interrupt 1 //1ms定時器中斷服務函數(shù)
{
TH0=(-1000)>>8;
TL0=-1000;
display();
readkey();
T1msc++;
if (isStart) //開始倒計時
{
if ((T1msc%100)==0) //滿 0.1秒
{
temp--;
if(temp==-1)
{
num_count--;
temp = 10; //0.1秒恢復9
}
}
if(num_count == -1) //
{
num_count = 99;
num--;
if(num == -1) num = 99;
}
}
if (T1msc>=1000)
{
T1msc=0;
}
}
void NumToLeds()
{
Leds[1]=num/10%10;
Leds[2]=num%10;
Leds[3]=num_count/10%10;
Leds[4]=num_count%10+0x10;
Leds[5]=temp; //0.1秒顯示
}
void main()
{
Delayms(10);
TMOD=0x01;
TR0=1;
ET0=1;
EA=1;
while(1)
{
NumToLeds(); //送數(shù)據(jù)
}
}
評論