ds1302.h

来自「单片机设计存档」· C头文件 代码 · 共 70 行

H
70
字号
#ifndef _DS1302_H_
#define _DS1302_H_

void condition()     //开始时的定义
{
	RST=0;
	SCK=0;
	RST=1;
}

void sendbyte(uint8 command,uint8 dat)  //发送一个字节
{
	uint8 i;
	condition();
	for(i=0;i<8;i++)
	{
		SDA=command&0x01;
		SCK=0;
		SCK=1;
		command>>=1;	
	}
	for(i=0;i<8;i++)
	{
		SDA=dat&0x01;
		SCK=0;
		SCK=1;
		dat>>=1;
	}
	RST=0;
}

uint8 readbyte(uint8 command)    //读一个字节
{
	uint8 i,dat=0;
	condition();
	for(i=0;i<8;i++)
	{
		SDA=command&0x01;
		SCK=0;
		SCK=1;
		command>>=1;
	}
	for(i=0;i<8;i++)
	{
		SCK=1;
		SCK=0;
		if(SDA)
			dat|=0x01<<i;
	}
	RST=0;
	return (dat);
}

void kaiguan(bit dat)    //1时准许写入;0时为写保护
{
	if(dat)
		sendbyte(0x8e,0x00);
	else
		sendbyte(0x8e,0x80);
}

void init_ds1302()
{
	kaiguan(1);
	sendbyte(0x80,0x45);	// 将0写入秒寄存器;
	sendbyte(0x82,0x59);	// 将20写入分寄存器;
	sendbyte(0x84,0x23);	// 将13写入小时寄存器;
	sendbyte(0x86,0x31);	// 将2日写入日寄存器;
	sendbyte(0x88,0x12);	// 将1月写入月寄存器;
	sendbyte(0x8a,0x07);	// 将05写刃瞧诩拇嫫鳎

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?