📄 ds1302.h
字号:
#include <reg51.h>
#include <intrins.h>
// common PreDefinition
#define HIGH 1
#define LOW 0
#define TRUE 1
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
// ds1302 PreDefinition
#define DS1302_RESET RST=LOW;SCL=LOW;RST=HIGH
sbit SCL = P2^3;// DS1302时钟信号 7脚
sbit SDA = P2^4;// DS1302数据信号 6脚
sbit RST = P2^5;// DS1302片选 5脚
/* **********************************************************************/
//void delay_DS1302(unsigned int d_time); //延时子函时,
void Write_DS1302_byte(unsigned char temp);
void Write_DS1302_RTC( unsigned char address,unsigned char dat );
unsigned char Read_DS1302_RTC( unsigned char address );
void dis_delay(void);
void Init_1302(void);
/* *************************************************************************** */
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++) //循环8次 写入数据
{
SCL=LOW;
_nop_();
SDA=temp&LSB; //每次传输低字节
temp>>=1; //右移一位
SCL=HIGH;
}
}
/****************************************************************************/
void Write_DS1302_RTC( unsigned char address,unsigned char dat )
{
DS1302_RESET; //启动
_nop_();
Write_Ds1302_Byte(address); //发送地址
_nop_();
Write_Ds1302_Byte(dat); //发送数据
RST=LOW; //恢复
}
/****************************************************************************/
unsigned char Read_DS1302_RTC( unsigned char address )
{
unsigned char i,temp=0x00;
DS1302_RESET; //启动
Write_Ds1302_Byte(address);
for (i=0;i<8;i++) //循环8次 读取数据
{
if(SDA)
temp|=0x80; //每次传输低字节
SCL=HIGH;
_nop_();
temp>>=1; //右移一位
SCL=LOW;
}
RST=LOW;
return (temp); //返回
}/******************************************************************************/
void Init_1302(void)//设置1302的初始时间
{
Write_DS1302_RTC(0x8e,0x00);//Open Write Protect bit
Write_DS1302_RTC(0x80,0x00);//Write Second 0
Write_DS1302_RTC(0x82,0x52);//Write Minute 52
Write_DS1302_RTC(0x84,0x21);//Write Hour 21
Write_DS1302_RTC(0x86,0x14);//Write Day 14
Write_DS1302_RTC(0x88,0x10);//Write Month 10
Write_DS1302_RTC(0x8a,0x05);//Write Week Friday
Write_DS1302_RTC(0x8c,0x06);//Write Year 06
Write_DS1302_RTC(0x8e,0x80);//Close Write Protect bit
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -