#include<reg52.h>
#include<ds1302.h>
#define uchar unsigned char
#define uint unsigned int
//ds1302pin
sbit SCLK=P3^6;
sbit SDA=P3^4;
sbit CE=P3^5;
void delayms()
{; ;}
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
void WB(uchar tem)
{uchar i;
ACC=tem;
for(i=0;i<8;i++)
{SDA=ACC0;
SCLK=1;
ACC=ACC>>1;
SCLK=0;
}
}
uchar RB()
{uchar i;
for(i=0;i<8;i++)
{ACC7=SDA;
SCLK=1;
ACC=ACC>>1;
SCLK=0;
}
return ACC;
}
void WBD(uchar addr2,uchar datas)
{ACC=addr2;
CE=0;
SCLK=0;
CE=1;
WB(ACC);
ACC=datas;
WB(ACC);
CE=0;
}
uchar RBD(uchar addr1)
{uchar tem1,tem3;
tem3=addr1;
CE=0;
SCLK=0;
CE=1;
WB(tem3);
SDA=1;
tem1=RB();
CE=0;
return tem1;
}
void initial(void)
{CE=0;
delayms();
SCLK=0;
delayms();
WBD(0x8e,0x00);
WBD(0x90,0xa6);
}
/*
uchar bdata WRITEDATA; //写入数据包括数据也包括命令
uchar bdata WRITEDATA1;
uchar bdata READDATA;//读数据包括寄存器的任何值
sbit IBIT=WRITEDATA^0; //写入数据的第一位
sbit IBIT1=WRITEDATA1^0;
sbit OBIT=READDATA^7; //读出数据的第一位
//SINGLE BYTE WRITE
void BYTEWRITE(uchar addr,uchar datas)
{uchar i,j;
WRITEDATA=addr;
CE=0;
SCLK=0;
CE=1;
for(i=0;i<8;i++)
{SDA=IBIT;
// delayms();
SCLK=1;
//delayms();
WRITEDATA=WRITEDATA>>1;
SCLK=0;
//
}
WRITEDATA1=datas;
for(j=0;j<8;j++)
{SDA=IBIT1;
// delayms();
SCLK=1;
WRITEDATA1=WRITEDATA1>>1;
SCLK=0;
// delayms();
}
CE=0;
delayms();
CE=1;
//SCLK=1;//
}
//SINGLE BYTE REA
uchar BYTEREAD(uchar addr1)
{uchar k,n;
WRITEDATA=addr1;
CE=0;
SCLK=0;
CE=1;
for(k=0;k<8;k++)
{SDA=IBIT;
//delayms();
SCLK=1;
WRITEDATA=WRITEDATA>>1;
//delayms();
SCLK=0;
//
}
SDA=1;
for(n=0;n<8;n++)
{//delayms();
OBIT=SDA;
READDATA=READDATA>>1;
SCLK=1;
//delayms();
SCLK=0;
// delayms();
}
// SCLK=1;
CE=0;
//
return READDATA;
}
//初始化initial
void ds1302initial()
{uchar ds1302st;
CE=0;
delayms();
SCLK=0;
delayms();
ds1302st=BYTEREAD(0xc1);
if(ds1302st!=0x55)
{ BYTEWRITE(0x8e,0x00);
BYTEWRITE(0x90,0xa6);
BYTEWRITE(0xc0,0x55);
}
}
//res1302
void rest1302()
{ CE=0;
delayms();
SCLK=0;
delayms();
} */ |