📄 ds_12887.c
字号:
/*========= DS 12887 RTC chip routines ========
* All required rotuines for the RTC chip.
* The Chip is used in non-multiplexed mode.
* All the control signals are generated by port bits as per the
timing diagram in data sheet.
* The Intel Timing logic is used
==============================================*/
/*==============Include Files =================*/
#include <reg52.h>
#include <stdio.h>
#include <DS_12887.h>
/*============== Bit Defines ==================*/
sbit RTC_AS = P1^1; // Address Latch
sbit RTC_RW = P1^2; // Read !Write
sbit RTC_DS = P1^3; // Data Select
sbit RTC_CS = P1^4; // !Chip Select;
sbit Min_Adj = P2^5; // IncPB
sbit Hour_Adj = P2^6; // DecPB
sbit Set_Clk = P2^7; // EnterPB
bit F_Clk_Adj = 0;
extern unsigned int hour, minute, second ;
extern unsigned char string[32];
/*=============================================*/
#define RTC_PORT P3 // P3 connected to RTC ( 1 byte )
sfr P3MDOUT = 0xA7 ;
/*==========Function Prototypes ===============*/
void RTC_Byte_Wr(unsigned char, unsigned char);
unsigned char RTC_Byte_Rd (unsigned char);
void RTC_Init (void);
unsigned int BCDtoBin(unsigned char);
unsigned int BinToBCD(unsigned char);
extern void display(char *);
/*=============================================*/
/*=============================================*/
unsigned char RTC_Byte_Rd ( unsigned char address)
{
unsigned char RdByte;
P3MDOUT = 0xFF;
RTC_DS = 1;
RTC_RW = 1;
RTC_CS = 0;
RTC_AS = 1;
P3 = address;
RTC_AS = 0; // Latch the address
RTC_DS = 0;
P3MDOUT = 0x00;
RdByte = P3;
RTC_DS = 1; // Read the data from RTC into P3
P3MDOUT = 0xFF;
RTC_CS = 1;
return RdByte;
}
/*===============================================*/
void RTC_Byte_Wr(unsigned char address, unsigned char byte)
{
P3MDOUT = 0xFF;
RTC_DS = 1;
RTC_RW = 1;
RTC_CS = 0;
RTC_AS = 1;
P3 = address;
RTC_AS = 0; // Latch the address
RTC_RW = 0;
P3 = byte;
RTC_RW = 1; // Write the data into RTC
RTC_CS = 1;
}
/*===============================================*/
void RTC_Init (void)
{
RTC_Byte_Wr ( REGA, 0x24); // A.0=A.1=A.2=A.3 = 0 for 2Hz sq.wave
// A.4=0,A.5=1,A.6=0 for Osc. On
RTC_Byte_Wr ( REGB, 0x0A); // B.1=1 for 24Hr mode;
// B.2=0 for BCD mode;
// B.3=0 Sq Wave enab.
}
/*===============================================*/
void ReadTime (void)
{
RTC_Byte_Wr ( REGB, 0x8A); // Stop clock register updates
hour = BCDtoBin ( RTC_Byte_Rd (CLK_HR));
minute = BCDtoBin ( RTC_Byte_Rd (CLK_MIN));
second = BCDtoBin ( RTC_Byte_Rd (CLK_SEC));
RTC_Byte_Wr ( REGB, 0x0A); // Restart clock register update
}
/*==============================================*/
void set_CurrentTime (void)
{
if (Min_Adj == 0 ) // User has pressed the Min PB
{
F_Clk_Adj = 1; // Stop clock updates in MAIN..
if ( minute < 60 )
{
++ minute;
sprintf(string, "TimeSet Min :%02u ", minute );
display(string); // Display the modified time
}
else
{
minute = 0; // If minute value > 59, then 0
}
}
//==============================================
if (Hour_Adj == 0 )
{
F_Clk_Adj = 1;
if ( hour < 24 )
{
hour++;
sprintf(string, "TimeSet Hour: %02u ", hour);
display(string);
}
else
{
hour = 0;
}
}
//=============================================
if ( Set_Clk == 0 )
{
F_Clk_Adj = 1;
RTC_Byte_Wr ( REGB, 0x8A); // Stop clock register updates
RTC_Byte_Wr ( CLK_SEC, 0);
RTC_Byte_Wr ( CLK_MIN, BinToBCD (minute));
RTC_Byte_Wr ( CLK_HR, BinToBCD (hour));
RTC_Byte_Wr ( REGB, 0x0A); // Start clock register updates
F_Clk_Adj = 0;
}
}
/*=============================================*/
unsigned int BCDtoBin(unsigned char val) // To read from RTC.
{
val -= (val/0x10)*0x06;
return val;
}
/*=============================================*/
unsigned int BinToBCD(unsigned char temp) // To write to RTC
{
temp += (temp/0x0A)*0x06;
return temp;
}
/*=============================================*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -