#include <reg52.h>
#include <intrins.h>
sbit lcdrs=P2^5;
sbit lcden=P2^7;
sbit rst=P1^5;
sbit io=P1^6;
sbit sclk=P1^7;
sbit led=P3^0;
sbit k1=P2^0;
sbit k2=P2^1;
sbit k3=P2^2;
sbit k4=P2^3;
unsigned char code chuzhi[7]={14,6,12,31,23,59,54};
unsigned char code xieadd[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
unsigned char code duadd[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
unsigned char xie[7]={0};
unsigned char du[7]={0};
unsigned char flag;
void delay(unsigned char ims)
{
int i,j;
for(i=ims;i>0;i--)
for(j=110;j>0;j--);
}
void write_com(unsigned char com)
{
lcdrs=0;
P0=com;
lcden=1;
delay(1);
lcden=0;
}
void write_data(unsigned char date)
{
lcdrs=1;
P0=date;
lcden=1;
delay(1);
lcden=0;
}
void desplay_main()
{
write_com(0x80);
write_data('2');
write_data('0');
write_com(0x80+4);
write_data('/');
write_com(0x80+7);
write_data('/');
write_com(0x80+11);
write_data('w');
write_data('e');
write_data('e');
write_data('k');
write_com(0x80+0x40+2);
write_data(':');
write_com(0x80+0x40+5);
write_data(':');
}
void init()
{
lcden=0;
write_com(0x38);//设置点阵,8位数据接口
write_com(0x0c);//开显示,光标显示,光标闪
write_com(0x06);//写一个字符指针加一,整屏不移动
write_com(0x01);//清屏
write_com(0x80);//初始化指针
desplay_main();
k1=1;
k2=1;
k3=1;
k4=1;
}
void desplay_year(unsigned char shu)
{
int shi,ge;
shi=shu/10;
ge=shu%10;
write_com(0x80+2);
write_data(0x30+shi);
write_data(0x30+ge);
}
void desplay_week(unsigned char shu)
{
write_com(0x80+15);
write_data(0x30+shu);
}
void desplay_month(unsigned char shu)
{
int shi,ge;
shi=shu/10;
ge=shu%10;
write_com(0x80+5);
write_data(0x30+shi);
write_data(0x30+ge);
}
void desplay_day(unsigned char shu)
{
int shi,ge;
shi=shu/10;
ge=shu%10;
write_com(0x80+8);
write_data(0x30+shi);
write_data(0x30+ge);
}
void desplay_hour(unsigned char shu)
{
int shi,ge;
shi=shu/10;
ge=shu%10;
write_com(0x80+0x40);
write_data(0x30+shi);
write_data(0x30+ge);
}
void desplay_minute(unsigned char shu)
{
int shi,ge;
shi=shu/10;
ge=shu%10;
write_com(0x80+0x40+3);
write_data(0x30+shi);
write_data(0x30+ge);
}
void desplay_second(unsigned char shu)
{
int shi,ge;
shi=shu/10;
ge=shu%10;
write_com(0x80+0x40+6);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_ds1302_byte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
sclk=0;
io=dat&0x01;
dat=dat>>1;
sclk=1;
}
}
void write_ds1302(unsigned char add,unsigned char dat)
{
rst=0;
_nop_();
sclk=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(dat);
rst=0;
_nop_();
io=1;
sclk=1;
}
unsigned char read_ds1302(unsigned char add)
{
unsigned char i,shu;
rst=0;
_nop_();
sclk=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
shu=shu>>1;
sclk=0;
if(io==1)
shu=shu|0x80;
sclk=1;
}
rst=0;
_nop_();
sclk=0;
_nop_();
sclk=1;
_nop_();
io=1;
return shu;
}
void set()
{
unsigned char i,j;
for(i=0;i<7;i++)
{
j=chuzhi[i]/10;
xie[i]=chuzhi[i]%10;
xie[i]=j*16+xie[i];
}
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(xieadd[i],xie[i]);
}
write_ds1302(0x8e,0x80);
}
void read_shuju()
{
unsigned char i;
for(i=0;i<7;i++)
{
du[i]=read_ds1302(duadd[i]);
}
}
void chuli()
{
unsigned char i,j;
for(i=0;i<7;i++)
{
j=du[i]/16;
du[i]=du[i]%16;
du[i]=j*10+du[i];
}
}
void desplay_ds1302()
{
desplay_year(du[0]);
desplay_month(du[2]);
desplay_day(du[3]);
desplay_week(du[1]);
desplay_hour(du[4]);
desplay_minute(du[5]);
desplay_second(du[6]);
}
void guangbiao(unsigned char add)
{
write_com(add);
write_com(0x0f);
}
void set_hour()
{
char
time;
unsigned char j,value;
guangbiao(0x80+0x40+1);
time=read_ds1302(duadd[4]);
j=time/16;
time=time%16;
time=j*10+time;
if(k2==0)
{
delay(5);
if(k2==0)
{
time++;
if(time==24) time=0;while(!k2);
}
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[4],value);
write_ds1302(0x8e,0x80);
desplay_hour(time);
}
if(k3==0)
{
delay(5);
if(k3==0)
{
time--;
if(time==-1) time=23;while(!k3);
}
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[4],value);
write_ds1302(0x8e,0x80);
desplay_hour(time);
}
}
void set_minute()
{
char time;
unsigned char j,value;
guangbiao(0x80+0x40+4);
time=read_ds1302(duadd[5]);
j=time/16;
time=time%16;
time=j*10+time;
if(k2==0)
{
delay(5);
if(k2==0)
{
time++;
if(time==60) time=0;
}
while(!k2);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[5],value);
write_ds1302(0x8e,0x80);
desplay_minute(time);
}
if(k3==0)
{
delay(5);
if(k3==0)
{
time--;
if(time==-1) time=59;
}
while(!k3);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[5],value);
write_ds1302(0x8e,0x80);
desplay_minute(time);
}
}
void set_second_zero()
{
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[6],0);
write_ds1302(0x8e,0x80);
}
void set_year()
{
char time;
unsigned char j,value;
guangbiao(0x80+3);
time=read_ds1302(duadd[0]);
j=time/16;
time=time%16;
time=j*10+time;
if(k2==0)
{
delay(5);
if(k2==0)
{
time++;
if(time==100) time=0;
}
while(!k2);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[0],value);
write_ds1302(0x8e,0x80);
desplay_year(time);
}
if(k3==0)
{
delay(5);
if(k3==0)
{
time--;
if(time==-1) time=99;
}
while(!k3);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[0],value);
write_ds1302(0x8e,0x80);
desplay_year(time);
}
}
void set_month()
{
char time;
unsigned char j,value;
guangbiao(0x80+6);
time=read_ds1302(duadd[2]);
j=time/16;
time=time%16;
time=j*10+time;
if(k2==0)
{
delay(5);
if(k2==0)
{
time++;
if(time==13) time=0;
}
while(!k2);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[2],value);
write_ds1302(0x8e,0x80);
desplay_month(time);
}
if(k3==0)
{
delay(5);
if(k3==0)
{
time--;
if(time==-1) time=12;
}
while(!k3);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[2],value);
write_ds1302(0x8e,0x80);
desplay_month(time);
}
}
void set_day()
{
char time;
unsigned char j,value;
guangbiao(0x80+9);
time=read_ds1302(duadd[3]);
j=time/16;
time=time%16;
time=j*10+time;
if(k2==0)
{
delay(5);
if(k2==0)
{
time++;
if(time==31) time=0;
}
while(!k2);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[3],value);
write_ds1302(0x8e,0x80);
desplay_day(time);
}
if(k3==0)
{
delay(5);
if(k3==0)
{
time--;
if(time==-1) time=31;
}
while(!k3);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[3],value);
write_ds1302(0x8e,0x80);
desplay_day(time);
}
}
void set_week()
{
char time;
unsigned char j,value;
guangbiao(0x80+15);
time=read_ds1302(duadd[1]);
j=time/16;
time=time%16;
time=j*10+time;
if(k2==0)
{
delay(5);
if(k2==0)
{
time++;
if(time==8) time=1;
}
while(!k2);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[1],value);
write_ds1302(0x8e,0x80);
desplay_week(time);
}
if(k3==0)
{
delay(5);
if(k3==0)
{
time--;
if(time==-1) time=7;
}
while(!k3);
j=time/10;
value=time%10;
value=j*16+value;
write_ds1302(0x8e,0x00);
write_ds1302(xieadd[1],value);
write_ds1302(0x8e,0x80);
desplay_week(time);
}
}
void set_time()
{
if(k1==0)
{
delay(5);
if(k1==0)
{
flag++; while(!k1);
desplay_ds1302();
guangbiao(0x80+0x40+1);
}
while(flag!=7)
{
if(k1==0)
{
delay(5);
if(k1==0)
{
flag=flag+1;while(!k1);
}
}
if(flag==1) set_hour();
if(flag==2) set_minute();
if(flag==3) set_year();
if(flag==4) set_month();
if(flag==5) set_day();
if(flag==6) set_week();
}
flag=0;
set_second_zero();
}
}
void main()
{
init();
set();
while(1)
{
read_shuju();
chuli();
set_time();
write_com(0x0c);
desplay_ds1302();
}
}
-
-
protues仿真图.rar
下载积分: 积分 -1 分
24.56 KB, 下载次数: 2, 下载积分: 积分 -1 分
一周热门 更多>