📄 复件 ds1302.c
字号:
*/
//#include "type.h"
//#include "fat.h"
//#include "list.h"
//#include "oriole.h"
#include "type.h"
#include "gpio.h"
#include "ds1302.h"
#include <intrins.h> //_nop_();
//extern SYSTEM_INFO xdata gSystem;
extern GPIO volatile xdata *gGpio;
#define REG_RTC_PORT_OUT gGpio->PortA.rGPIO_OUT
#define REG_RTC_PORT_IN gGpio->PortA.rGPIO_IN
#define CTRL_RTC_PORT gGpio->PortA.rGPIO_OE
#define MASK_RTC_RST 0x40
#define MASK_RTC_CLK 0x10
#define MASK_RTC_DAT 0x20
#define SET_REG(reg, mask) (reg |= mask)
#define CLEAR_REG(reg, mask) (reg &= (~mask))
#define GET_REG(reg, mask) (reg & mask)
static void RTCInputByte(unsigned char ucDa)
{
unsigned char i;
for(i = 0; i < 8; ++i)
{
if (ucDa & 0x01)
{
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_DAT);
}
else
{
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_DAT);
}
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
ucDa >>= 1;
}
}
static unsigned char RTCOutputByte(void)
{
unsigned char i;
unsigned char Dat = 0;
CLEAR_REG(CTRL_RTC_PORT, MASK_RTC_DAT);
for(i = 0; i < 8; ++i)
{
Dat >>= 1;
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
if (GET_REG(REG_RTC_PORT_IN, MASK_RTC_DAT))
{
Dat |= 0x80;
}
}
SET_REG(CTRL_RTC_PORT, MASK_RTC_DAT);
return Dat;
}
void RTC_W1302(unsigned char ucAddr, unsigned char ucDa)
{
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
RTCInputByte(ucAddr);
RTCInputByte(ucDa);
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
}
unsigned char RTC_R1302(unsigned char ucAddr)
{
unsigned char ucDa;
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
RTCInputByte(ucAddr);
ucDa = RTCOutputByte();
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
return ucDa;
}
void RTC_BurstR1302T(unsigned char *pSecDa)
{
unsigned char i;
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
RTCInputByte(0xbf);
for (i = 0; i < 8; ++i)
{
do
{
*pSecDa = RTCOutputByte();
}
while(*pSecDa == 0xff);
pSecDa++;
}
SET_REG(REG_RTC_PORT_OUT, MASK_RTC_CLK);
CLEAR_REG(REG_RTC_PORT_OUT, MASK_RTC_RST);
}
/*
void RTC_BurstW1302R(unsigned char *pReDa)
{
unsigned char i;
RTC_W1302(0x8e, 0x00);
gGpio->PortA.rGPIO_OUT &= (~MASK_RTC_RST); //DS_RST = 0;
gGpio->PortA.rGPIO_OUT &= (~MASK_RTC_CLK); //DS_CLK = 0;
gGpio->PortA.rGPIO_OUT |= MASK_RTC_RST; //DS_RST = 1;
RTCInputByte(0xfe);
for (i = 0; i < 31; ++i)
{
RTCInputByte(*pReDa);
pReDa++;
}
gGpio->PortA.rGPIO_OUT |= MASK_RTC_CLK; //DS_CLK = 1;
gGpio->PortA.rGPIO_OUT &= (~MASK_RTC_RST); //DS_RST =0;
RTC_W1302(0x8e, 0x80);
}
*/
void InitDS1302(void)
{
SET_REG(CTRL_RTC_PORT, (MASK_RTC_RST | MASK_RTC_CLK | MASK_RTC_DAT));
}
/*
void SaveWord(unsigned char addr, unsigned int val)
{
unsigned char temp;
RTC_W1302(0x8e, 0); //disable wp
temp = (unsigned char)val;
RTC_W1302(addr, temp);
temp = (unsigned char)(val >> 8);
RTC_W1302((addr + 2), temp);
RTC_W1302(0x8e, 0x80); //enable wp
}
unsigned int LoadWord(unsigned char addr)
{
unsigned int val = 0;
val = RTC_R1302(addr + 2);
val <<= 8;
val += RTC_R1302(addr);
return val;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -