能读不能写,为什么呀?求帮忙看一下代码
//DS1302.c程序
#include <1302.h>
void WriteByte1302(u16 addr,u16 dat) //void writeds1302_dat(u8 dat)
{
u8 i;
GPIOA->CRL&=0XFF000FFF;
GPIOA->CRL|=0X33333333; //控制三线作输出模式
GPIOA->ODR|=1<<5;//DS1302_SETCE();ce=1
delay(100);
addr=addr&0xFE;
for(i=0;i<8;i++)
{
GPIOA->ODR&=0XFFF7;//sclk=0
delay(100);
if(addr&0x01)
{
GPIOA->ODR|=1<<4; //io=dat&0x01;DS1302_SETIO();//io=1
}
else
GPIOA->ODR&=0XFFEF;//GPIOA->ODR&=0//io=0
delay(100);
GPIOA->ODR|=1<<3; //sclk=1;DS1302_SETSCK()
delay(100);
addr=addr>>1;
delay(100);
}
for(i=0;i<8;i++)
{
GPIOA->ODR&=0XFFF7;//sclk=0;DS1302_CLRSCK()
delay(100);
if(dat&0x01)
{
GPIOA->ODR|=1<<4; //io=dat&0x01;DS1302_SETIO();//io=1
}
else
GPIOA->ODR&=0XFFEF;//GPIOA->ODR=0<<4;//DS1302_CLRIO()//io=0
delay(100);
dat=dat>>1;
GPIOA->ODR|=1<<3; //sclk=1
delay(100);
}
GPIOA->ODR&=0XFFDF;//DS1302_CLRCE(); //ce=0
}
/***********************/
void Write1302(u16 add,u8 dat)
{
u8 dat1,dat2,date,temp;
temp=dat;
dat1=temp/10; //BCD码转换
dat2=temp%10;
date=dat1*16+dat2;
WriteByte1302(0x8e,0x00); //关闭写保护
WriteByte1302(add,date);
WriteByte1302(0x8e,0x80);//打开写保护
}
/***************************/
u16 ReadByte1302(u16 dat) 读时间字节函数
{
u16 i,temp,dat0,dat1,dat2;
GPIOA->CRL&=0XFF000FFF;
GPIOA->CRL|=0X33333333;
DS1302_CLRCE(); //res=0;
delay(100); //_nop_();
DS1302_CLRSCK();//sclk=0;
delay(100); //_nop_();
DS1302_SETCE(); //res=1;
delay(100); //_nop_();
for(i=0;i<8;i++)
{
DS1302_CLRIO();//GPIOA->ODR&=0//io=0
DS1302_CLRSCK(); //sclk=0;
if(dat&0x01)
DS1302_SETIO();//GPIOA->ODR|=1<<4; //io=1
dat=dat>>1;
DS1302_SETSCK();//GPIOA->ODR|=1<<3; //sclk=1;
}
GPIOA->CRL&=0XFF000FFF;
GPIOA->CRL|=0X00343000;
for(i=0;i<8;i++)
{
temp=temp>>1;
DS1302_CLRSCK(); //sclk=0;
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4))
temp=temp|0x80;
DS1302_SETSCK();//GPIOA->ODR|=1<<3; //sclk=1;
}
DS1302_CLRCE();//res=0;
delay(100); //_nop_();
dat1=temp/16;
dat2=temp%16; //数制转换
dat0=dat1*10+dat2;
return(dat0);
}
u16 Read1302(u16 addr) //读1302里时间
{
u16 dat;
dat=ReadByte1302(addr);
return dat;
}
void initial(void) //ds1302初始化函数
{
GPIOA->ODR&=0xffef; //GPIOA->ODR&=0<<4;rst=0;
GPIOA->ODR&=0xfff7; //GPIOA->ODR&=0<<3; //sclk=0;
WriteByte1302(WRITE_PROTECT,0x80);
} |