#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit SCK=P3^6;
sbit SDA=P3^4;
sbit RST=P3^5;
sbit rs=P2^6;
sbit rw=P2^5;
sbit lcden=P2^7;
uchar
timedata[7]={14,02,03,18,12,0,0}; //nian 星期 y
uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar disp[13];
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_ds1302_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
SCK=0;
SDA=dat&0x01;
dat=dat>>1;
SCK=1;
}
}
void write_ds1302(uchar add,uchar dat)
{
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(dat);
RST=0;
_nop_();
SCK=1;
SDA=1;
}
read_ds1302(uchar add)
{
uchar i,temp;
RST=0;
_nop_();
SCK=0;
_nop_();
RST=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
temp>>=1;
SCK=0;
if(SDA)
temp=temp|0x80;
SCK=1;
}
RST=0;
_nop_();
SCK=0;
_nop_();
SCK=1;
SDA=1;
return temp;
}
void set_rtc()
{
uchar i,tmp;
for(i=0;i<7;i++)
{
tmp=timedata[i]/10;
timedata[i]=timedata[i]%10;
timedata[i]=timedata[i]+tmp*16;
}
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(write_add[i],timedata[i]);
}
write_ds1302(0x8e,0x80);
}
void read_rtc(uchar add)
{
uchar i;
for(i=0;i<7;i++)
{
timedata[0]=read_ds1302(add);
}
}
void write_com(uchar com)
{
rs=0;
rw=0;
lcden=0;
delay(5);
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
rs=1;
rw=0;
lcden=0;
delay(5);
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void display()
{ read_rtc(0x81);
write_com(0x80+0x40);
write_date('0'+timedata[0]/16); //时
write_date('0'+(timedata[0]&0x0f));
write_date('-');
write_date('0'+timedata[5]/16); //分
write_date('0'+(timedata[5]&0x0f));
write_date('-');
write_date('0'+timedata[4]/16); //秒
write_date('0'+(timedata[4]&0x0f));
/* write_com(0x80);
write_date('2');
write_date('0');
write_date('0'+TIME[6]/16); //年
write_date('0'+(TIME[6]&0x0f));
write_date('-');
write_date('0'+TIME[4]/16); //月
write_date('0'+(TIME[4]&0x0f));
write_date('-');
write_date('0'+TIME[3]/16); //日
write_date('0'+(TIME[3]&0x0f));
write_com(0x8D);
write_date('0'+(TIME[5]&0x07)); */
}
void init()
{
lcden=0;
write_com(0x38);
write_com(0x0f);
write_com(0x06);
write_com(0x01);
}
void main()
{
init();
set_rtc();
while(1)
{
display();
}
}
一周热门 更多>