新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > MSP430單片機的PCF8563時鐘芯片C語言驅(qū)動程序

        MSP430單片機的PCF8563時鐘芯片C語言驅(qū)動程序

        作者: 時間:2016-11-27 來源:網(wǎng)絡(luò) 收藏
        #define NOP _NOP();_NOP()
        #define NowSec cNowTime[0]
        #define NowMin cNowTime[1]
        #define NowHour cNowTime[2]
        #define NowDate cNowTime[3]
        #define NowMonth cNowTime[4]
        #define NowYear cNowTime[5]
        #define SetSec cSetTime[0]
        #define SetMin cSetTime[1]
        #define SetHour cSetTime[2]
        #define SetDate cSetTime[3]
        #define SetMonth cSetTime[4]
        #define SetYear cSetTime[5]
        uchar Temp_Count;
        uchar cSetTime[6];
        uchar cNowTime[6];
        void Timer_Out_Byte(uchar OutByte);
        uchar Timer_In_Byte(void);
        void ReadRealtime(void)
        {
        NOP;
        HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0;
        NOP;
        HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1;
        Timer_Out_Byte(0xBF); //Read, Burst Mode
        for(TimeCount=0;TimeCount<6;TimeCount++)
        {
        if (TimeCount==5)
        Timer_In_Byte(); //dummy read.
        cNowTime[TimeCount]=Timer_In_Byte();
        //change BCD to uchar
        cNowTime[TimeCount]=(cNowTime[TimeCount]/0x10)*10+(cNowTime[TimeCount]%0x10);
        }
        NOP;
        HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0;
        }
        void WriteRealtime(void)
        {
        NOP;
        HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0; //for sure to close rest.
        NOP;
        HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0;
        NOP;
        HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1;
        Timer_Out_Byte(0x8E); //Write,Single Mode
        Timer_Out_Byte(0x00); //Write, Protect Byte=0, Disable protect
        NOP;
        HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0;
        NOP;
        HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1;
        Timer_Out_Byte(0x80); //Write,Single Mode
        Timer_Out_Byte(0x00); //Write, OSC enalbe, old second distroied
        NOP;
        HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0;
        for(TimeCount=0;TimeCount<6;TimeCount++)
        {
        //change uchar to BCD
        cSetTime[TimeCount]=(cSetTime[TimeCount]/10)*0x10+(cSetTime[TimeCount]);
        }
        NOP;
        HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1;
        Timer_Out_Byte(0XBE); //Write,Burst Mode
        Timer_Out_Byte(SetSec & 0x7F); //OSC enable for sure.
        Timer_Out_Byte(SetMin);
        Timer_Out_Byte(SetHour & 0x7F); //24 Hour Mode
        Timer_Out_Byte(SetDate);
        Timer_Out_Byte(SetMonth);
        Timer_Out_Byte(0x00);
        Timer_Out_Byte(SetYear);
        Timer_Out_Byte(0x00);
        NOP;
        HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0;
        NOP;
        HT1381_OUTPORT |= Timer_Rest; //Timer_Rest=1;
        Timer_Out_Byte(0x8E); //Write,Single Mode
        Timer_Out_Byte(0x80); //Write, Protect Byte=0, enable protect
        NOP;
        HT1381_OUTPORT &=~Timer_Rest; //Timer_Rest=0;
        }
        void Timer_Out_Byte(uchar OutByte)
        {
        uchar Timer_Count;
        for (Timer_Count=0;Timer_Count<8;Timer_Count++)
        {
        HT1381_DIRPORT |= Timer_SDA; //output
        if ((OutByte & 0x01) == 0)
        HT1381_OUTPORT &=~Timer_SDA;
        else
        HT1381_OUTPORT |= Timer_SDA;
        NOP;
        HT1381_OUTPORT |= Timer_SCL;
        NOP;
        HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0;
        OutByte = OutByte >> 1;
        }
        }
        uchar Timer_In_Byte(void)
        {
        uchar InByte,Timer_Count;
        InByte=0x00;
        HT1381_DIRPORT &=~Timer_SDA; //Timer_SDA=1;//input
        for (Timer_Count=0;Timer_Count<8;Timer_Count++)
        {
        InByte >>= 1;
        NOP;
        HT1381_OUTPORT |= Timer_SCL;
        if(HT1381_INPORT&Timer_SDA)
        InByte |= 0x80;
        else
        InByte &= 0x7F;
        NOP;
        HT1381_OUTPORT &=~Timer_SCL; //Timer_SCL=0;
        }
        return(InByte);
        }


        關(guān)鍵詞: MSP430單片機PCF8563時鐘芯

        評論


        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 白沙| 宝清县| 东平县| 岱山县| 涿鹿县| 鸡泽县| 原阳县| 梓潼县| 淳化县| 调兵山市| 禹州市| 双峰县| 兴宁市| 鹤山市| 芒康县| 葫芦岛市| 临安市| 睢宁县| 佛学| 揭东县| 繁峙县| 巴彦淖尔市| 余江县| 斗六市| 县级市| 绩溪县| 定日县| 新巴尔虎右旗| 靖州| 新干县| 松潘县| 民丰县| 临澧县| 安塞县| 宣化县| 海林市| 铁岭县| 东乌珠穆沁旗| 老河口市| 广水市| 垦利县|