[C51代碼]DS1302驅(qū)動
#include "Atmel/AT89X51.h"
#include "link.h"
#include "ds1302.h"
/*******************變量定義********************/
uchar co
(3 << 5) + 31,//1月
(6 << 5) + 29,//2月
(0 << 5) + 31,//3月
(3 << 5) + 30,//4月
(5 << 5) + 31,//5月
(1 << 5) + 30,//6月
(3 << 5) + 31,//7月
(6 << 5) + 31,//8月
(1 << 5) + 30,//9月
(4 << 5) + 31,//10月
(0 << 5) + 30,//11月
(2 << 5) + 31 //12月
};
uchar DataArrayYear[5];
uchar DataArrayMonth[3];
uchar DataArrayDay[3];
uchar TimeArray[9];
uchar WeekArray[7][4]={"Mon.","Tue.","Wed.","Thu.","Fri.","Sat.","Sun."};
systemtime Nowtime;
/****************************************************************
*函數(shù)功能:寫入數(shù)據(jù)到ds1302
*入口參數(shù):數(shù)據(jù)uchar Content
*出口參數(shù):無
****************************************************************/
void DS1302Write_f(uchar Content )
{
uchar i ;
for( i = 8 ; i > 0 ; i-- )
{
if( Content & 0x01 )
{
DIO=0x01;
}
else
{
DIO=0x00;
}
Content >>= 1 ;
SCLK=1;
SCLK=0;
}
}
/****************************************************************
*函數(shù)功能:讀取ds1302的數(shù)據(jù)
*入口參數(shù):無
*出口參數(shù):uchar ReadValue
****************************************************************/
uchar DS1302Read_f( void )
{
uchar i, ReadValue ;
DIO=1;
for( i = 8 ; i > 0 ; i-- )
{
ReadValue >>= 1 ;
if( DIO )
{
ReadValue |= 0x80 ;
}
else
{
ReadValue &= 0x7f ;
}
SCLK=1;
SCLK=0;
}
return ReadValue ;
}
/****************************************************************
*函數(shù)功能:從DS1302指定的地址寫入一個字節(jié)的內(nèi)容
*入口參數(shù):要寫入數(shù)據(jù)的地址:Address
寫入數(shù)據(jù)的具體值:Content
*出口參數(shù):無
****************************************************************/
void DS1302Writebyte( uchar Address, uchar Content )
{
RST=0;
SCLK=0;
RST=1;
DS1302Write_f( Address ) ;
DS1302Write_f( Content ) ;
RST=0;
SCLK=1;
}
/****************************************************************
*函數(shù)功能:從DS1302指定的地址讀出一個字節(jié)的內(nèi)容
*入口參數(shù):要讀出數(shù)據(jù)的地址:Address
*出口參數(shù):uchar ReadValue
****************************************************************/
uchar DS1302Readbyte( uchar Address )
{
uchar ReadValue ;
RST=0;
SCLK=0;
RST=1;
DS1302Write_f( Address );
ReadValue = DS1302Read_f();
RST=0;
SCLK=1;
return ReadValue ;
}
/****************************************************************
*函數(shù)功能:讀取DS1302時間
*入口參數(shù):無
*出口參數(shù):無
****************************************************************/
void DS1302Gettime(void)
{
Nowtime.Year = BCD2DEC(DS1302Readbyte(READ_YEAR)); //年
Nowtime.Month = BCD2DEC(DS1302Readbyte(READ_MONTH)); //月
Nowtime.Day = BCD2DEC(DS1302Readbyte(READ_DAY)); //日
Nowtime.Week = BCD2DEC(DS1302Readbyte(READ_WEEK)); //周
Nowtime.Hour = BCD2DEC(DS1302Readbyte(READ_HOUR)); //時
Nowtime.Minute = BCD2DEC(DS1302Readbyte(READ_MINUTE)); //分
Nowtime.Second = BCD2DEC(DS1302Readbyte(READ_SECOND)); //秒
}
/****************************************************************
*函數(shù)功能:周數(shù)調(diào)整函數(shù)
*入口參數(shù):年:y ;月:m ;日:d
*出口參數(shù):周幾uchar week
****************************************************************/
/*****************周數(shù)調(diào)整函數(shù)*******************/
uchar WeekDay20(uchar y, uchar m, uchar d)
{
uchar week, day;
day = WeekTab[m - 1]; //月表
week = day >> 5; //月星期數(shù)
day &= 0x1f; //月天數(shù)
if((m < 3) && (y & 0x03))
{ //平年
if (m == 2) day--; //平年月天數(shù)
week++; //平年月表+1
}
y = y + (y >> 2); //年+年/4
week = (week + y + d + 2) % 7; //(星期=年+年/4+月表+2日)%7
return week; //返回星期
}
/****************************************************************
*函數(shù)功能:初始化寫入DS1302時鐘寄存器的值(主程序中只需調(diào)用一次即可)
*入口參數(shù):無
*出口參數(shù):無
****************************************************************/
void DS1302Initial(void)
{
if(DS1302Readbyte( 0xc1)!= 0xf0 )
{
DS1302Writebyte( WRITE_PROTECT,0x00) ; //允許寫操作
DS1302Writebyte( WRITE_YEAR, 0x09 ) ; //年
DS1302Writebyte( WRITE_WEEK, 0x05 ) ; //星期
DS1302Writebyte( WRITE_MONTH, 0x05 ) ; //月
DS1302Writebyte( WRITE_DAY, 0x16 ) ; //日
DS1302Writebyte( WRITE_HOUR, 0x14 ) ; //小時
DS1302Writebyte( WRITE_MINUTE, 0x41 ) ; //分鐘
DS1302Writebyte( WRITE_SECOND, 0x00 ) ; //秒
DS1302Writebyte( 0x90, 0x00 ) ; //不充電
DS1302Writebyte( 0xc0, 0xf0 ) ; //判斷是否初始化一次標(biāo)識寫入
DS1302Writebyte( WRITE_PROTECT, 0x80 ) ; //禁止寫操作
DS1302Writebyte( 0xc1, 0xf0 );
}
}
/****************************************************************
*函數(shù)功能:數(shù)據(jù)轉(zhuǎn)換成ASCII碼
*入口參數(shù):無
*出口參數(shù):無
****************************************************************/
void DS1302DataConvert(void)
{
DS1302Gettime();
DataArrayYear[0] =2;
DataArrayYear[1] =0;
DataArrayYear[2] =Nowtime.Year/10+0;
DataArrayYear[3] =Nowtime.Year%10+0;
DataArrayYear[4] =