#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint countb=0,counta=0; // counta 為10ms計(jì)數(shù)器,countb為1s計(jì)數(shù)器,均為全局變量
sbit s1 = P2^0;
sbit s2 = P2^1;
sbit s3 = P2^2;
sbit s4 = P2^3;
sbit en = P2^5;
sbit speaker = P2^4;
sbit k1=P3^2;
sbit k2=P3^3;
uchar a1,a2,b1,b2;
code ledseg7[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //0~9不帶小數(shù)點(diǎn)的段碼表
// 0 1 2 3 4 5 6 7 8 9
code ledseg8[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
// 0 1 2 3 4 5 6 7 8 9 //0~9帶小數(shù)點(diǎn)的段碼表
/********延時函數(shù)********/
void Delay(uint xms) //延時程序,xms是形式參數(shù)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=100;j>0;j--); //此處分號不可少
}
/*******顯示函數(shù)********/
void display(uchar a,uchar b,uchar c,uchar d)
{
en = 1; //啟用數(shù)碼管信號
s1 = 0;
P0 = ledseg7[a];
Delay(5);
s1 = 1;
s2 = 0;
P0 = ledseg8[b];
Delay(5);
s2 = 1;
s3 = 0;
P0 = ledseg7[c];
Delay(5);
s3 = 1;
s4 = 0;
P0 = ledseg7[d];
Delay(5);
s4 = 1;
}
/********重置,暫停設(shè)置******/
void key()
{
if(k1==0) //開始,暫停
{
Delay(10);
if(k1==0)
{
while(!k1);
TR0=~TR0;
}
}
if(k2==0) //復(fù)位
{
Delay(10);
if(k2==0)
{
TH0=0xDC; TL0=0x00;
countb=0;
counta=0;
TR0=0;
while(k2!=0);
}
}
}
/********定時器T0中斷函數(shù)*******/
void timer0() interrupt 1
{
TH0=0xDC;TL0=0x00;
counta++; //計(jì)數(shù)值加1
if(counta==100) //若counta為99,(100*10ms=1s)
{
counta=0; // counta清0
countb++;
}
if(countb==30)
{
TR0=0;
s3=1;
counta=0,countb=0;
speaker=0;
Delay(500);
speaker=1;
Delay(500);
speaker=0;
Delay(500);
speaker=1;
}
}
void main()
{
P0=0xff;
TMOD=0x01; //定時器T0方式1
TH0=0xDC; TL0=0x00; //10ms定時初值
TR0=0;
EA=1;
ET0=1; //開總中斷,開定時器T0中斷,啟動定時器T0
while(1)
{
key();
b2=countb/10; //取出記數(shù)秒數(shù)的十位
b1=countb%10; //取出計(jì)數(shù)秒的個位
a2=counta/10; //取出記數(shù)百分秒數(shù)的十位
a1=counta%10; //取出計(jì)數(shù)百分秒的個位
display(b2,b1,a2,a1); //調(diào)顯示函數(shù)
}
}