新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > DS3231高精度時鐘模塊程序

        DS3231高精度時鐘模塊程序

        作者: 時間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
        void Ack_I2C(bit a)
        {
        if(a==0)
        SDA=0; //在此發(fā)出應(yīng)答或非應(yīng)答信號
        else
        SDA=1;
        delayus(3);
        SCL=1;
        delayus(5); //時鐘低電平周期大于4μs
        SCL=0; //清時鐘線,鉗住I2C總線以便繼續(xù)接收
        delayus(2);
        }
        uchar write_byte(uchar addr, uchar write_data)
        {
        Start_I2C();
        SendByte(DS3231_WriteAddress);
        if (ack == 0)
        return 0;
        SendByte(addr);
        if (ack == 0)
        return 0;
        SendByte(write_data);
        if (ack == 0)
        return 0;
        Stop_I2C();
        delayus(10);
        return 1;
        }
        uchar read_current()
        {
        uchar read_data;
        Start_I2C();
        SendByte(DS3231_ReadAddress);
        if(ack==0)
        return(0);
        read_data = RcvByte();
        Ack_I2C(1);
        Stop_I2C();
        return read_data;
        }
        uchar read_random(uchar random_addr)
        {
        Start_I2C();
        SendByte(DS3231_WriteAddress);
        if(ack==0)
        return(0);
        SendByte(random_addr);
        if(ack==0)
        return(0);
        return(read_current());
        }
        void ModifyTime(uchar yea,uchar mon,uchar da,uchar hou,uchar min,uchar sec)
        {
        uchar temp=0;
        temp=HEX2BCD(yea);
        write_byte(DS3231_YEAR,temp); //修改年
        temp=HEX2BCD(mon);
        write_byte(DS3231_MONTH,temp); //修改月
        temp=HEX2BCD(da);
        write_byte(DS3231_DAY,temp); //修改日
        temp=HEX2BCD(hou);
        write_byte(DS3231_HOUR,temp); //修改時
        temp=HEX2BCD(min);
        write_byte(DS3231_MINUTE,temp); //修改分
        temp=HEX2BCD(sec);
        write_byte(DS3231_SECOND,temp); //修改秒
        }
        void TimeDisplay(uchar Dhour,uchar Dmin,uchar Dsec)
        {
        dis_buf[7]=dis_code[Dhour / 10]; // 時十位
        dis_buf[6]=dis_code[Dhour % 10]; // 時個位
        dis_buf[4]=dis_code[Dmin / 10]; // 分十位
        dis_buf[3]=dis_code[Dmin % 10]; // 分個位
        dis_buf[1]=dis_code[Dsec / 10]; // 秒十位
        dis_buf[0]=dis_code[Dsec % 10]; // 秒個位
        dis_buf[2]=0xbf; // 顯示"-"
        dis_buf[5]=0xbf;
        }
        void DateDisplay(uchar Dyear,uchar Dmonth,uchar Dday)
        {
        dis_buf[7]=dis_code[Dyear / 10]; // 年十位
        dis_buf[6]=dis_code[Dyear % 10]; // 年個位
        dis_buf[4]=dis_code[Dmonth / 10]; // 月十位
        dis_buf[3]=dis_code[Dmonth % 10]; // 月個位
        dis_buf[1]=dis_code[Dday / 10]; // 天十位
        dis_buf[0]=dis_code[Dday % 10]; // 天個位
        dis_buf[2]=0xbf; // 顯示"-"
        dis_buf[5]=0xbf;
        }
        void get_show_time(void)
        {
        uchar Htemp1,Htemp2,Mtemp1,Mtemp2,Stemp1,Stemp2;
        Htemp1=read_random(DS3231_HOUR); //時 24小時制
        Htemp1&=0x3f;
        Htemp2=BCD2HEX(Htemp1);
        Mtemp1=read_random(DS3231_MINUTE); //分
        Mtemp2=BCD2HEX(Mtemp1);
        Stemp1=read_random(DS3231_SECOND); //秒
        Stemp2=BCD2HEX(Stemp1);
        TimeDisplay(Htemp2,Mtemp2,Stemp2);
        }
        void get_show_date(void)
        {
        uchar Ytemp1,Ytemp2,Mtemp1,Mtemp2,Dtemp1,Dtemp2;
        Ytemp1=read_random(DS3231_YEAR); //年
        Ytemp2=BCD2HEX(Ytemp1);
        Mtemp1=read_random(DS3231_MONTH); //月
        Mtemp2=BCD2HEX(Mtemp1);
        Dtemp1=read_random(DS3231_DAY); //日
        Dtemp2=BCD2HEX(Dtemp1);
        DateDisplay(Ytemp2,Mtemp2,Dtemp2);
        }
        void get_show_Temperature(void)
        {
        uchar Ttemp1,Ttemp2,Ttemp3,Ttemp4;
        Ttemp1=read_random(DS3231_TEMPERATUREH); //溫度 高字節(jié)
        Ttemp2=BCD2HEX(Ttemp1);
        Ttemp3=read_random(DS3231_TEMPERATUREL); //溫度低字節(jié)
        Ttemp4=BCD2HEX(Ttemp3);
        DateDisplay(0,Ttemp2,Ttemp4);
        }
        void timer0() interrupt 1
        {
        TH0=0xFC;
        TL0=0x17;
        P2=0xff; // 先關(guān)閉所有數(shù)碼管
        P0=dis_buf[dis_index]; // 顯示代碼傳送到P0口
        P2=dis_digit;
        if (dis_digit & 0x80)
        dis_digit=(dis_digit << 1) | 0x1;
        else
        dis_digit=(dis_digit << 1);
        dis_index++;
        dis_index&=0x07; // 8個數(shù)碼管全部掃描完一遍之后,再回到第一個開始下一次掃描
        }
        void main()
        {
        uint ii = 0;
        RESET=0x1; //DS3231復(fù)位操作,正常操作下不需要每次都復(fù)位
        delayus(5000);
        led0=0;
        led1=0;
        led2=0;
        led3=0;
        led4=0;
        P0=0xff;
        P2=0xff;
        dis_digit=0xfe;
        dis_index=0;
        TimeDisplay(12, 5, 18);
        TMOD=0x11; // 定時器0, 1工作模式1, 16位定時方式
        TH0=0xFC;
        TL0=0x17;
        TCON=0x01;
        IE=0x82; // 使能timer0,1 中斷
        TR0=1;
        if (write_byte(DS3231_CONTROL, 0x1C) == 0)
        led0=1;
        if (write_byte(DS3231_STATUS, 0x00) == 0)
        led1=1;
        ModifyTime(10,6,13,15,30,00); //初始化時鐘,2010/6/13,15/30/00
        //小時采用24小時制
        while(1)
        {
        //get_show_date(); //顯示日期
        //get_show_Temperature(); //顯示溫度
        get_show_time(); //顯示時間
        delayus(50000);
        }
        }
        上一頁 1 2 下一頁

        關(guān)鍵詞: DS3231高精度時鐘模塊程

        評論


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

        關(guān)閉
        主站蜘蛛池模板: 道真| 澄江县| 正宁县| 吉林市| 墨江| 桃源县| 长兴县| 泗水县| 宜章县| 青海省| 沙洋县| 政和县| 拉萨市| 泾阳县| 平谷区| 迁西县| 娱乐| 曲阜市| 西丰县| 池州市| 阿克苏市| 精河县| 孝感市| 克什克腾旗| 宝鸡市| 西华县| 皮山县| 龙口市| 武清区| 胶南市| 通道| 丽水市| 江安县| 山阴县| 临江市| 镇江市| 商丘市| 富顺县| 西和县| 日土县| 西宁市|