Realtime clock library for DS1307 and DS3231m

Dependents:   vfd_modular_clock_mbed

Files at this revision

API Documentation at this revision

Comitter:
Backstrom
Date:
Wed Feb 11 05:58:07 2015 +0000
Parent:
0:1602fdac44ec
Child:
2:6119507e6713
Commit message:
Add getTemp and forceTempConversion to ds3231m.;

Changed in this revision

ds3231m.cpp Show annotated file Show diff for this revision Revisions of this file
ds3231m.h Show annotated file Show diff for this revision Revisions of this file
--- a/ds3231m.cpp	Wed Feb 11 05:30:48 2015 +0000
+++ b/ds3231m.cpp	Wed Feb 11 05:58:07 2015 +0000
@@ -191,6 +191,46 @@
     write_byte(status, 0x0F);
 }
 
+void DS3231M::getTemp(int8_t* i, uint8_t* f)
+{
+    char data[2]; // msb, lsb
+    
+    *i = 0;
+    *f = 0;
+    
+    data[0] = 0x11;
+    int w = m_i2c.write(DS3231M_SLAVE_ADDR, data, 1);
+    int r = m_i2c.read(DS3231M_SLAVE_ADDR, data, 2);
+    
+    // integer part in entire byte
+    *i = (uint8_t)data[0];
+    // fractional part in top two bits (increments of 0.25)
+    *f = ((uint8_t)data[1] >> 6) * 25;
+    
+    // float value can be read like so:
+    // float temp = ((((short)data[0] << 8) | (short)data[1]) >> 6) / 4.0f;
+}
+
+void DS3231M::forceTempConversion(uint8_t block)
+{
+    // read control register (0x0E)
+    uint8_t control = read_byte(0x0E);  // read control register
+    control |= 0x20; // Set CONV bit
+
+    // write new control register value
+    write_byte(control, 0x0E);
+
+    if (!block) return;
+    
+    uint8_t data;
+    // Temp conversion is ready when control register becomes 0
+    do {
+        // Block until CONV is 0
+        data = read_byte(0x0E);
+    } while ((data & 0x20) != 0);
+}
+
+
 uint8_t DS3231M::read_byte(uint8_t offset)
 {
     char buf[1];
--- a/ds3231m.h	Wed Feb 11 05:30:48 2015 +0000
+++ b/ds3231m.h	Wed Feb 11 05:58:07 2015 +0000
@@ -52,6 +52,10 @@
     virtual void SQWEnable(bool enable);
     virtual void SQWSetFreq(enum RTC_SQW_FREQ freq);
     virtual void Osc32kHzEnable(bool enable);
+    
+    // Temperature
+    void getTemp(int8_t* i, uint8_t* f);
+    void forceTempConversion(uint8_t block);
 
 private:
     I2C& m_i2c;