极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 19871|回复: 3

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

[复制链接]
发表于 2014-12-13 22:29:54 | 显示全部楼层 |阅读模式
今天用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
======================

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

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

  7.   rtc.setDOW(SATURDAY);        // 设置星期
  8.   rtc.setTime(22, 56,00);     //设置时间
  9.   rtc.setDate(9, 11, 2013);   // 设置日期2013年11月9日
  10.   rtc.writeProtect(true);
  11.   Serial.begin(9600);
  12. }
  13. void loop()
  14. {
  15.   char * t=rtc.getTimeStr();
  16.   char * d=rtc.getDateStr(FORMAT_LONG,FORMAT_BIGENDIAN, '-');
  17.   char * w=rtc.getDOWStr(FORMAT_LONG);
  18.   Serial.println(t);
  19.   Serial.println(d);
  20.   Serial.println(w);
  21.   delay(1000);
  22.   
  23. }
复制代码


DS1302.h
  1. /*
  2.   DS1302.h - Arduino library support for the DS1302 Trickle Charge Timekeeping Chip
  3.   Copyright (C)2010 Henning Karlsen. All right reserved
  4.   
  5.   You can find the latest version of the library at
  6.   http://www.henningkarlsen.com/electronics

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

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

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

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

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

  26. #if defined(ARDUINO) && ARDUINO >= 100
  27.         #include "Arduino.h"
  28. #else
  29.         #include "WProgram.h"
  30. #endif

  31. #define FORMAT_SHORT        1
  32. #define FORMAT_LONG        2

  33. #define FORMAT_LITTLEENDIAN        1
  34. #define FORMAT_BIGENDIAN        2
  35. #define FORMAT_MIDDLEENDIAN        3

  36. #define MONDAY                1
  37. #define TUESDAY                2
  38. #define WEDNESDAY        3
  39. #define THURSDAY        4
  40. #define FRIDAY                5
  41. #define SATURDAY        6
  42. #define SUNDAY                7

  43. #define TCR_D1R2K        165
  44. #define TCR_D1R4K        166
  45. #define TCR_D1R8K        167
  46. #define TCR_D2R2K        169
  47. #define TCR_D2R4K        170
  48. #define TCR_D2R8K        171
  49. #define TCR_OFF                92

  50. class Time
  51. {
  52. public:
  53.         uint8_t                hour;
  54.         uint8_t                min;
  55.         uint8_t                sec;
  56.         uint8_t                date;
  57.         uint8_t                mon;
  58.         uint16_t        year;
  59.         uint8_t                dow;

  60.                 Time();
  61. };

  62. class DS1302_RAM
  63. {
  64. public:
  65.         byte        cell[31];

  66.                 DS1302_RAM();
  67. };

  68. class DS1302
  69. {
  70. public:
  71.                 DS1302(uint8_t ce_pin, uint8_t data_pin, uint8_t sclk_pin);
  72.         Time        getTime();
  73.         void        setTime(uint8_t hour, uint8_t min, uint8_t sec);
  74.         void        setDate(uint8_t date, uint8_t mon, uint16_t year);
  75.         void        setDOW(uint8_t dow);

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

  80.         void        halt(bool value);
  81.         void        writeProtect(bool enable);
  82.         void        setTCR(uint8_t value);

  83.         void                writeBuffer(DS1302_RAM r);
  84.         DS1302_RAM        readBuffer();
  85.         void                poke(uint8_t addr, uint8_t value);
  86.         uint8_t                peek(uint8_t addr);

  87. private:
  88.         uint8_t _ce_pin;
  89.         uint8_t _data_pin;
  90.         uint8_t _sclk_pin;
  91.         uint8_t _burstArray[8];

  92.         uint8_t        _readByte();
  93.         void        _writeByte(uint8_t value);
  94.         uint8_t        _readRegister(uint8_t reg);
  95.         void         _writeRegister(uint8_t reg, uint8_t value);
  96.         void        _burstRead();
  97.         uint8_t        _decode(uint8_t value);
  98.         uint8_t        _decodeH(uint8_t value);
  99.         uint8_t        _decodeY(uint8_t value);
  100.         uint8_t        _encode(uint8_t vaule);
  101. };
  102. #endif
复制代码
回复

使用道具 举报

发表于 2014-12-14 04:54:43 | 显示全部楼层
本帖最后由 timer 于 2014-12-14 05:16 编辑

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

使用道具 举报

发表于 2014-12-14 07:44:49 | 显示全部楼层
没遇到过 会不会是 rtc.writeProtect(); 导致的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-14 08:33:28 | 显示全部楼层
连线是没有问题的,我怀疑是我的1302坏了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 00:40 , Processed in 0.041309 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表