#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar num,time,ge,s,s1num,shi,fen,miao,i,date,add;
uchar code table[]="2013 10 08";
sbit ce=P3^5;
sbit ds=P3^4;
sbit sclk=P3^6;
sbit lcdrs=P2^6;
sbit lcdrd=P2^5;
sbit lcde=P2^7;
uint x,y,z;
uchar dsread ();
uchar dssignleread(uchar add);
void dswrite (uchar add,uchar date);
void DS1302Init();
void depy(uint z) 延时函数
{
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void writecom(uchar com) lcd输入命令
{
lcdrs=0;
lcdrd=0;
lcde=0;
P0=com;
depy(5);
lcde=1;
depy(5);
lcde=0;
}
void writedate(uchar date) lcd输入数据
{
lcdrs=1;
lcdrd=0;
lcde=0;
P0=date;
depy(5);
lcde=1;
depy(5);
lcde=0;
}
void dswrite (uchar add,uchar date) 1302写入
{
for(i=0;i<8;i++)
{
ds=add>>7;
add<<=1;
sclk=1;
sclk=0;
}
}
uchar dsread () 1302读
{
uchar date,date1;
for(i=0;i<8;i++)
{
date1=ds;
date=date>>1;
date|=date1<<7;
sclk=1;
sclk=0;
}
return date;
}
uchar dssignleread(uchar add) 读出时间
{
ce=1;
dswrite((add<<1)|0x81);
date=dsread();
ce=0;
sclk=1;
ds=0;
ds=1;
return date;
}
void dssignlewrite(uchar add,uchar date) 写入时间
{
ce=1;
dswrite((add<<1)|0x80);
dswrite(date);
ce=0;
}
void DS1302Init() 1302初始化
{
dssignlewrite(0x8E,0x00);
dssignlewrite(0x80,((0/10)<<4|(0%10)));
dssignlewrite(0x82,((0/10)<<4|(0%10)));
dssignlewrite(0x84,((12/10)<<4|(12%10)));
}
void start() 初始化
{
writecom(0x38);
writecom(0x0c);
writecom(0x06);
for(num=0;num<10;num++)
{
writedate(table[num]);
depy(5);
}
writecom(0x80+0x45);
writedate(':');
writecom(0x80+0x48);
writedate(':');
DS1302Init();
}
void writeser(uchar add,uchar date)
{
date=((date&0x70)>>4)*10+(date&0x0F);
s=date/10;
ge=date%10;
writecom(0x80+0x40+add);
writedate(0x30+s);
writedate(0x30+ge);
}
void main ()
{
start();
while(1)
{
miao=dssignleread(0);
writeser(9,miao);
fen=dssignleread(1);
writeser(6,fen);
shi=dssignleread(1);
writeser(3,shi);
}
} |