📄 ds1302.c
字号:
#include <AT89X55.H>
#include <DS1302.H>
#include <SPI.h>
#include <8255.h>
#include <ABSACC.H>
struct Time RTC_REG={0,0, 0,1, 7,0, 5,2, 1,0, 5,0, 5,8, 0,8};
//S Min H Date M Day Y WP
void RTC_Init (void)
{unsigned char *P;
P=&RTC_REG.Sec_Reg;
Write_RTC (P, 8);
}
unsigned char Read_RTC_One_Byte (unsigned char Reg_Adress)
{unsigned char cDate;
DS1302_RST_H(); //SCK=0 RST=1
/*******************************************************************************/
SPI_WriteOneByte(Reg_Adress);
cDate= ( SPI_ReadOneByte() );
/*******************************************************************************/
DS1302_RST_L(); //RST=0
return (cDate);
}
void Write_RTC_One_Byte (unsigned char Reg_Adress,unsigned char cData)
{
DS1302_RST_H(); //SCK=0 RST=1
/*******************************************************************************/
/*******************************************************************************/
/**************** 8DS1302 Write Protect turn off ****************/
SPI_WriteOneByte(DS1302_WP_REG_Write);
SPI_WriteOneByte(DS1302_Control_Write_Enable);
/*******************************************************************************/
DS1302_RST_H(); //SCK=0 RST=1
DS1302_RST_L(); //RST=0
/*******************************************************************************/
SPI_WriteOneByte(Reg_Adress);
SPI_WriteOneByte(cData);
/*******************************************************************************/
/*******************************************************************************/
DS1302_RST_L(); //RST=0
}
/*
*********************************************************************************************
* Write RTC
*
* Description: Write RTC
*
* Arguments : dataVector is the start address of the RTC (DS1302)
* dataNum is the number of data to be written to RTC
*
*
* Returns : none
*
* Notes : struct RTC
{unsigned char Sec_Reg;
unsigned char _10Sec_Reg;
unsigned char Min_Reg;
unsigned char _10Min_Reg;
unsigned char HR_Reg;
unsigned char _10HR_Reg;
unsigned char DATE_Reg;
unsigned char _10DATE_Reg;
unsigned char MONTH_Reg;
unsigned char _10MONTH_Reg;
unsigned char DAY;
unsigned char _10DAY;
unsigned char YEAR_Reg;
unsigned char _10YEAR_Reg;
unsigned char CONTROL_Reg_L;
unsigned char CONTROL_Reg_H;
};
*
*
**********************************************************************************************
*/
void Write_RTC (unsigned char *dataVector, unsigned char dataNum)
{unsigned char cLoop;
unsigned char cDate;
DS1302_RST_H(); //SCK=0 RST=1
/**************** 8DS1302 Write Protect turn off ****************/
SPI_WriteOneByte(DS1302_WP_REG_Write);
SPI_WriteOneByte(DS1302_Control_Write_Enable);
/*******************************************************************************/
/*******************************************************************************/
DS1302_RST_L(); //RST=0
DS1302_RST_H(); //SCK=0 RST=1
/**************** Burst Write RTC ****************/
SPI_WriteOneByte(DS1302_RTC_Burst_Write); //Write command Burst_Write
for ( cLoop=0 ; cLoop<dataNum ; cLoop++,dataVector++ )
{
cDate = *dataVector & 0x0F;
dataVector++;
cDate |= ( (*dataVector) << 4 ); //Hight 4 Bit & Low 4 Bit
SPI_WriteOneByte(cDate);
}
/*******************************************************************************/
/*******************************************************************************/
DS1302_RST_L(); //RST=0
}
/*
*********************************************************************************************
* Read RTC
*
* Description: Read RTC
*
* Arguments :
*
*
* Returns : none
*
* Notes : It will write array like Fig
*Fig: struct RTC
{unsigned char Sec_Reg;
unsigned char _10Sec_Reg;
unsigned char Min_Reg;
unsigned char _10Min_Reg;
unsigned char HR_Reg;
unsigned char _10HR_Reg;
unsigned char DATE_Reg;
unsigned char _10DATE_Reg;
unsigned char MONTH_Reg;
unsigned char _10MONTH_Reg;
unsigned char DAY;
unsigned char _10DAY;
unsigned char YEAR_Reg;
unsigned char _10YEAR_Reg;
unsigned char CONTROL_Reg_L;
unsigned char CONTROL_Reg_H;
};
*
*
**********************************************************************************************
*/
void Read_RTC (void)
{unsigned char cLoop;
unsigned char *P;
unsigned char cDate;
DS1302_RST_H(); //SCK=0 RST=1
/**************** Burst Read RTC ****************/
P=&RTC_REG.Sec_Reg;
SPI_WriteOneByte(DS1302_RTC_Burst_Read); //Write command Burst_Read
for ( cLoop=0;cLoop<8;cLoop++,P++ )
{
cDate= ( SPI_ReadOneByte() );
*P= cDate & 0x0F; //Low 4 to
P++;
*P = ( (cDate & 0xF0) >> 4 ); //Hight 4 to
}
/*******************************************************************************/
/*******************************************************************************/
DS1302_RST_L(); //RST=0
}
/*
*********************************************************************************************
* BCD_2_ASCII
*
* Description: Conver BCD to ASCII
*
* Arguments : dataVector is the start address of the array (BCD)
* dataNum is the length of the array
*
* Returns : none
*
* Notes :
*
*
**********************************************************************************************
*/
void BCD_2_ASCII (unsigned char *dataVector, unsigned char dataNum)
{unsigned char cLoop;
//unsigned char *P;
//P=&RTC_REG.Sec_Reg;
for ( cLoop=0;cLoop < dataNum;cLoop++,dataVector++ )
*dataVector += 0x30;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -