#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//特殊的位声明
sbit dula=P2^0;
sbit wela=P2^1;
sbit lm16=P2^2;
sbit dsas=P2^3;
sbit dsrw=P2^4;
sbit dsds=P2^5;
sbit dscs=P2^6;
sbit dq=P2^7;
sbit spk=P3^0;
sbit lcdrs=P3^1;
sbit lcdrw=P3^2;
sbit lcden=P3^3;
sbit key0=P3^4;
sbit key1=P3^5;
sbit key2=P3^6;
sbit key3=P3^7;
uchar wendu,day,date,mon,year,flag;
char shi,fen,miao;
uchar code table_dula[]=
{
0x3f,0x06,0x5b,0x4f, //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,
0x40,
};
uchar code table_wela[]=
{
//0x01,0x02,0x04,0x08,
//0x10,0x20,0x40,0x80, //位选择
0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f,
};
uchar code table_day[]=
{
31,28,31,30,31,30,31,31,30,31,30,31
};
void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//向ds12887写入地址和数据
void write_ds(uchar add,uchar date)
{
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P1=add;
dsas=0;
dsrw=0;
P1=date;
dsrw=1;
dsas=1;
dscs=1;
}
//读函数
uchar read_ds(uchar add)
{
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P1=add;
dsas=0;
dsds=0;
P1=0xff;
ds_date=P1;
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
void init()
{
dula=0;
wela=0;
write_ds(0x0a,0x20);
write_ds(0x0b,0x26);
// read_ds(0x0c);
}
void set_time()
{
write_ds(0,0); //秒
write_ds(2,36); //分
write_ds(4,20); //时
write_ds(6,7); //星期
write_ds(7,24); //日期
write_ds(8,10); //月份
write_ds(9,11); //年份
}
//这是时钟数码管的显示函数
void displaysz(uchar wei,uchar shishu)
{
uchar shi, ge;
ge=shishu%10;
shi=shishu/10;
wela=1;
P0=table_wela[wei];
wela=0;
P0=0x00;
dula=1;
P0=table_dula[ge];
dula=0;
P0=0xff;
delay(5);
wela=1;
P0=table_wela[wei-1];
wela=0;
P0=0x00;
dula=1;
P0=table_dula[shi];
dula=0;
P0=0xff;
delay(5);
}
//温度的显示
void displaywd(uchar wendu)
{
uchar wendu0,wendu1;
wendu0=wendu%10;
wendu1=wendu/10;
wela=1;
P0=table_wela[2];
wela=0;
P0=0x00;
dula=1;
P0=0x39;
dula=0;
P0=0xff;
delay(5);
wela=1;
P0=table_wela[1];
wela=0;
P0=0x00;
dula=1;
P0=table_dula[wendu0];
dula=0;
P0=0xff;
delay(5);
wela=1;
P0=table_wela[0];
wela=0;
P0=0x00;
dula=1;
P0=table_dula[wendu1];
dula=0;
P0=0xff;
delay(5);
}
void keyscan()
{
uchar tt; //tt
if(key0==0) //键盘检测
{
delay(5); //延时
if(key0==0) //再次检测确认是否被按下
{
tt++;
delay(5);
flag=1;
while(!key0); //如果不松手,则进入死循环,即一直等待松手
if(tt!=0)
{
while(tt==1) //调节分钟
{
displaysz(6,fen);
if(key1==0)
{
delay(5);
while(!key1);
{
fen++;
if(fen==60)
fen=0;
}
}
if(key2==0)
{
delay(5);
while(!key2);
{
fen--;
if(fen==-1)
fen=59;
}
}
if(key0==0)
break;
}
while(tt==2) //调节小时
{
displaysz(4,shi);
if(key1==0)
{
delay(5);
while(!key1);
{
shi++;
if(shi==24)
shi=0;
}
}
if(key2==0)
{
delay(5);
while(!key2);
{
shi--;
if(shi==-1)
shi=23;
}
}
if(key0==0)
break;
}
while(tt==3) //调节秒
{
displaysz(6,miao);
if(key1==0)
{
delay(5);
while(!key1);
miao++;
if(miao==60)
miao=0;
}
if(key2==0)
{
delay(5);
while(!key2);
miao--;
if(miao==-1);
miao=59;
}
if(key0==0)
break;
}
while(tt==4) //调节月份
{
displaysz(4,mon);
if(key1==0)
{
delay(5);
while(!key1);
mon++;
if(mon==13)
mon=1;
}
if(key2==0)
{
delay(5);
while(!key2);
mon--;
if(mon==0)
mon=12;
}
if(key0==0)
break;
}
while(tt==5) //调节日期
{
displaysz(6,date);
if(key1==0)
{
delay(5);
while(!key1);
date++;
if(date==table_day[mon-1]+1)
date=1;
}
if(key2==0)
{
delay(5);
while(!key2);
date--;
if(date==0)
date=table_day[mon-1];
}
if(key0==0)
break;
}
if(tt==6) //确认键,并且最后退出循环
{
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
write_ds(6,day);
write_ds(7,date);
write_ds(8,mon);
flag=0;
tt=0;
}
}
}
}
}
void main()
{
init();
wendu=28;
set_time();
while(1)
{
keyscan();
displaywd(wendu);
if(flag==0)
{
shi=read_ds(4);
fen=read_ds(2);
displaysz(6,fen);
displaysz(4,shi);
}
}
}
/*
void keyscan()
{
rd=0;
if(flag1==1)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
flag1=0;
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
flag1=0;
}
}
}
if(s1==0)
{
delay(5);
if(s1==0)
{ s1num++;
flag=1;
flag1=0;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
flag=0;
write_ds(0,miao);
write_ds(2,fen);
write_ds(4,shi);
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(1);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
if(s3==0)
{
delay(1);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
if(miao==0)
{
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
}
}
}
}
} */
一周热门 更多>