/************************** Copyright (c) **********************************
|
** FUZHOU FUGUANG ELECTRONICS Co.,LTD.
|
** ¸£Öݸ£¹âµç×ÓÓÐÏÞ¹«Ë¾
|
** http://www.fuguang.com
|
**
|
**-------------- File Info -------------------------------------------------
|
** File name: DS1302_Driver.c
|
** Last modified Date: 2008-11-16
|
** Last Version: 1.0
|
** Descriptions: DS1302 Drivers lib
|
**
|
**--------------------------------------------------------------------------
|
** Created by: mxpopstar
|
** Created date: 2008-11-16
|
** Version: 1.0
|
** Descriptions: The original version
|
**
|
**--------------------------------------------------------------------------
|
** Modified by: mxpopstar
|
** Modified date: 2008-11-20
|
** Version:
|
** Descriptions: DS1302 Drivers lib
|
**
|
***************************************************************************/
|
//
|
|
#include "DS1302_Driver.h"
|
|
void InitIO_DS1302(void)
|
{
|
DDRB |= 0x70;
|
RST_DS1302_0;//ÇåÁãRST
|
SCLK_DS1302_0;
|
_delay_ms(1);
|
}
|
//
|
|
void Config_DS1302(void)
|
{
|
unsigned char temp;
|
//----------- ³õʼ»¯DS1302 ------------------
|
Ds1302_Write_Byte(0x8e,0x00);//¼Ä´æÆ÷ÔÊÐíдÈë
|
|
temp=Ds1302_Read_Byte(0x81); //read second
|
temp=((temp/16)*10+(temp%16));
|
if(temp>59)
|
{
|
temp = 0;
|
Ds1302_Write_Byte(0x80,temp); //write second
|
}
|
|
temp=Ds1302_Read_Byte(0x83); //read minute
|
temp =((temp/16)*10+(temp%16));
|
if(temp>59)
|
{
|
temp = 0;
|
Ds1302_Write_Byte(0x82,temp); //write minute
|
}
|
|
temp =Ds1302_Read_Byte(0x85); //read hour
|
temp=((temp/16)*10+(temp%16));
|
if(temp>23)
|
{
|
temp = 0;
|
Ds1302_Write_Byte(0x84,temp); //write hour
|
}
|
|
temp=Ds1302_Read_Byte(0x87); //read day
|
temp=((temp/16)*10+(temp%16));
|
if((temp<1)||(temp>31))
|
{
|
temp = 1;
|
Ds1302_Write_Byte(0x86,temp); //write day
|
}
|
|
temp=Ds1302_Read_Byte(0x89); //read month
|
temp=((temp/16)*10+(temp%16));
|
if((temp<1)||(temp>12))
|
{
|
temp = 1;
|
Ds1302_Write_Byte(0x88,temp); //write month
|
}
|
|
temp=Ds1302_Read_Byte(0x8b); //read week
|
temp=((temp/16)*10+(temp%16));
|
if((temp<1)||(temp>7))
|
{
|
temp = 1;
|
Ds1302_Write_Byte(0x8a,temp); //write week
|
}
|
|
temp=Ds1302_Read_Byte(0x8d); //read year
|
temp=((temp/16)*10+(temp%16));
|
if((temp>99)||(temp<8))
|
{
|
temp = 9;
|
Ds1302_Write_Byte(0x8c,temp); //write year
|
}
|
}
|
//-------------------------------------------
|
|
void SetDateTime(const Date_Time *datetime)
|
{
|
unsigned char temp = 0;
|
|
temp = datetime->second/10*16 + datetime->second%10;
|
Ds1302_Write_Byte(0x80,temp); //write second
|
|
temp = datetime->minute/10*16 + datetime->minute%10;
|
Ds1302_Write_Byte(0x82,temp); //write minute
|
|
temp = datetime->hour/10*16 + datetime->hour%10;
|
Ds1302_Write_Byte(0x84,temp); //write hour
|
|
temp = datetime->day/10*16 + datetime->day%10;
|
Ds1302_Write_Byte(0x86,temp); //write day
|
|
temp = datetime->month/10*16 + datetime->month%10;
|
Ds1302_Write_Byte(0x88,temp); //write month
|
|
temp = datetime->year/10*16 + datetime->year%10;
|
Ds1302_Write_Byte(0x8c,temp); //write year
|
}
|
//
|
|
void GetDateTime(Date_Time *datetime)
|
{
|
unsigned char temp = 0;
|
|
temp=Ds1302_Read_Byte(0x81);
|
datetime->second = ((temp/16)*10+(temp%16));
|
|
temp=Ds1302_Read_Byte(0x83);
|
datetime->minute = ((temp/16)*10+(temp%16));
|
|
temp =Ds1302_Read_Byte(0x85);
|
datetime->hour = ((temp/16)*10+(temp%16));
|
|
temp=Ds1302_Read_Byte(0x87);
|
datetime->day = ((temp/16)*10+(temp%16));
|
|
temp=Ds1302_Read_Byte(0x89);
|
datetime->month = ((temp/16)*10+(temp%16));
|
|
temp=Ds1302_Read_Byte(0x8d);
|
datetime->year = ((temp/16)*10+(temp%16));
|
}
|
//-------------------------------------------
|
|
void Ds1302_Write_Byte(const unsigned char command, const unsigned char data)
|
{
|
unsigned char n;
|
|
IO_Direction_OUT; //ÉèÖÃIOÒý½ÅΪÊä³ö
|
SCLK_DS1302_0; //SCLÇåÁã
|
RST_DS1302_1; //ÖÃλRST
|
_delay_us(2);
|
|
for(n=0; n<8; n++)
|
{
|
if((command>>n) & 0x01)
|
IO_DS1302_1;//SDAΪ"1"
|
else
|
IO_DS1302_0;//SDAΪ"0"
|
SCLK_DS1302_1;//SCLÖÃλ
|
SCLK_DS1302_0;//SCLÇåÁã
|
}
|
|
for(n=0; n<8; n++)
|
{
|
if((data>>n) & 0x01)
|
IO_DS1302_1;//SDAΪ"1"
|
else
|
IO_DS1302_0;//SDAΪ"0"
|
SCLK_DS1302_1;//SCLÖÃλ
|
SCLK_DS1302_0;//SCLÇåÁã
|
}
|
|
RST_DS1302_0;//ÇåÁãRST
|
SCLK_DS1302_0;
|
}
|
//
|
|
|
|
unsigned char Ds1302_Read_Byte(const unsigned char command)
|
{
|
unsigned char n=0;
|
unsigned char data=0;
|
|
IO_Direction_OUT; //ÉèÖÃIOÒý½ÅΪÊä³ö
|
SCLK_DS1302_0; //SCLÇåÁã
|
RST_DS1302_1; //ÖÃλRST
|
_delay_us(2);
|
|
for(n=0; n<=7; n++)
|
{
|
if((command>>n) & 0x01)
|
IO_DS1302_1;//SDAΪ"1"
|
else
|
IO_DS1302_0;//SDAΪ"0"
|
SCLK_DS1302_1;//SCLÖÃλ
|
SCLK_DS1302_0;//SCLÇåÁã
|
}
|
|
IO_Direction_IN; //ÉèÖÃIOÒý½ÅΪÊäÈë
|
_delay_us(2);
|
|
for(n=0; n<=7; n++)
|
{
|
SCLK_DS1302_1; //SCLÖÃλ
|
if(Data_DS1302_1) //¶ÁÈ¡DS1302Êä³öµÄÊý¾Ý
|
data += 0x80;
|
if(n<7)
|
data >>= 1;
|
SCLK_DS1302_0; //SCLÇåÁã
|
}
|
|
SCLK_DS1302_0; //SCLÇåÁã
|
RST_DS1302_0; //ÇåÁãRST
|
|
IO_Direction_OUT; //»Ö¸´IOÒý½ÅΪÊä³ö
|
return data;
|
}
|
//
|