📄 ds12c887.c
字号:
/*************************************************
File name: DS12C887.c
Author: Jinku Hu
Version: 1.0
Date: 2008.06.19
Description: All fuctions related with RTC DS12C887 is contained
Others:
Function List:
History:
Date Author Modification
2008.06.19 Jinku Hu Build up
*************************************************/
// Define interface between DS12C887 and MCU
sbit DS12C887_RST = P1^6;
sbit DS12C887_CS = P2^5;
// Define constant related with Reg in DS12C887
#define CMD_START_DS12C887 0x20 // Start DS12C887
#define CMD_START_OSCILLATOR 0x70 // Start Oscillator
#define CMD_CLOSE_DS12C887 0x30 // Close DS12C887
// Set bit with OR, Clear bit with AND
#define MASK_SETB_SET 0x80 // Unable update
#define MASK_CLR_SET 0x7f // Enable update
#define MASK_SETB_DM 0x04 // Binary format
#define MASK_CLR_DM 0xfb // BCD format
#define MASK_SETB_2412 0x02 // 24 hours format
#define MASK_CLR_2412 0xfd // 12 hours format
#define MASK_SETB_DSE 0x01 // Daylight Savings Enable
#define MASK_CLR_DSE 0xfe // Daylight Savings Unable
// Define Reg of DS12C887
#define DS12C887_Second XBYTE[0xC000] //Second
#define DS12C887_Minute XBYTE[0xC002] //Minute
#define DS12C887_Hour XBYTE[0xC004] //Hour
#define DS12C887_Week XBYTE[0xC006] //Week
#define DS12C887_Day XBYTE[0xC007] //Day
#define DS12C887_Month XBYTE[0xC008] //Month
#define DS12C887_Year XBYTE[0xC009] //Year
#define DS12C887_RegA XBYTE[0xC00a] //Register A,A.7 to check busy or not
#define DS12C887_RegB XBYTE[0xC00b] //Register B,B.7
// B.2 BCD cod or Binary:1 Binary
// B.1 format of hour bytes 24/12
// 1 24 hour format
#define DS12C887_RegC XBYTE[0xC00c] //Register C
#define DS12C887_RegD XBYTE[0xC00d] //Register D
#define DS12C887_Century XBYTE[0xC032] //Century
unsigned char DS12C887_Century_Binary; //Binary mode of Century Reg
/*************************************************
Function: DS12C887_Start
Description: Start DS12C887.
It is used only once when DS12C887 is first used
Calls:
Called By:
Input: NULL
Output: NULL
Return:
Others:
History:
Date Author Modification
2008.06.19 Jinku Hu Build Up
*************************************************/
void DS12C887_Start(void)
{
DS12C887_RegA = CMD_START_DS12C887;
}
/*************************************************
Function: DS12C887_Start
Description: Close DS12C887.
It's seldom used.
Calls:
Called By:
Input: NULL
Output: NULL
Return:
Others:
History:
Date Author Modification
2008.06.19 Jinku Hu Build Up
*************************************************/
void DS12C887_Close(void)
{
DS12C887_RegA = CMD_CLOSE_DS12C887;
}
/*************************************************
Function: DS12C887_Initial
Description: Initialize DS12C887.
Calls: DS12C887_Start
Called By:
Input: NULL
Output: NULL
Return:
Others:
History:
Date Author Modification
2008.06.19 Jinku Hu Build Up
*************************************************/
void DS12C887_Initial()
{
DS12C887_RST = 1; //Unable Reset of DS12C887
DS12C887_Start();
DS12C887_RegB = DS12C887_RegB | MASK_SETB_SET; /*Unable to update*/
DS12C887_RegB = DS12C887_RegB | MASK_SETB_DM | MASK_SETB_2412
& MASK_CLR_DSE; /*Binary code, 24 hours format, Daylight Saving forbidden*/
DS12C887_Century = 0x20; /*Set Century Reg as 21*/
DS12C887_RegB = DS12C887_RegB & MASK_CLR_SET; /* Enable update */
}
/*************************************************
Function: DS12C887_Century_Get
Description: Get century data from DS12C887
Calls:
Called By:
Input: NULL
Output: NULL
Return: DS12C887_Century
Others:
History:
Date Author Modification
2008.06.19 Jinku Hu Build Up
*************************************************/
unsigned char DS12C887_Century_Get(void)
{
if(DS12C887_Century == 0x20)
return(20);
else
return(19);
}
/*************************************************
Function: DS12C887_Time_Set
Description: Set time of DS12C887
Calls:
Called By:
Input: chSeconds、chMinutes、chHours is in Binary code
Output: NULL
Return: DS12C887_Minute
Others:
History:
Date Author Modification
2008.06.19 Jinku Hu Build Up
*************************************************/
void DS12C887_Time_Set(unsigned char chSeconds,
unsigned char chMinutes,unsigned char chHours)
{
DS12C887_RegB = DS12C887_RegB | MASK_SETB_SET; /*Unable update*/
DS12C887_Second = chSeconds;
DS12C887_Minute = chMinutes;
DS12C887_Hour = chHours;
DS12C887_RegB = DS12C887_RegB & MASK_CLR_SET; /*Enable update*/
}
/*************************************************
Function: DS12C887_Time_Set
Description: Set time of DS12C887
Calls:
Called By:
Input: chDate、chMonth、chYear is in Binary code
Output: NULL
Return: DS12C887_Minute
Others:
History:
Date Author Modification
2008.06.19 Jinku Hu Build Up
*************************************************/
void DS12C887_Date_Set(unsigned char chDate,unsigned char chMonth,unsigned char chYear)
{
DS12C887_RegB = DS12C887_RegB | MASK_SETB_SET; /*Unable update*/
DS12C887_Day = chDate;
DS12C887_Month = chMonth;
DS12C887_Year = chYear;
DS12C887_RegB = DS12C887_RegB & MASK_CLR_SET; /*Enable update*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -