#include "reg52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned char
sbit sclk = P1^7;
sbit io = P2^3;
sbit ce = P1^3;
sbit ACC_7 = ACC^7;
uchar code tab[12] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
uchar buf[] = {10,10,10,10,10,10,10,10};
uchar dspcom = 0,min,hour,day;
bit clock_flag;
void init_ds1302();
void w_ds1302(uchar add,uchar dat);
uchar r_ds1302(uchar add);
void display();
void init_timer0();
void main()
{
init_timer0();
init_ds1302();
while(1)
{
buf[3] = hour/16;
buf[4] = hour%16;
buf[5] = 11;
buf[6] = min/16;
buf[7] = min%16;
}
}
void init_ds1302()
{
w_ds1302(0x8e,0x00);
w_ds1302(0x80,0x55);
w_ds1302(0x82,0x00);
w_ds1302(0x84,0x00);
w_ds1302(0x8e,0x80);
}
void w_ds1302(uchar add,uchar dat)
{
uchar i;
ce = 0;
sclk = 0;
ce = 1;
for(i = 8;i 0;i--)
{
sclk = 0;
io = (bit)(add&0x01);
add = 1;
sclk = 1;
}
for(i = 8;i 0;i--)
{
sclk = 0;
io = (bit)(dat&0x01);
dat = 1;
sclk = 1;
}
sclk = 1;
ce = 0;
}
uchar r_ds1302(uchar add)
{
uchar i,dat;
EA = 0;
ce = 0;
sclk = 0;
ce = 1;
for(i = 8;i 0;i--)
{
sclk = 0;
io = (bit)(add&0x01);
add = 1;
sclk = 1;
}
for(i = 8;i 0;i--)
{
ACC_7 = io;
sclk = 1;
ACC = 1;
sclk = 0;
}
sclk = 1;
ce = 0;
EA = 1;
dat = ACC;
return (dat);
}
void timer0() interrupt 1
{
TH0 = (65535-2000)/256;
TL0 = (65535-2000)%256;
min = r_ds1302(0x81);
hour = r_ds1302(0x83);
display();
}