2015-10-31 78 views
1

我有一个DS3231 RTC模块,我试图通过I2C使用我的Arduino UNO读取它的时间。我正在使用随库提供的示例代码,但它似乎不起作用。我无法获得DS3231 RTC的工作

我走出串行监控的唯一的事情是这样的:

20165-85-165 25:165:165 Temperature=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);} 
+0

SDA和SCL这不是C!不要为Arduino代码添加C标签。 – Olaf

回答

0

对于任何有此问题的人,只要尝试更换电池即可。

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

我改变了电池,一切都在工作。

+0

看起来像我的问题。几个月后我将无法尝试,但会试着回来。 –

-1

使用:Serial.begin(9600); /另一个波特率代替Serial.begin(115200);

+0

这会有帮助吗? –

0

你把它连接到什么引脚?在将RTC的SDA和SCL分别连接到Arduino的SDA和SCL之前,我有同样的问题。根据型号的不同,这些是Mega2560上的引脚20和21,19和18上的Micro ...

0

这里有同样的问题,事实证明DS3231通信可能会因为不同事件。似乎隐藏的输出来自于此,至少在这里使用连接到ESP8266 Arduino的DS3231进行调试。

datasheet规格:

I2C接口进行访问,只要VCC或VBAT处于 有效电平。如果连接到DS3231的微控制器由于VCC或其他事件的丢失而复位为 ,则可能 微控制器和DS3231 I2C通信可能变为 未同步,例如,微控制器在从DS3231读取数据 时复位。当微控制器复位时,可通过切换SCL将接口置于已知状态,直到SDA 被观察到处于高电平。此时,当SCL为高电平时,微控制器 应拉低SDA,产生START条件。

并激发他们的官方application note 3506

使用ESP8266和使用他们的I2C implementation。您可以通过宏功能了解如何按位访问SDA和SCL引脚。这里是基于official example for the 8051的一个实现的复位功能。

#define SDA_LOW() (GPES = (1 << SDA)) 
#define SDA_HIGH() (GPEC = (1 << SDA)) 
#define SCL_LOW() (GPES = (1 << SCL)) 
#define SCL_HIGH() (GPEC = (1 << SCL)) 
#define SDA_READ() ((GPI & (1 << SDA)) != 0) 

void resetRTC() { 

    pinMode(SDA, INPUT_PULLUP); 
    pinMode(SCL, INPUT_PULLUP); 
    do { 
    SDA_HIGH(); 
    SCL_HIGH(); 
    if (SDA_READ()) { 
     SDA_LOW(); 
     SDA_HIGH(); 
    } 
    SCL_LOW(); 
    } while (SDA_READ() == 0); 

} 

这是工作的罚款,似乎解决问题

一个更简单的解决方案将被调用Wire.status(),这也似乎工作。

Wire.status(); 

我不确定是否所有情况下。这种方法正在检查状态,在一种情况下,它调用twi_write_start(),它与上面的函数resetRTC()有一些相似之处。

在您希望实现为ATMEL Arduino的类似功能的情况下,你需要寻找到按位执行操作上Arduino I2C core.