}
void write_data(uchar date)
{
lcdrs=1;
delay(5);
P0=date;
lcde=1;
delay(5);
lcde=0;
}
void write_sfm(uchar add,uchar date) //shuaxinghanshu
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数)
{
unsigned char i;
ACC = d;
for(i=8; i>0; i--)
{
DS1302_IO = ACC0; //相当于汇编中的 RRC
DS1302_CLK = 1;
DS1302_CLK = 0;
ACC = ACC >> 1;
}
}
uchar DS1302OutputByte(void) //实时时钟读取一字节(内部函数)
{
uchar i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; //相当于汇编中的 RRC
ACC7 = DS1302_IO;
DS1302_CLK = 1;
DS1302_CLK = 0;
}
return(ACC);
}
void Write1302(unsigned char ucAddr, unsigned char ucDa) //ucAddr: DS1302地址, ucData: 要写的数据
{
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr); // 地址,命令
DS1302InputByte(ucDa); // 写1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
}
uchar Read1302(uchar ucAddr) //读取DS1302某地址的数据
{
uchar dat1,dat2;
DS1302_RST = 0;
DS1302_CLK = 0;
DS1302_RST = 1;
DS1302InputByte(ucAddr|0x01); // 地址,命令
dat1 = DS1302OutputByte(); // 读1Byte数据
DS1302_CLK = 1;
DS1302_RST = 0;
dat2=dat1/16; //数据进制转换
dat1=dat1%16; //十六进制转十进制
dat1=dat1+dat2*10;
return(dat1);
}
/*void DS1302_SetProtect(bit flag) //是否写保护
{
if(flag)
Write1302(0x8E,0x10);
else
Write1302(0x8E,0x00);
} */
void keyscan()
{
if(stop==0)
{
delay(5);
if(stop==0)
{ biaozhi=1;
while(!stop);
flag++;
if(flag==1)
{
write_com(0x80+0x40+0x08);
write_com(0x0f);
}
if(flag==2)
{
write_com(0x80+0x40+0x05);
write_com(0x0f);
}
if(flag==3)
{
write_com(0x80+0x40+0x02);
write_com(0x0f);
}
if(flag==4)
{
flag=0;
write_com(0x0c);
biaozhi=0;
Write1302(0x80,0x01); //使时钟暂停
Write1302(0x8e,0x00);
Write1302(0x84,xiaoshi);
Write1302(0x8e,0x80);
}
}
}
if(flag!=0)
{
if(jia==0)
{
delay(5);
if(jia==0)
{
while(!jia);
if(flag==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(7,miao);
write_com(0x80+0x40+7);
}
if(flag==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(4,fen);
write_com(0x80+0x40+4);
}
if(flag==3)
{
xiaoshi++;
if(xiaoshi==24)
xiaoshi=0;
write_sfm(1,xiaoshi);
write_com(0x80+0x40+1);
}
}
}
if(jian==0)
{
delay(5);
if(jian==0)
{
while(!jian);
if(flag==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(7,miao);
write_com(0x80+0x40+7);
}
if(flag==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(4,fen);
write_com(0x80+0x40+4);
}
if(flag==3)
{
xiaoshi--;
if(xiaoshi==-1)
xiaoshi=23;
write_sfm(1,xiaoshi);
write_com(0x80+0x40+1);
}
}
}
}
}
void init()
{ uchar j;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
write_com(0x80+0x41) ;
for( j=1;j<=8;j++)
{
write_data(table2[j]);
}
// xiaoshi=1;
// fen=1;
// miao=1;
// Write1302(0x80,miao);
// Write1302(0x82,fen);
// Write1302(0x84,xiaoshi);
}
void main()
{
init();
while(1)
{
keyscan();
if(biaozhi==0)
{
miao=Read1302(DS1302_SECOND);
fen=Read1302(DS1302_MINUTE);
xiaoshi=Read1302(DS1302_HOUR);
write_sfm(7,miao);
write_sfm(4,fen);
write_sfm(1,xiaoshi);
}
// keyscan();
}
}