沿时。。DS1302读取数据
//_nop_() ;
SCLK = 0; //DS1302在下降沿时,放置数据
//_nop_();
}
_nop_();
//输出数据;dat
for(i=0; i<8; i++)
{
Temp = IO;//从最低位开始接收
dat = (dat >> 1) | (Temp<<7);
SCLK= 1;
//_nop_();
SCLK= 0;//DS1302在下降沿时,放置数据
//_nop_();
}
RST = 0; // 停止DS1302数据传送
_nop_();//以下为DS1302复位的稳定时间,必须的。
SCLK = 1;
_nop_();
IO = 0;
_nop_();
IO = 1;
_nop_();
return dat;
}
/*------------------------------------------------
向DS1302写入时钟数据
------------------------------------------------ */
void Ds1302_Write_Time()
{
uchar i,tmp;
for (i=0 ;i<8 ;i++)
{
tmp = Time_Data[i]/10;
Time_Buf[i] = Time_Data[i]%10;
Time_Buf[i] = Time_Buf[i] + tmp*16;
}
//BCD码处理,,,将十六进制数据转化为十进制
Ds1302_Write_Byte( 0x8e , 0x00 );//关闭写保护,可以可以写入数据
Ds1302_Write_Byte( 0x80 , 0x80 ); //暂停
Ds1302_Write_Byte( 0x8a , Time_Buf[7] ); // 星期
Ds1302_Write_Byte( 0x80 , Time_Buf[6] ); // 秒
Ds1302_Write_Byte( 0x82 , Time_Buf[5] ); // 分
Ds1302_Write_Byte( 0x84 , Time_Buf[4] ); // 时
Ds1302_Write_Byte( 0x8a , Time_Buf[7] ); // 星期
Ds1302_Write_Byte( 0x86 , Time_Buf[3] ); // 日
Ds1302_Write_Byte( 0x88 , Time_Buf[2] ); // 月
Ds1302_Write_Byte( 0x8c , Time_Buf[1] ); // 年
Ds1302_Write_Byte( 0x8e , 0x80 ); //打开写保护,不能写入数据
}
/*------------------------------------------------
读出DS1302时钟数据
------------------------------------------------*/
void Ds1302_Read_Time( )
{
uchar i,tmp;
Time_Buf[1]= Ds1302_Read_Byte(0x8d) ;// 年
Time_Buf[2]= Ds1302_Read_Byte(0x89) ;//月
Time_Buf[3]= Ds1302_Read_Byte(0x87) ;//日
Time_Buf[4]= Ds1302_Read_Byte(0x85) ;//时
Time_Buf[5]= Ds1302_Read_Byte(0x83) ;//分
Time_Buf[6]= (Ds1302_Read_Byte(0x81))&0x7f ;//秒
Time_Buf[7]= Ds1302_Read_Byte(0x8b) ; //周
for (i=0 ;i<8 ;i++)
{
tmp = Time_Buf[i]/16;
Time_Data[i] = Time_Buf[i]%16;
Time_Data[i] = Time_Data[i] + tmp*10;
}
//BCD码处理,,,将十进制数据转化为十六进制
}
/*------------------------------------------------
初始化DS1302
------------------------------------------------*/
void Ds1302_Init(void)
{
RST=0;//RST脚置低
SCLK=0;//SCK脚置低
Ds1302_Write_Byte(0x80,0x00);
}
///
__DS1302_H__/
//
#ifndef __DS1302_H__
#define __DS1302_H__
#include<reg52.h>
#include<intrins.h>
//---重定义关键词---//
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsi