您的位置 首页 产品

51单片机 + ds12c887时钟芯片 开发的电子时钟

原文地址:51单片机+ds12c887时钟芯片开发的电子时钟作者:紫暮秋风主要代码如下voiddelay(intz){intx,y;for(x=z;x>0;x–)for(y=110;

原文地址:51单片机+ds12c887时钟芯片开发的电子时钟作者:紫暮秋风

首要代码如下

void delay(int z)
{
int x,y;
for(x=z;x>0;x–)
for(y=110;y>0;y–);
}

void write_com(char com) //写指令操作
{
rs=0;
lcden=0; //这一步万万不可少,不然不出成果
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}

void didi()
{
beep=0;
delay(50);
beep=1;
delay(100);
beep=0;
delay(50);
beep=1;
}
void write_data(char date) //写指令操作
{
rs=1;
lcden=0; //这一步万万不可少,不然不出成果
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0; //运用结束后必定要开释使能端 即置零

}

void init() //初始化函数
{
char num;
EA=1;
EX0=1;
IT0=1;
wela=0;
dula=0;
lcden=0;
set_alarm(17,13,30);
write_ds(0x0B,0x26);
read_ds(0x0c);
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_data(table1[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<10;num++)
{
write_data(table2[num]);
delay(5);
}

}

void write_time(char clock,char address1)
{
char ge,shi;
shi=clock/10;
ge=clock%10;
write_com(0x80+0x40+address1);
write_data(0x30+shi);
delay(10);
write_data(0x30+ge); //记住此处必定要加0x30 由于char型数字+0x30才能在液晶上显现出来,别的字符如a,b等可直接显现
} //例如write_data(a) write_data(5+0x30)才是对的//不能写write_data(a)或write_data(5)

void keyscan1() //加法检测
{
if(s17==0)
{
delay(50);
if(s17==0&flag1==1)
{
flag1=0;
}
if((s17==0)&&(s13num==1))
{
if(miao==59)
{
miao=0;
}
miao++;
write_time(miao,0x08);
}

if((s17==0)&&(s13num==2))
{
if(fen==59)
{
fen=0;
}

fen++;
write_time(fen,0x05);
}

if((s17==0)&&(s13num==3))
{
if(shi==23)
{
shi=0;
}
shi++;
write_time(shi,0x02);
}

while(!s17);
}

}

void keyscan2() //减法检测
{
if(s21==0)
{
delay(50);

if(s21==0&flag1==1)
{
flag1=0;
}
if((s21==0)&&(s13num==1))
{
if(miao==0)
{
miao=59;
}
miao–;
write_time(miao,0x08);
}

if((s21==0)&&(s13num==2))
{
if(fen==0)
{
fen=59;
}

fen–;
write_time(fen,0x05);
}

if((s21==0)&&(s13num==3))
{
if(shi==0)
{
shi=23;
}
shi–;
write_time(shi,0x02);
}

while(!s21);
}
}

void keyscan() //复位键检测
{
rd=0;
if(s13==0)
{
delay(5);
if(s13==0)
{
flag=1;
TR0=0;
s13num++;
}
while(!s13);
if(flag1==1)
{
s13num=4;
flag1=0;
}
write_com(0x0f);
}

if(s13num==1)
{
write_com(0x80+0x40+0x08);
}
if(s13num==2)
{
write_com(0x80+0x40+0x05);
}
if(s13num==3)
{
write_com(0x80+0x40+0x02);
}
if(s13num==4)
{
s13num=0;
flag=0;
write_com(0x0c);
write_ds(4,shi);
write_ds(2,fen);
write_ds(0,miao);
}

keyscan1();
keyscan2();
}

void write_ds(char add,char date)
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsrw=0;
P0=date;
dsrw=1;
dsas=1;
dscs=1;
}

char read_ds(char add)
{
char ds_date;
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P0=add;
dsas=0;
dsds=0;
P0=0xff; //此处必定要给P0附0xff
ds_date=P0;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}

void set_time()
{
write_ds(4,15);
write_ds(2,03);
write_ds(0,50);
}

void set_alarm(char ashi,char afen,char amiao)
{
write_ds(1,amiao);
write_ds(3,afen);
write_ds(5,ashi);

}

void main()
{

init();
while(1)
{
keyscan();
if(flag1==1)
didi();
if(flag==0)
{
miao=read_ds(0);
fen=read_ds(2);
shi=read_ds(4);
write_time(miao,0x08);
write_time(fen,0x05);
write_time(shi,0x02);
}

}

}

void ext0() interrupt 0
{
char c;
beep=0;
c=read_ds(0x0C);
flag1=1;
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/xinpin/chanpin/261575.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部