注:mshi秒的十位 fshi 分的十位 sshi分的十位 key2 秒分时的加 KEY3秒分时的减 key4时钟开启暂停 key5清零 如何在加一个按键 能切换成闹钟模式按key2 3可调定时 再按一下换成时钟 切换成闹钟的时候不影响时钟的运行 时钟到闹钟设定的时间 小灯亮 我有写了一个函数去设置 但是不对 #include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void delayms(uint);
uchar num1,a,b,c,d;
uchar codetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit key5=P3^4; sbit key6=P3^5;
void delayms(uint xms)
{
uinti,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display()
{
int mshi,mge,fshi,fge,sshi,sge;
if(num1==20)
{
num1=0;
a++;
if(a==60)
{
a=0;
b++;
}
if(b==60)
{
b=0;
c++;
}
if(c==24)
{
c=0;
}
}
mshi=a/10;
mge=a%10;
fshi=b/10;
fge=b%10;
sshi=c/10;
sge=c%10;
P1=table[mge];
P2=0x7f;
delayms(1);
P2=0xff;
P1=table[mshi];
P2=0xbf;
delayms(1);
P2=0xff;
P1=0X40;
P2=0xdf;
delayms(1);
P2=0xff;
P1=table[fge];
P2=0xef;
delayms(1);
P2=0xff;
P1=table[fshi];
P2=0xf7;
delayms(1);
P2=0xff;
P1=0x40;
P2=0xfb;
delayms(1);
P2=0xff;
P1=table[sge];
P2=0xfd;
delayms(1);
P2=0xff;
P1=table[sshi];
P2=0xfe;
delayms(1);
P2=0xff;
}
void keyscan()
{
if(key1==0)
{
delayms(10);
if(key1==0) //选择时分秒
{
d++;
if(d==4)
d=0;
}
while(!key1) display(); //等待按键释放
}
if(d==1) //选择调秒
{
if(key2==0) //key2 (++)
{
delayms(10);
if(key2==0)
{
a++;
if(a==60) //当到60时重新归0
a=0;
while(!key2)display();
}
}
if(key3==0) //key3 (--)
{
delayms(10);
if(key3==0)
{
if(a==0)
a=60;
a--;
while(!key3)display();
}
}
}
if(d==2) //选分
{
if(key2==0)
{
delayms(10);
if(key2==0)
{
b++;
if(b==60)
b=0;
while(!key2)display();
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
if(b==0)
b=60;
b--;
while(!key3)display();
}
}
}
if(d==3) //选时
{
if(key2==0)
{
delayms(10);
if(key2==0)
{
c++;
if(c==24)
c=0;
while(!key2)display();
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
if(c==0)
c=24;
c--;
while(!key3)display();
}
}
}
if(key4==0)
{
delayms(10);
if(key4==0)
{
TR0=~TR0; //启动或停止定时器
while(!key4)display();
}
}
if(key5==0)
{
delayms(10);
if(key5==0)
{
a=0;
b=0;
c=0;
while(!key5)display();
}
}
}
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1;
while(1)
{
keyscan();
display();
}
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256; //重装初值
TL0=(65536-45872)%256;
num1++;
} #include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void delayms(uint);
uchar num1,a,b,c,d;
uchar codetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit key5=P3^4;
void delayms(uint xms)
{
uinti,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void display()
{
int mshi,mge,fshi,fge,sshi,sge;
if(num1==20)
{
num1=0;
a++;
if(a==60)
{
a=0;
b++;
}
if(b==60)
{
b=0;
c++;
}
if(c==24)
{
c=0;
}
}
mshi=a/10;
mge=a%10;
fshi=b/10;
fge=b%10;
sshi=c/10;
sge=c%10;
P1=table[mge];
P2=0x7f;
delayms(1);
P2=0xff;
P1=table[mshi];
P2=0xbf;
delayms(1);
P2=0xff;
P1=0X40;
P2=0xdf;
delayms(1);
P2=0xff;
P1=table[fge];
P2=0xef;
delayms(1);
P2=0xff;
P1=table[fshi];
P2=0xf7;
delayms(1);
P2=0xff;
P1=0x40;
P2=0xfb;
delayms(1);
P2=0xff;
P1=table[sge];
P2=0xfd;
delayms(1);
P2=0xff;
P1=table[sshi];
P2=0xfe;
delayms(1);
P2=0xff;
}
void keyscan()
{
if(key1==0)
{
delayms(10);
if(key1==0) //选择时分秒
{
d++;
if(d==4)
d=0;
}
while(!key1) display(); //等待按键释放
}
if(d==1) //选择调秒
{
if(key2==0) //key2 (++)
{
delayms(10);
if(key2==0)
{
a++;
if(a==60) //当到60时重新归0
a=0;
while(!key2)display();
}
}
if(key3==0) //key3 (--)
{
delayms(10);
if(key3==0)
{
if(a==0)
a=60;
a--;
while(!key3)display();
}
}
}
if(d==2) //选分
{
if(key2==0)
{
delayms(10);
if(key2==0)
{
b++;
if(b==60)
b=0;
while(!key2)display();
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
if(b==0)
b=60;
b--;
while(!key3)display();
}
}
}
if(d==3) //选时
{
if(key2==0)
{
delayms(10);
if(key2==0)
{
c++;
if(c==24)
c=0;
while(!key2)display();
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
if(c==0)
c=24;
c--;
while(!key3)display();
}
}
}
if(key4==0)
{
delayms(10);
if(key4==0)
{
TR0=~TR0; //启动或停止定时器
while(!key4)display();
}
}
if(key5==0)
{
delayms(10);
if(key5==0)
{
a=0;
b=0;
c=0;
while(!key5)display();
}
}
}
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1; //开总中断
ET0=1; //开定时器0中断
TR0=1;
while(1)
{
keyscan();
display();
}
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256; //重装初值
TL0=(65536-45872)%256;
num1++;
}
|