完整代码如下
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit Key1=P2^0;
sbit Key2=P2^1;
sbit Key3=P2^2;
sbit Key4=P2^3;
sbit LED=P2^6;
sbit SPK=P2^7;
void delay(uint ms);
uchar count,shi,ge,countdown,state;
uchar Buf[2]={0};
uchar code DispBitCode[] = {0xfe,0xfd};
uchar code DispCode[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF} ;
uchar Count=0;
uint time=0;
uint show_time=0;
bit start = 0;
bit alarm = 0;
void Delay(uint x)
{
while(--x);
}
void DisPlay (uchar *DispBuf)
{
uchar k;
for(k=0; k<2; k++)
{
P1=DispBitCode[k];
P0=DispCode[DispBuf[k]];
Delay (500);
}
}
void KeyScan (void)
{
{
if(Key1==0)
{
delay(15);
TR0=0;
state=1;
}
if(state==1)
{
delay(15);
}
}
if(Key2==0)
{
delay(15);
if(Key2==0)
{
if(start==0)
{
if(time<59)
time++;
else
time=0;
show_time=time;
}
while(!Key2);
}
}
if(Key3==0)
{
delay(15);
if(Key3==0)
{
if(start==0)
{
if(time>0)
time--;
else
time=59;
show_time=time;
}
while(!Key3);
}
}
if(Key4==0)
{
delay(15);
TR0=1;
state=0;
}
}
void TimerInit (void)
{
TMOD=0x11;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TH1=0xfe;
TL1=0x00;
TR1=0;
ET1=1;
TR0=0;
ET0=1;
EA=1;
}
void openAlarm (void)
{
TR1=1;
LED=0;
}
void closeAlarm(void)
{
TR1 = 0;
LED=1;
}
void Time_Init()
{
LED=1;
time=10;
show_time =time;
while(1)
{
Buf[0]= show_time%10;
Buf[1]= show_time/10;
DisPlay(Buf);
KeyScan();
if(1 == alarm)
{
TR0 = 0;
openAlarm();
}
else
closeAlarm();
}
return;
}
void Timer0(void) interrupt 1
{
TH0=(5535 - 50000)/256;
TL0=(65535 - 50000)%256;
count++;
if(20==Count)
{
Count = 0;
show_time--;
if(0==show_time)
{
alarm=1;
}
}
}
void Timerl(void) interrupt 3
{
TH1=0xfd;
TL1=0x00;
SPK=~SPK;
} |