標(biāo)題: 基于stc12c5a60s2的ds1302掉電后繼續(xù)記錄時(shí)間 [打印本頁(yè)]

作者: a164417839    時(shí)間: 2018-5-26 15:01
標(biāo)題: 基于stc12c5a60s2的ds1302掉電后繼續(xù)記錄時(shí)間
#include"1302.h"
/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時(shí)鐘初始化2013年1月1日星期二12點(diǎn)00分00秒。---//
//---存儲(chǔ)順序是秒分時(shí)日月周年,存儲(chǔ)格式是用BCD碼---//
uchar TIME[7]={0,0x00,0x12,0x01,0x01,0x02,0x13};

/*******************************************************************************
* 函 數(shù) 名         : Ds1302Write
* 函數(shù)功能                   : 向DS1302命令(地址+數(shù)據(jù))
* 輸    入         : addr,dat
* 輸    出         : 無(wú)
*******************************************************************************/

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;//數(shù)據(jù)從低位開始傳送
                addr >>= 1;
                SCLK = 1;//數(shù)據(jù)在上升沿時(shí),DS1302讀取數(shù)據(jù)
                _nop_();
                SCLK = 0;
                _nop_();
        }
        for (n=0; n<8; n++)//寫入8位數(shù)據(jù)
        {
                DSIO = dat & 0x01;
                dat >>= 1;
                SCLK = 1;//數(shù)據(jù)在上升沿時(shí),DS1302讀取數(shù)據(jù)
                _nop_();
                SCLK = 0;
                _nop_();       
        }       
                 
        RST = 0;//傳送數(shù)據(jù)結(jié)束
        _nop_();
}

/*******************************************************************************
* 函 數(shù) 名         : Ds1302Read
* 函數(shù)功能                   : 讀取一個(gè)地址的數(shù)據(jù)
* 輸    入         : 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;//數(shù)據(jù)從低位開始傳送
                addr >>= 1;
                SCLK=1;//數(shù)據(jù)在上升沿時(shí),DS1302讀取數(shù)據(jù)
                _nop_();
                SCLK = 0;//DS1302下降沿時(shí),放置數(shù)據(jù)
                _nop_();
        }
        _nop_();
        for(n=0; n<8; n++)//讀取8位數(shù)據(jù)
        {
                dat1 = DSIO;//從最低位開始接收
                dat = (dat>>1) | (dat1<<7);
                SCLK = 1;
                _nop_();
                SCLK = 0;//DS1302下降沿時(shí),放置數(shù)據(jù)
                _nop_();
        }

        RST = 0;
        _nop_();        //以下為DS1302復(fù)位的穩(wěn)定時(shí)間,必須的。
        SCLK = 1;
        _nop_();
        DSIO = 0;
        _nop_();
        DSIO = 1;
        _nop_();
        return dat;       
}

/*******************************************************************************
* 函 數(shù) 名         : Ds1302Init
* 函數(shù)功能                   : 初始化DS1302.
* 輸    入         : 無(wú)
* 輸    出         : 無(wú)
*******************************************************************************/
void Ds1302Init()
{

        uchar n;
        RST=0;
        SCLK=0;
        Ds1302Write(0x8E,0X00);                 //禁止寫保護(hù),就是關(guān)閉寫保護(hù)功能
        Ds1302Write(0x8F,0x80);
        for (n=0; n<7; n++)//寫入7個(gè)字節(jié)的時(shí)鐘信號(hào):分秒時(shí)日月周年
        {
                Ds1302Write(WRITE_RTC_ADDR[n],TIME[n]);       
        }
        Ds1302Write(0x8E,0x80);                 //打開寫保護(hù)功能
       
}


void Ds1302ReadTime()
{
        uchar n;
        for (n=0; n<7; n++)//讀取7個(gè)字節(jié)的時(shí)鐘信號(hào):分秒時(shí)日月周年
        {
                TIME[n]=Ds1302Read(READ_RTC_ADDR[n]);
        }
               
}

請(qǐng)問(wèn)如果要斷電顯示應(yīng)該怎么編寫初始化函數(shù)比較好呢~謝謝


作者: ahshmj    時(shí)間: 2018-5-28 08:50
斷電怎么可以顯示呢?難道顯示部分不需要電嗎?一般設(shè)計(jì)中,1302是有后備電源的,但僅僅能維持內(nèi)部時(shí)鐘正常走時(shí),不能維持外部的電路或芯片工作,更難以維持顯示部分工作。




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1