好友
阅读权限10
听众
最后登录1970-1-1
|
答案:#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6;
sbit wela=P2^7;
void delayms(uchar);
uint shi=12,fen=0,miao=0,num,key,y,flag2,flag3;
bit flag,flag1;
uchar code tb1[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar tb2[6];
uchar mm[20];
void keyscan()
{
uchar temp,temph,templ,k;
P3=0xf0;
temp=P3;
temph=temp&0xf0;
if(temph!=0xf0)
{
delayms(10);
temp=P3;
temph=temp&0xf0;
if(temph!=0xf0)
{
P3=0x0f;
temp=P3;
templ=temp&0x0f;
k=temph^templ;
switch(k)
{
case 0xee:key=0;break;
case 0xde:key=1;break;
case 0xbe:key=2;break;
case 0x7e:key=3;break;
case 0xed:key=4;break;
case 0xdd:key=5;break;
case 0xbd:key=6;break;
case 0x7d:key=7;break;
case 0xeb:key=8;break;
case 0xdb:key=9;break;
case 0xbb:key=10;flag3=1;TR1=0;break; //设置键
case 0x7b:key=11;shi=fen=miao=0;TR1=0;break; //清零键
case 0xe7:key=12;flag2=3;break; //设置时
case 0xd7:key=13;flag2=2;break; //设置分
case 0xb7:key=14;flag2=1;break; //设置秒
case 0x77:key=15;TR1=1;flag2=0;mm[20]=0;break;//启动
}
if(flag2>=1)
{
mm[y=y+1]=key;
}
while(temph!=0xf0)
{
P3=0xf0;
temp=P3;
temph=temp&0xf0;
}
}
}
}
void display() //显示
{
uchar k,t=0x01;
tb2[0]=shi/10;
tb2[1]=shi%10;
tb2[2]=fen/10;
tb2[3]=fen%10;
tb2[4]=miao/10;
tb2[5]=miao%10;
for(k=0;k<6;k++)
{
dula=1;
if(k==1|k==3)
P0=tb1[tb2[k]]|0x80;
else P0=tb1[tb2[k]];
dula=0;
P0=0xff;
wela=1;
P0=~t;
wela=0;
delayms(10);
t=t<<1;
}
}
void shezhi() //设置
{
if(flag2==1&&flag3==1)
{
miao=mm[y]+mm[y-1]*10;
}
if(flag2==2&&flag3==1)
{
fen=mm[y]+mm[y-1]*10;
}
if(flag2==3&&flag3==1)
{
shi=mm[y]+mm[y-1]*10;
}
}
void delayms(uchar xms) //延时
{
uchar i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void main()
{
TMOD=0x20;//定时器T1工作方式2
TH1=(256-250);
TL1=(256-250);//周期为250us
EA=1;
ET1=1;
TR1=0;
while(1)
{
display();
if(flag2>=1)
shezhi();
keyscan();
}
}
void t1_time() interrupt 3 //定时器中断
{
num++;
if(num==4000)//4000*250us=1s
{
num=0;
if(flag==0) //正计时
{
miao++;
if(miao>=60)
{
miao=0;
fen++;
}
if(fen>=60)
{
fen=0;
shi++;
}
if(shi>=24)
{
shi=0;
P0=0x00;
}
}
if(flag==1) //倒计时
{
miao--;
if(miao==0)
{
if(fen!=0)
{
fen--;
miao=60;
miao--;
}
else
if(shi!=0)
{
shi--;
fen=60;
fen--;
miao=60;
miao--;
}
else
if(miao==0&&fen==0&&shi==0)
TR1=0;
}
}
}
}
在这里也谢谢大家对我的帮助。谢谢大家 |
|