mybag1 发表于 2014-12-13 22:29:54

求助DS1302为什么会生成两个时间?

今天用DS1302做个时钟,可是设置好时间后,会不停的显示两个时间,一会儿是正常的2014年****时间,一会儿显示2010-09-09 ***错误的时间,这是怎么回事?
=======================
2014-12-13
22:22:09
2010-09-09
01:14:03
2014-12-13
22:22:11
2010-09-09
01:14:04
======================

代码:
#include <DS1302.h>
DS1302 rtc(2, 5, 7);
void setup()
{

rtc.halt(false);
rtc.writeProtect(false);

rtc.setDOW(SATURDAY);      // 设置星期
rtc.setTime(22, 56,00);   //设置时间
rtc.setDate(9, 11, 2013);   // 设置日期2013年11月9日
rtc.writeProtect(true);
Serial.begin(9600);
}
void loop()
{
char * t=rtc.getTimeStr();
char * d=rtc.getDateStr(FORMAT_LONG,FORMAT_BIGENDIAN, '-');
char * w=rtc.getDOWStr(FORMAT_LONG);
Serial.println(t);
Serial.println(d);
Serial.println(w);
delay(1000);

}


DS1302.h
/*
DS1302.h - Arduino library support for the DS1302 Trickle Charge Timekeeping Chip
Copyright (C)2010 Henning Karlsen. All right reserved

You can find the latest version of the library at
http://www.henningkarlsen.com/electronics

This library has been made to easily interface and use the DS1302 RTC with
the Arduino.

If you make any modifications or improvements to the code, I would appreciate
that you share the code with me so that I might include it in the next release.
I can be contacted through http://www.henningkarlsen.com/electronics/contact.php

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA02110-1301USA
*/
#ifndef DS1302_h
#define DS1302_h

#if defined(ARDUINO) && ARDUINO >= 100
        #include "Arduino.h"
#else
        #include "WProgram.h"
#endif

#define FORMAT_SHORT        1
#define FORMAT_LONG        2

#define FORMAT_LITTLEENDIAN        1
#define FORMAT_BIGENDIAN        2
#define FORMAT_MIDDLEENDIAN        3

#define MONDAY                1
#define TUESDAY                2
#define WEDNESDAY        3
#define THURSDAY        4
#define FRIDAY                5
#define SATURDAY        6
#define SUNDAY                7

#define TCR_D1R2K        165
#define TCR_D1R4K        166
#define TCR_D1R8K        167
#define TCR_D2R2K        169
#define TCR_D2R4K        170
#define TCR_D2R8K        171
#define TCR_OFF                92

class Time
{
public:
        uint8_t                hour;
        uint8_t                min;
        uint8_t                sec;
        uint8_t                date;
        uint8_t                mon;
        uint16_t        year;
        uint8_t                dow;

                Time();
};

class DS1302_RAM
{
public:
        byte        cell;

                DS1302_RAM();
};

class DS1302
{
public:
                DS1302(uint8_t ce_pin, uint8_t data_pin, uint8_t sclk_pin);
        Time        getTime();
        void        setTime(uint8_t hour, uint8_t min, uint8_t sec);
        void        setDate(uint8_t date, uint8_t mon, uint16_t year);
        void        setDOW(uint8_t dow);

        char        *getTimeStr(uint8_t format=FORMAT_LONG);
        char        *getDateStr(uint8_t slformat=FORMAT_LONG, uint8_t eformat=FORMAT_LITTLEENDIAN, char divider='.');
        char        *getDOWStr(uint8_t format=FORMAT_LONG);
        char        *getMonthStr(uint8_t format=FORMAT_LONG);

        void        halt(bool value);
        void        writeProtect(bool enable);
        void        setTCR(uint8_t value);

        void                writeBuffer(DS1302_RAM r);
        DS1302_RAM        readBuffer();
        void                poke(uint8_t addr, uint8_t value);
        uint8_t                peek(uint8_t addr);

private:
        uint8_t _ce_pin;
        uint8_t _data_pin;
        uint8_t _sclk_pin;
        uint8_t _burstArray;

        uint8_t        _readByte();
        void        _writeByte(uint8_t value);
        uint8_t        _readRegister(uint8_t reg);
        void         _writeRegister(uint8_t reg, uint8_t value);
        void        _burstRead();
        uint8_t        _decode(uint8_t value);
        uint8_t        _decodeH(uint8_t value);
        uint8_t        _decodeY(uint8_t value);
        uint8_t        _encode(uint8_t vaule);
};
#endif

timer 发表于 2014-12-14 04:54:43

本帖最后由 timer 于 2014-12-14 05:16 编辑

确认接线和1302有无故障
依程式码应该依序秀时间,日期,星期

zoologist 发表于 2014-12-14 07:44:49

没遇到过 会不会是 rtc.writeProtect(); 导致的

mybag1 发表于 2014-12-14 08:33:28

连线是没有问题的,我怀疑是我的1302坏了:L
页: [1]
查看完整版本: 求助DS1302为什么会生成两个时间?