Arduino实时时钟定时器设备

时间:2013-06-11 20:28:47

标签: time arduino clock

嘿,我有这个问题,我最近几天一直在处理这个问题,但我知道这是一个非常糟糕的编程方式,我真的希望有人能提出更好的方法要解决这个问题。

我想从RTC(真正的时钟计时器)获得时间我认为这是首字母缩略词,,, 它使用的是DS 1307 IC ,,,,,每次我在恒定功率下启动和运行程序时它都可以正常启动,但是当我取下USB电缆大约10秒然后重新连接它的时候会给我这些有趣的时间。 / p>

像2036年那样,46小时165分钟真的只是垃圾。 所以我读到某个地方,这些时间只是程序的方式,说没有连接到设备。我没有真正得到,因为它永久地插入,但嘿,这就是它想要的。

所以这是我从库附带的示例中获得的基本代码。 我想是因为没有连接只是做一个while循环,直到设备获得连接,这很有效,但有时需要10秒才能启动。

RTC连接了备用电池,线路SCL至A5和SDA A4

正如我说的那样有效,但需要很长时间才能启动并给我正确的时间。

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC;

void setup () {
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();
 Serial.println("RTC capturing time!");
 while (! RTC.isrunning()) 
  {
    Serial.println("RTC is NOT running!");
    Wire.begin();
    RTC.begin();
  }  
Serial.println("RTC IS running!");
// following line sets the RTC to the date & time this sketch was compiled
//  RTC.adjust(DateTime(__DATE__, __TIME__));
}


void loop () {

DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();

Serial.println();
delay(1000);
}

输出看起来像这样只有很多RTC没有运行!

RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC IS running!
2013/6/11 22:22:0

2013/6/11 22:22:2

2013/6/11 22:22:3

2013/6/11 22:22:4

2013/6/11 22:22:5

如果我不想包含我的while循环想法,我会像前面所说的那样搞乱时间和日期,直到它出于某种原因自行完成。

如果有人知道更好的方法来解决我的问题,请告诉我。我真的很困惑为什么会发生这种情况。

2 个答案:

答案 0 :(得分:1)

试试这段代码......

void setup () {
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();
  Serial.println("RTC capturing time!");

  while (!RTC.isrunning()) 
  {
    // do not really need this, remove after testing
    Serial.println("RTC is NOT running!");

    delay(10);

  }  
  Serial.println("RTC IS running!");
  // following line sets the RTC to the date & time this sketch was compiled
  //  RTC.adjust(DateTime(__DATE__, __TIME__));
}

答案 1 :(得分:0)

您应该明确USB断开连接时如何为RTC供电。首先,你应该检查电池是否真的好。然后你需要确保Arduino注意到RTC是由电池供电的。这是因为RTC将在电池供电时完全关闭I2C - &gt;当电源恢复时,必须重新初始化I2C。关键是你的DS1307库可能没有考虑到这一点。

有疑问,您需要分析库的源代码并阅读DS1307芯片的数据表。

另一件事是数据表说

  当VCC大于VBAT + 0.2V时,器件从电池切换到VCC   当VCC大于1.25 x VBAT

时识别输入

你有没有在启动时测量VBAT和VCC?