电子时钟(4个按键)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P1^0;
sbit key2=P1^1;
sbit key3=P1^2;
sbit key4=P1^3;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delayms(uint);
uchar second,minute,hour,fenge,numt0,flag1,flag2,flag3;
void display0 ()
{
dula=1;
P0=0x40;
dula=0;
P0=0xff;
wela=1;
P0=0xdb;
wela=0;
delayms(5);
P0=0x00;
}
void display1(uchar second)
{
uchar shi,ge;
shi=second/10;
ge=second%10;

dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xbf;
wela=0;
delayms(5);
P0=0x00;

dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0x7f;
wela=0;
delayms(5);
P0=0x00;
}
void display2(uchar minute)
{
uchar shi,ge;
shi=minute/10;
ge=minute%10;

dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delayms(5);
P0=0x00;

dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delayms(5);
P0=0x00;
}
void display3(uchar hour)
{
uchar shi,ge;
shi=hour/10;
ge=hour%10;

dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delayms(5);
P0=0x00;

dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delayms(5);
P0=0x00;
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void init()
{
wela=0;
P0=0Xf3;
dula=0;
wela=1;
P0=0Xfe;
wela=0;
second=0;
minute=0;
hour=0;
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
}
void keyscan()
{
if(key1==0)
{
delayms(5);
if(key1==0)
{
flag1++;
if(flag1%2==0)
flag2=1;
else flag2=0;
while(!key1)
{
display0();
display1(second);
display2(minute);
display3(hour);
}
second++;
if(second==60)
{
second=0;
}
TR0=~TR0;
}
}
if(flag2==1)
{
if(key2==0)
{
delayms(5);
if(key2==0)
{
while(!key2)
{
display0();
display1(second);
display2(minute);
display3(hour);
}
second++;
if(second==60)
second=0;
}
}
if(key3==0)
{
delayms(5);
if(key3==0)
{
while(!key3)
{
display0();
display1(second);
display2(minute);
display3(hour);
}
minute++;
if(minute==60)
minute=0;
}
}
if(key4==0)
{
delayms(5);
if(key4 ==0)
{
while(!key4)
{
display0();
display1(second);
display2(minute);
display3(hour);
}
hour++;
if(hour==24)
hour=0;
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
if(flag1>=1)
{
display0();
display1(second);
display2(minute);
display3(hour);
}
}
}
void T0_time() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;

numt0++;
if(numt0==20)
{
numt0=0;
second++;
if(second==60)
{
second=0;
minute++;
{
if(minute==60)
{
minute=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
}

相关文档
最新文档