地址、数据发送子程序
void Write1302 ( unsigned char addr,dat )
{
unsigned char i,temp;
CE=0;
//CE
引脚为低,
数据传送
中止
SCLK=0;
//
清零时钟总线
CE = 1;
//CE引脚为高,逻辑控制有效
//发送地址
for
( i=8; i>0; i-- )
//循环8次移位
{
SCLK = 0;
temp = addr;
DIO = (bit)(temp&0x01); //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}
//发 送数据
for ( i=8; i>0; i-- )
{
SCLK = 0;
temp = dat;
DIO = (bit)(temp&0x01);
dat >>= 1;
SCLK = 1;
}
CE = 0;
}
//数据读取子程序
unsigned char Read1302 ( unsigned char addr )
{
unsigned
char i,temp,dat1,dat2;
CE=0;
SCLK=0;
CE = 1; //发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK = 0;
temp = addr;
DIO = (bit)(temp&0x01); //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}//读取数据
for ( i=8; i>0; i-- )
{
ACC_7=DIO;
SCLK = 1;
ACC>>=1;
SCLK = 0;
}
CE=0;
dat1=ACC;
dat2=dat1/16; //数据进制转换
dat1=dat1%16; //十六进制转十进制
dat1=dat1+dat2*10;
return (dat1);
}//初始化DS1302
void Initial(void)
{
Write1302 (WRITE_PROTECT,0X00); //禁止写保护
Write1302 (WRITE_SECOND,0x56); //秒位初始化
Write1302 (WRITE_MINUTE,0x34); //分钟初始化
Write1302 (WRITE_HOUR,0x12); //小时初始化
Write1302 (WRITE_PROTECT,0x80); //允许写保护
} |