void write_1302(unsigned char add, unsigned char dat) //写数据;
{
unsigned char i;
REST_1302();
for(i = 0; i < 8; i++)
{
SCLK = 0;
if (0x01 & (add >> i)) IO = 1;
else IO = 0;
SCLK = 1;
}
for(i = 0; i < 8; i++)
{
SCLK = 0;
if (0x01 & (dat >> i)) IO = 1;
else IO = 0;
SCLK = 1;
}
RST = 0;
}
unsigned char read_1302(unsigned char add) //读出数据;
{
unsigned char temp = 0, i;
REST_1302();
for(i = 0; i < 8; i++)
{
SCLK = 0;
if (0x01 & (add >> i)) IO = 1;
else IO = 0;
SCLK = 1;
}
for(i = 0; i < 8; i++)
{
SCLK = 1;
SCLK = 0;
if (IO) temp = (1 << i) | temp;
}
RST = 0;
return temp; //返回数据;
}
void REST_1302(void) //复位一下;
{
RST = 0;
SCLK = 0;
RST = 1;
}
看看这个:保证解决问题,改改管脚定义;其他别动就行啦,好好分析吧!!!
你应该再写一个具体的主调函数,不说明你向DS1302哪个地址写入了什么数据,或从哪个地址读数据.这样才能更准确的分析,我大概看了一下,发现unsigned char Read1302(void)函数中,你只对读到的1进行了存入,而没有对读到的0进行存放.所以需作如下更改:
unsigned char Read1302(void)
{
unsigned char i,dat;
delaynus(2);
for(i=0;i<8;i++)
{
if(DATA) {dat=(dat>>=1)|0x80}; //读到数据1
else
dat>>=1; //读到数据0
SCLK=1;
delaynus(2);
SCLK=0;
delaynus(2);
}
return dat;
}
还有就是在读操作时,指今得或0x01(即cmd | 0x01)
你再去试一下.看看.