#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P3^4;
sbit lcdrs=P3^5;
sbit dula=P2^6;
sbit wela=P2^7;
uchar num;
sbit sck=P2^2;
sbit sda=P2^0;
sbit rst=P2^1;
unsigned char timedata[7]={0,0,0,0x23,0x11,0x03,0x11};
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
code unsigned char table[]={'0','1','2','3','4','5','6','7','8','9'};
void write_ds1302_byte(unsigned char temp)
{
unsigned char i;
for(i=0;i<8;i++)
{
sda=temp&0x01;
sck=1;sck=0;
temp>>=1;
}
}
void write_ds1302(unsigned char address,unsigned char dat)
{
rst=0;
_nop_();
sck=0;_nop_();
rst=1;_nop_();
write_ds1302_byte(address);
write_ds1302_byte(dat);
sck=1;
rst=0;
}
unsigned char read_ds1302(unsigned char address)
{
unsigned char i,temp=0x00;
rst=0;_nop_();
sck=0;_nop_();
rst=1;_nop_();
write_ds1302_byte(address);
for(i=0;i<8;i++)
{
if(sda)
temp|=0x80;
sck=0;
temp>>=1;
sck=1;
}
rst=0;_nop_();
rst=0;sck=0;_nop_();
sck=1;_nop_();sda=0;
_nop_();sda=1;_nop_();
return (temp);
}
void set_rtc(void)
{
unsigned char i;
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(write_rtc_address[i],timedata[i]);
}
write_ds1302(0x8e,0x80);
}
void read_rtc(void)
{
unsigned char i;
for(i=0;i<7;i++)
{
timedata[i]=read_ds1302(read_rtc_address[i]);
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void init()
{
dula=0;
wela=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void display(void)
{
write_com(0x80+0x01);
write_data('2');
write_data('0');
write_data(table[(timedata[3]>>4)&&0x0f]);
write_data(table[timedata[3]&&0x0f]);
write_data('-');
write_data(table[(timedata[4]>>4)&&0x0f]);
write_data(table[timedata[4]&&0x0f]);
write_data('-');
write_data(table[(timedata[3]>>4)&&0x0f]);
write_data(table[timedata[3]&&0x0f]);
write_data(table[(timedata[5]>>4)&&0x0f]);
write_data(table[timedata[5]&&0x0f]);
write_com(0x80+0x40);
write_data(table[(timedata[2]>>4)&&0x0f]);
write_data(table[timedata[2]&&0x0f]);
write_data(':');
write_data(table[(timedata[1]>>4)&&0x0f]);
write_data(table[timedata[1]&&0x0f]);
write_data(':');
write_data(table[(timedata[0]>>4)&&0x0f]);
write_data(table[timedata[0]&&0x0f]);
}
main()
{
init();
set_rtc();
while(1)
{
read_rtc();
display();
delay(500);
}
} |