⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock_ds1307.c

📁 一个流量控制仪器的源码
💻 C
字号:
/***********************************************************************************

DS1307时钟芯片控制程序  DS1307.C

MCU 型号: Philips P89C668
时钟频率: 11.0592 MHz
接口方式: I2C 100KHz
开发环境: Keil C51 V7.07a	
开发日期: 2003.11.09
程序编写: 鲍方
	
***********************************************************************************/

#include <REG668.H>
#include <I2C.H>



#define uchar unsigned char
#define uint unsigned int

// I2C write address of DS1307
#define DS1307_ADDRESS 0xD0


enum weekdays { SUNDAY = 1, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
                FRIDAY, SATURDAY };           // define the weekdays

enum months { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE,
              JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
                                              // define the months

typedef struct                           
{                                             
  uchar seconds;
  uchar minute;
  uchar hour;
  uchar weekday;
  uchar day;
  uchar month;
  uchar year;
  uchar control;
} DS1307_time;


//定义系统日期时间,初始化-2004.1.1.  00:00:00
//DS1307格式
xdata struct DS1307_time current_clock = {0x00, 0x00, 0x00, SUNDAY , 0x01 , JANUARY , 04};



//CL(SYSTEMCLOCK) 系统时钟时间--程序格式
xdata uchar	uc8_SystemDateTime[8];

//字节序号	0		1		2		3
//时间格式	百小时(0-9)	小时(00-99)	分钟(0-59)	秒(0-59)
//字节序号	4		5		6		7
//时间格式	年(00-99)	月(01-12)	日(01-31)	控制字节


// 10进制码-->BCD码
// function chartobcd
// converts a decimal value to the BCD equivalent
// decimal values must be stored in the Real Time Clock in BCD format
// passed is the decimal value to convert
// returned is the BCD equivalent
unsigned char chartobcd(unsigned char n)
{
  return ((n / 10) << 4) | (n % 10);  
}


// BCD码-->10进制码
// function bcdtochar
// converts a number in BCD format to the decimal equivalent
// decimal values must be stored in the Real Time Clock in BCD format
// passed is the BCD format value
// returned is the decimal equivalent
uchar bcdtochar(unsigned char bcdnum)
{
  return (bcdnum & 0x0f) + (bcdnum >> 4) * 10;
}


/*******************************************************************
		系统日期时间设置函数(内部格式转换)
函数原型: void fnSet_SystemClock()
功能: 	系统日期时间设置DS1307(内部格式转换),并立即运行!

涉及变量:	uc8_SystemDateTime[8];
		current_clock
********************************************************************/
void fnSet_SystemClock()
{
	//内部格式转换
	//程序格式-->DS1307格式
	//10进制码-->BCD码
	
	current_clock.year = chartobcd(uc8_SystemDateTime[4]);

	current_clock.month = chartobcd(uc8_SystemDateTime[5]);
	
	current_clock.day = chartobcd(uc8_SystemDateTime[6]);
	
	current_clock.hour = chartobcd(uc8_SystemDateTime[1]);
	
	current_clock.minute = chartobcd(uc8_SystemDateTime[2]);
	
	current_clock.seconds = chartobcd(uc8_SystemDateTime[3]);
	
	current_clock.weekday = 1;
	
	//强制DS1307运行于24小时模式
	
	current_clock.hour = current_clock.hour & 0x3f;
	
	// enable the oscillator (CH bit = 0) 
	
	current_clock.seconds = current_clock.seconds & 0x7f;
	
	current_clock.control = 0x00;
	
	//I2C写入
	I2C_SendStr(DS1307_ADDRESS,0x00,(uchar *)&current_clock ,8);
	
}



/*******************************************************************
		系统日期时间读取函数(内部格式转换)
函数原型: void fnRead_SystemClock()
功能: 	系统日期时间读取DS1307(内部格式转换)

涉及变量:	uc8_SystemDateTime[8];
		current_clock
********************************************************************/
void fnRead_SystemClock()
{
	//I2C读取
	I2C_RcvStr(DS1307_ADDRESS,0x00,(uchar *)&current_clock,8);

	//内部格式转换
	//DS1307格式-->程序格式
	//BCD码-->10进制码
	
	uc8_SystemDateTime[4] = bcdtochar(current_clock.year);
	
	uc8_SystemDateTime[5] = bcdtochar(current_clock.month);
	
	uc8_SystemDateTime[6] = bcdtochar(current_clock.day);
	
	uc8_SystemDateTime[1] = bcdtochar(current_clock.hour);
	
	uc8_SystemDateTime[2] = bcdtochar(current_clock.minute);
	
	uc8_SystemDateTime[3] = bcdtochar(current_clock.seconds);
	

}


/*******************************************************************
		DS1307的RAM数据操作函数:在某地址写入n字节数据
函数原型: bit fnWrite_DS1307RAM(uchar ucAddress,uchar ucSize, uchar *uc_Data);
功能: 	DS1307的RAM数据操作函数:在DS1307的RAM某地址写入n字节数据
		所有数据的地址范围检查。
		数据地址的范围00H-3FH,注意00H-07H是DS1307的工作寄存器。
涉及变量:	
		ucAddress	写入的RAM地址
		ucSize		写入的数据长度
		*uc_Data	写入数据的指针
返回:	0			出错
		1			正常
********************************************************************/
bit fnWrite_DS1307RAM(uchar ucAddress,uchar ucSize, uchar *uc_Data)
{
	//检查所有数据的最终写入地址是否越界00H-3FH
	if( ucAddress + ucSize > 0x3F)
		return(0);

	//利用I2C总线把数据写入DS1307的RAM
	//I2C写入
	I2C_SendStr(DS1307_ADDRESS,ucAddress,uc_Data,ucSize);
	
	//返回  正常
	return(1);
}



/*******************************************************************
		DS1307的RAM数据操作函数:在某地址读入n字节数据
函数原型: bit fnRead_DS1307RAM(uchar ucAddress,uchar ucSize, uchar *uc_Data);
功能: 	DS1307的RAM数据操作函数:在DS1307的RAM某地址读入n字节数据
		所有数据的地址范围检查。
		数据地址的范围00H-3FH,注意00H-07H是DS1307的工作寄存器。
涉及变量:	
		ucAddress	读入的RAM地址
		ucSize		读入的数据长度
		*uc_Data	读入数据的指针
返回:	0			出错
		1			正常
********************************************************************/
bit fnRead_DS1307RAM(uchar ucAddress,uchar ucSize, uchar *uc_Data)
{
	//检查所有数据的最终读入地址是否越界00H-3FH
	if( ucAddress + ucSize > 0x3F)
		return(0);

	//利用I2C总线把数据读入DS1307的RAM
	//I2C读取
	I2C_RcvStr(DS1307_ADDRESS,ucAddress,uc_Data,ucSize);
	
	//返回  正常
	return(1);
}

⌨️ 快捷键说明

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