求!!!!!!单片机8051电子时钟设计!

2024-12-29 07:16:03
推荐回答(3个)
回答1:

单片机8051电子时钟设计 我有细聊

回答2:

hi我或留联系方式,我有

回答3:

我这个用单片机+DS12C887时钟芯片 制作的不怕掉电的带按键声音的时钟。用的是数码管显示,你可以自己改成1602液晶显示。原理图不好上,直接上程序了。程序是我调试通过的,可直接用。有问题再联系我。
#include
#define uint unsigned int
#define uchar unsigned char
uchar code table[]= {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0};
bit flag;
sbit dscs=P3^5;
sbit dswr=P3^6;
sbit dsre=P3^7;
sbit dsal=P3^1;
sbit J1=P0^0;
sbit J2=P0^1;
sbit J3=P0^2;
sbit J4=P0^3;
sbit D=P3^0;
char shi,fen,miao,temp,sett,setd,J2n,J3n;
char dshi,dfen,dmiao;
void delay(uchar);
void display(char,char,char,char);
void write_ds(uchar,uchar);
uchar read_ds(uchar);
void scakey();
void settime();
void dingshi();
void main()
{
EA=1;
EX0=1;
dshi=read_ds(0x4e);
dfen=read_ds(0x4f);
dmiao=read_ds(0x50);
// write_ds(0x0a,0x2f);//启动振荡器设置SQW端输出2HZ方波 //
write_ds(0x0a,0x0f);//关闭振荡器
write_ds(0x0b,0x2e);//设置二进制模式,24小时模式,方波允许模式//
// write_ds(0x04,23);
// write_ds(0x02,59);
// write_ds(0x00,57);
while(1)
{
miao=read_ds(0x00);
fen=read_ds(0x02);
shi=read_ds(0x04);
display(shi,fen,miao,0);
scakey();
settime();
dingshi();

}
}

void delay(uchar z)
{
uchar x,y;
for (x=z;x>0;x--)
for(y=96;y>0;y--);
}

void display(char shi,char fen,char miao,char temp) //显示时间//
{
uchar x,y,z;
z=1;
x=shi/10;
y=shi%10;
if(temp==3&&y==0&&flag==1)
y=16;
if(temp==0&&x==0)
x=16;
P0=table[x];
P2=0x7f;
delay(z);
P2=0xff;
P0=table[y]|0x80;
P2=0xbf;
delay(z);
x=fen/10;
y=fen%10;
if(temp==2&&y==0&&flag==1)
y=16;
if(temp==0&&x==0)
x=16;
P2=0xff;//显示小时//
P0=table[x];
P2=0xdf;
delay(z);
P2=0xff;
P0=table[y]|0x80;
P2=0xef;
delay(z);
x=miao/10;
y=miao%10;
if(temp==1&&y==0&&flag==1)
y=16;
P2=0xff;//显示分钟//
if(temp==0&&x==0)
x=16;
P0=table[x];
P2=0xf7;
delay(z);
P2=0xff;
P0=table[y];
P2=0xfb;
delay(z);
P2=0xff;//显示秒//
if(sett!=0)
{
P0=table[sett+9];
P2=0xfe;
delay(z);
P2=0xff;
}
if(setd!=0)
{
P0=table[13];
P2=0xfd;
delay(z);
P2=0xff;
P0=table[setd+9];
P2=0xfe;
delay(z);
P2=0xff;
}
P0=0xff;
}

void write_ds(uchar adds,uchar date)
{
dsal=1;
dswr=1;
dscs=0;
P1=adds;
dsal=0;
dswr=0;
P1=date;
dswr=1;
dsal=1;
dscs=1;

}

uchar read_ds(uchar adds)
{
uchar num;
dsal=1;
dsre=1;
dscs=0;
P1=adds;
dsal=0;
dsre=0;
P1=0xff;
dsre=1;
num=P1;
dsal=1;
dscs=1;
return num;
}

void scakey()
{

J1=P0^0;
if(J1==0)
{
delay(5);
if(J1==0)
{
//display(1);
// P1=0xfe;
// D=0;
// delay(50);
// D=1;
sett++;
while(J1==0)
{
J1=P0^0;

}

P0=0xff;
}
}
J2=P0^1;
if(J2==0)
{
delay(5);
if(J2==0)
{
// display(2);
// P1=0xfd;
// D=0;
// delay(50);
// D=1;
J2n++;
while(J2==0)
{
J2=P0^1;

}

P0=0xff;
}
}
J3=P0^2;
if(J3==0)
{
delay(5);
if(J3==0)
{
// display(3);
// P1=0xfb;
// D=0;
// delay(50);
// D=1;
J3n++;
while(J3==0)
{
J3=P0^2;

}

P0=0xff;
}
}
J4=P0^3;
if(J4==0)
{
delay(5);
if(J4==0)
{
//display(4);
//P1=0xf7;
//D=0;
//delay(50);
//D=1;
setd++;
while(J4==0)
{
J4=P0^3;

}

P0=0xff;
}
}
}

void settime()
{
while(sett!=0)
{
scakey();
display(shi,fen,miao,sett);
switch(sett)
{
case 1:
{
if(J2n==1)
{
J2n=0;
miao++;
if(miao==60)
miao=0;
}
if(J3n==1)
{
J3n=0;
miao--;
if(miao==-1)
miao=59;
}
break;
}
case 2:
{
if(J2n==1)
{
J2n=0;
fen++;
if(fen==60)
fen=0;
}
if(J3n==1)
{
J3n=0;
fen--;
if(fen==-1)
fen=59;
}
break;
}
case 3:
{
if(J2n==1)
{
J2n=0;
shi++;
if(shi==24)
shi=0;
}
if(J3n==1)
{
J3n=0;
shi--;
if(shi==-1)
shi=23;
}
break;
}
case 4:
sett=0; break;
default :break;
}
write_ds(4,shi);
write_ds(2,fen);
write_ds(0,miao);
}
}

void dingshi()
{
while(setd!=0)
{
D=1;
scakey();
display(dshi,dfen,dmiao,setd);
switch(setd)
{
case 1:
{
if(J2n==1)
{
J2n=0;
dmiao++;
if(dmiao==60)
dmiao=0;
}
if(J3n==1)
{
J3n=0;
dmiao--;
if(dmiao==-1)
dmiao=59;
}
break;
}
case 2:
{
if(J2n==1)
{
J2n=0;
dfen++;
if(dfen==60)
dfen=0;
}
if(J3n==1)
{
J3n=0;
dfen--;
if(dfen==-1)
dfen=59;
}
break;
}
case 3:
{
if(J2n==1)
{
J2n=0;
dshi++;
if(dshi==24)
dshi=0;
}
if(J3n==1)
{
J3n=0;
dshi--;
if(dshi==-1)
dshi=23;
}
break;
}
case 4:
setd=0; break;
default :break;
}
write_ds(5,dshi);
write_ds(3,dfen);
write_ds(1,dmiao);
write_ds(0x4e,dshi);
write_ds(0x4f,dfen);
write_ds(0x50,dmiao);
}
}

void interrupt0() interrupt 0
{
D=0;
read_ds(0x0c);

}