#include <REG51.H>
#define uchar unsigned char
#define uint unsigned int
uchar code SEG7[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar code WEEK_SEG7[8]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8};
uchar ACT[8]={0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff};
uchar deda,sec,min,hour,week=1;
uchar cnt;
void init(void)
{
TMOD=0x11;
TH0=-(50000/256);
TL0=-(50000%256);
ET0=1;
TR0=1;
TH1=-(1000/256);
TL1=-(1000%256);
ET1=1;
TR1=1;
EA=1;
}
void time0(void) interrupt 1
{
TH0=-(50000/256);
TL0=-(50000%256);
deda++;
}
void time1(void) interrupt 3
{
TH1=-(1000/256);
TL1=-(1000%256);
if(++cnt>7)cnt=0;
switch (cnt)
{
case 0:P0=SEG7[sec%10];P1=ACT[cnt];break; 1ms 显示一位
case 1:P0=SEG7[sec/10];P1=ACT[cnt];break;
case 2:P0=SEG7[min%10];P1=ACT[cnt];break;
case 3:P0=SEG7[min/10];P1=ACT[cnt];break;
case 4:P0=SEG7[hour%10];P1=ACT[cnt];break;
case 5:P0=SEG7[hour/10];P1=ACT[cnt];break;
case 6:P0=0xff;P1=0xfe;break;
case 7:P0=WEEK_SEG7[week];P1=ACT[cnt];break;
default:break;
}
}
void conv(void)
{
if(deda>=20){deda=0;sec++;}
if(sec>=60){sec=0;min++;}
if(min>=60){min=0;hour++;}
if(hour>=24){hour=0;week++;}
if(week>7){week=1;}
}
void delay(uint k)
{
uint data i,j;
for(i=0;i<k;i++){
for(j=0;j<121;j++)
{;}}
}
uchar scan_key(void)
{
uchar temp;
P2=0xfe;
temp=P2;
if(temp!=0xfe)
{delay(20);
temp=P2;
if(temp!=0xfe)return temp;
}
return 0;
}
void main(void)
{ uchar key_flag;
init();
while(1)
{
conv();
key_flag=scan_key();
switch(key_flag)
{
case 0xee:if(++min>59)min=0;delay(300);break; 0-59
case 0xde:if(++hour>23)hour=0;delay(300);break; 0-23
case 0xbe:if(++week>7)week=1;delay(300);break; 1-7
default:break;
}
}
}
文章评论(0条评论)
登录后参与讨论