#include <reg51.h>//<stc15.h>
#define uchar unsigned char
#define uint unsigned int
void BCSegNum();
void XSSegNum();
void DelayMs(uint n);
uchar Second=59, Hour=8, Minute=13;
static uchar Counter;
uchar flag0=0;
sbit dp=P2^7;
SegNum[]={1,2,3,4};//四个数码管
SegCode[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
BitCode[]={0x01,0x02,0x04,0x08};
void main()
{
TMOD = 0x01; //16位不自动重装,方式1
TH0 = 0x4c; //50ms
TL0 = 0x00;
ET0 = 1;
EA = 1;
TR0 = 1;
EX0 = 1;
EX1 =1;
IT0 = 1;
IT1 = 1;
P1 = 0;
while(1)
{
BCSegNum();
XSSegNum();
}
}
void BCSegNum()
{
SegNum[3] = Hour/10; //时十位
SegNum[2] = Hour%10;
SegNum[1] = Minute/10;
SegNum[0] = Minute%10;
}
void XSSegNum()
{
uchar i;
for(i=0;i<4;i++)
{
P2=SegCode[SegNum[i]];
P1=BitCode[i];
DelayMs(3);
P1=0x00;
}
}
void T0_Isr(void)interrupt 1
{
TH0 = 0x4c;
TL0 =0x00;
Counter++;
if(Counter<=10)dp = 0;
else dp = 1;
if(Counter==20)
{
Second++;
Counter = 0;
if(Second==60)
{
Second=0;
Minute++;
if(Minute==60)
{
Minute=0;
Hour++;
if(Hour==24)
{
Hour=0;
}
}
}
}
}
void DelayMs(uint n)
{
uchar j;
while(n--)
{
for(j=0;j<113;j++);
}
}
void Ex0Isr() interrupt 0 //选择键
{
TR0 = 0;
flag0++; //flag0=0无键按下
if(flag0==3)
{
flag0=0;
TR0 = 1;
}
}
void Ex1tsr() interrupt 2 //加键
{
if(flag0==1) //分加1
{
Minute++;
if(Minute==60)
Minute=0;
}
if(flag0==2) //时加1
{
Hour++;
if(Hour==24)
Hour=0;
}
} |