我有一个DS3231 RTC模块,我正在尝试通过I2C使用我的Arduino UNO读取其时间。我正在使用库提供的示例代码,但似乎无法正常工作。

我从串行监视器中获得的唯一信息是:

20165-85-165 25:165:165Temperature=254

我在另一个RTC模块上也遇到了同样的问题,我的猜测(可能不正确)是尽管似乎没有复位引脚,但它们可能已溢出。

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;

byte year, month, date, DoW, hour, minute, second;

void setup() {
    // Start the I2C interface
    Wire.begin();
  #define oneTime
  #ifdef oneTime
    Clock.setSecond(50);//Set the second
    Clock.setMinute(59);//Set the minute
    Clock.setHour(11);  //Set the hour
    Clock.setDoW(5);    //Set the day of the week
    Clock.setDate(31);  //Set the date of the month
    Clock.setMonth(5);  //Set the month of the year
    Clock.setYear(13);  //Set the year (Last two digits of the year)
  #endif
        // Start the serial interface
    Serial.begin(115200);

}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature;
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();

  temperature=Clock.getTemperature();

  Serial.print("20");
  Serial.print(year,DEC);
  Serial.print('-');
  Serial.print(month,DEC);
  Serial.print('-');
  Serial.print(date,DEC);
  Serial.print(' ');
  Serial.print(hour,DEC);
  Serial.print(':');
  Serial.print(minute,DEC);
  Serial.print(':');
  Serial.print(second,DEC);
  Serial.print('\n');
  Serial.print("Temperature=");
  Serial.print(temperature);
  Serial.print('\n');
}
void loop() {ReadDS3231();delay(1000);}

最佳答案

对于在那里遇到此问题的任何人,只需尝试更换电池即可。

我购买了一个新的DS3231模块,但从零开始就没有工作。我得到的数据很奇怪,温度却为零。当我设法正确读取日期时,它没有保存。我尝试了所有我找不到的图书馆。

我更换了电池,现在一切正常。

07-24 09:29