Files
STC89C51-Perpetual-Calendar/Keil Code/Revise_1/ds1302.c
T

155 lines
4.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include"ds1302.h"
//---DS1302д��Ͷ�ȡʱ����ĵ�ַ����---//
//---���ʱ�������� ���λ��дλ;-------//
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
//---DS1302ʱ�ӳ�ʼ��2013��1��1�����ڶ�12��00��00�롣---//
//---�洢˳�������ʱ��������,�洢��ʽ����BCD��---//
uchar TIME[7] = {0, 0, 0x12, 0x01, 0x01, 0x02, 0x13};
/*******************************************************************************
* �� �� �� : Ds1302Write
* �������� : ��DS1302�����ַ+���ݣ�
* �� �� : addr,dat
* �� �� : ��
*******************************************************************************/
void Ds1302Write(uchar addr, uchar dat)
{
uchar n;
RST = 0;
_nop_();
SCLK = 0;//�Ƚ�SCLK�õ͵�ƽ��
_nop_();
RST = 1; //Ȼ��RST(CE)�øߵ�ƽ��
_nop_();
for (n=0; n<8; n++)//��ʼ���Ͱ�λ��ַ����
{
DSIO = addr & 0x01;//���ݴӵ�λ��ʼ����
addr >>= 1;
SCLK = 1;//������������ʱ��DS1302��ȡ����
_nop_();
SCLK = 0;
_nop_();
}
for (n=0; n<8; n++)//д��8λ����
{
DSIO = dat & 0x01;
dat >>= 1;
SCLK = 1;//������������ʱ��DS1302��ȡ����
_nop_();
SCLK = 0;
_nop_();
}
RST = 0;//�������ݽ���
_nop_();
}
/*******************************************************************************
* �� �� �� : Ds1302Read
* �������� : ��ȡһ����ַ������
* �� �� : addr
* �� �� : dat
*******************************************************************************/
uchar Ds1302Read(uchar addr)
{
uchar n,dat,dat1;
RST = 0;
_nop_();
SCLK = 0;//�Ƚ�SCLK�õ͵�ƽ��
_nop_();
RST = 1;//Ȼ��RST(CE)�øߵ�ƽ��
_nop_();
for(n=0; n<8; n++)//��ʼ���Ͱ�λ��ַ����
{
DSIO = addr & 0x01;//���ݴӵ�λ��ʼ����
addr >>= 1;
SCLK = 1;//������������ʱ��DS1302��ȡ����
_nop_();
SCLK = 0;//DS1302�½���ʱ����������
_nop_();
}
_nop_();
for(n=0; n<8; n++)//��ȡ8λ����
{
dat1 = DSIO;//�����λ��ʼ����
dat = (dat>>1) | (dat1<<7);
SCLK = 1;
_nop_();
SCLK = 0;//DS1302�½���ʱ����������
_nop_();
}
RST = 0;
_nop_(); //����ΪDS1302��λ���ȶ�ʱ��,����ġ�
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
return dat;
}
/*******************************************************************************
* �� �� �� : Ds1302Init
* �������� : ��ʼ��DS1302.
* �� �� : ��
* �� �� : ��
*******************************************************************************/
void Ds1302Init()
{
uchar n;
uchar sec;
// ���CHλ�ж��Ƿ��ѳ�ʼ��
sec = Ds1302Read(0x81);
if(!(sec & 0x80)) // CH=0, ʱ���Ѿ����У�������д
return;
// ��һ�ε翪��, д��Ĭ��ʱ��
Ds1302Write(0x8E,0X00);
for (n=0; n<7; n++)
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80);
}
void Ds1302WriteTime()
{
uchar n;
Ds1302Write(0x8E,0X00);
for (n=0; n<7; n++)
{
Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);
}
Ds1302Write(0x8E,0x80);
}
/*******************************************************************************
* �� �� �� : Ds1302ReadTime
* �������� : ��ȡʱ����Ϣ
* �� �� : ��
* �� �� : ��
*******************************************************************************/
void Ds1302ReadTime()
{
uchar n;
for (n=0; n<7; n++)//��ȡ7���ֽڵ�ʱ���źţ�����ʱ��������
{
TIME[n] = Ds1302Read(READ_RTC_ADDR[n]);
}
}