DS3231 External RTC I2C

Dependents:   HumidityController

Revision:
1:1607610a4ee9
Parent:
0:e4fbbd93299b
--- a/DS3231.cpp	Tue Aug 05 16:43:01 2014 +0000
+++ b/DS3231.cpp	Wed Aug 06 03:03:29 2014 +0000
@@ -27,6 +27,7 @@
 #include "DS3231.h"
 #define CLOCK_ADDRESS  0x68
 #define ADDR  char(CLOCK_ADDRESS<<1)
+#define SINCE 1900
 
 // Constructor
 DS3231::DS3231(PinName sda_pin, PinName scl_pin) : _i2c(sda_pin, scl_pin) {
@@ -41,18 +42,15 @@
     bool PM;
     bool h12;
 
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    //I2C.write(uint8_t(0x00));
-    //I2C.endTransmission();
-    _i2c.start();
-    _i2c.write(uint8_t(CLOCK_ADDRESS));
-    _i2c.write(uint8_t(0x00));
-    _i2c.stop();
+    char data[7];
+    char cmd;
+    cmd = 0x00;
+    _i2c.write(ADDR, &cmd, 1 );
+    _i2c.read( ADDR, data, 7 );
     
-    //I2C.requestFrom(CLOCK_ADDRESS, 7);
-    second     = bcdToDec(_i2c.read(uint8_t(CLOCK_ADDRESS)));
-    minute     = bcdToDec(_i2c.read(uint8_t(CLOCK_ADDRESS)));
-    tempBuffer = bcdToDec(_i2c.read(uint8_t(CLOCK_ADDRESS)));
+    second     = bcdToDec(data[0]);
+    minute     = bcdToDec(data[1]);
+    tempBuffer = bcdToDec(data[2]);
 
     h12        = tempBuffer & 0x40;
     if (h12) {
@@ -61,10 +59,10 @@
     } else {
         hour = bcdToDec(tempBuffer & 0x3F);
     }
-    DoW   = bcdToDec(_i2c.read(uint8_t(CLOCK_ADDRESS)));
-    date  = bcdToDec(_i2c.read(uint8_t(CLOCK_ADDRESS)));
-    month = bcdToDec(_i2c.read(uint8_t(CLOCK_ADDRESS)) & 0x7F);
-    year  = bcdToDec(_i2c.read(uint8_t(CLOCK_ADDRESS)));
+    DoW   = bcdToDec(data[3]);
+    date  = bcdToDec(data[4]);
+    month = bcdToDec(data[5] & 0x7F);
+    year  = bcdToDec(data[6]);
 }
 
 int8_t DS3231::getSecond(void) {
@@ -243,9 +241,6 @@
     cmd = 0x02;
 
     // Start by reading byte 0x02.
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    //I2C.write(uint8_t(0x02));
-    //I2C.endTransmission();
     _i2c.write(ADDR, &cmd, 1 );
     _i2c.read( ADDR, &temp_buffer, 1 );
 
@@ -264,39 +259,33 @@
 float DS3231::getTemperature(void) {
     // Checks the internal thermometer on the DS3231 and returns the 
     // temperature as a floating-point value.
-    char tempMSB;
-    char tempLSB;
+    char temp[2];
     char cmd;
     cmd = 0x11;
-    _i2c.write(ADDR, &cmd, 1 );
-    _i2c.read( ADDR, &tempMSB, 1);    // Here's the MSB
-    _i2c.read( ADDR, &tempLSB, 1);    // Here's the LSB
-    return float(tempMSB) + 0.25*(tempLSB>>6);
+    _i2c.write(ADDR, &cmd, 1);
+    _i2c.read( ADDR, temp, 2);
+    return float(temp[0]) + 0.25*(temp[1]>>6);
 }
 
 void DS3231::getA1Time(uint8_t& A1Day, uint8_t& A1Hour, uint8_t& A1Minute, uint8_t& A1Second, uint8_t& AlarmBits, bool& A1Dy, bool& A1h12, bool& A1PM) {
     uint8_t temp_buffer;
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    //I2C.write(uint8_t(0x07));
-    //I2C.endTransmission();
-    _i2c.start();
-    _i2c.write(uint8_t(CLOCK_ADDRESS));
-    _i2c.write(uint8_t(0x07));
-    _i2c.stop();
+    char data[4];
+    char cmd;
+    cmd = 0x07;
+    _i2c.write(ADDR, &cmd, 1);
+    _i2c.read( ADDR, data, 4);
 
-    //I2C.requestFrom(CLOCK_ADDRESS, 4);
-
-    temp_buffer = _i2c.read(uint8_t(CLOCK_ADDRESS));  // Get A1M1 and A1 Seconds
+    temp_buffer = data[0];  // Get A1M1 and A1 Seconds
     A1Second    = bcdToDec(temp_buffer & 0x7F);
     // put A1M1 bit in position 0 of DS3231_AlarmBits.
     AlarmBits   = AlarmBits | (temp_buffer & 0x80)>>7;
 
-    temp_buffer = _i2c.read(uint8_t(CLOCK_ADDRESS));  // Get A1M2 and A1 minutes
+    temp_buffer = data[1];  // Get A1M2 and A1 minutes
     A1Minute    = bcdToDec(temp_buffer & 0x7F);
     // put A1M2 bit in position 1 of DS3231_AlarmBits.
     AlarmBits   = AlarmBits | (temp_buffer & 0x80)>>6;
 
-    temp_buffer = _i2c.read(uint8_t(CLOCK_ADDRESS));  // Get A1M3 and A1 Hour
+    temp_buffer = data[2];  // Get A1M3 and A1 Hour
     // put A1M3 bit in position 2 of DS3231_AlarmBits.
     AlarmBits   = AlarmBits | (temp_buffer & 0x80)>>5;
     // determine A1 12/24 mode
@@ -308,7 +297,7 @@
         A1Hour  = bcdToDec(temp_buffer & 0x3F);   // 24-hour
     }
 
-    temp_buffer = _i2c.read(uint8_t(CLOCK_ADDRESS));  // Get A1M4 and A1 Day/Date
+    temp_buffer = data[3];  // Get A1M4 and A1 Day/Date
     // put A1M3 bit in position 3 of DS3231_AlarmBits.
     AlarmBits   = AlarmBits | (temp_buffer & 0x80)>>4;
     // determine A1 day or date flag
@@ -324,21 +313,19 @@
 
 void DS3231::getA2Time(uint8_t& A2Day, uint8_t& A2Hour, uint8_t& A2Minute, uint8_t& AlarmBits, bool& A2Dy, bool& A2h12, bool& A2PM) {
     uint8_t temp_buffer;
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    //I2C.write(uint8_t(0x0b));
-    //I2C.endTransmission();
-    _i2c.start();
-    _i2c.write(uint8_t(CLOCK_ADDRESS));
-    _i2c.write(uint8_t(0x0b));
-    _i2c.stop();
+    char data[3];
+    char cmd;
+    cmd = 0x0B;
+    _i2c.write(ADDR, &cmd, 1);
+    _i2c.read( ADDR, data, 4);
 
     //I2C.requestFrom(CLOCK_ADDRESS, 3); 
-    temp_buffer = _i2c.read(uint8_t(CLOCK_ADDRESS));  // Get A2M2 and A2 Minutes
+    temp_buffer = data[0];  // Get A2M2 and A2 Minutes
     A2Minute    = bcdToDec(temp_buffer & 0x7F);
     // put A2M2 bit in position 4 of DS3231_AlarmBits.
     AlarmBits   = AlarmBits | (temp_buffer & 0x80)>>3;
 
-    temp_buffer = _i2c.read(uint8_t(CLOCK_ADDRESS));  // Get A2M3 and A2 Hour
+    temp_buffer = data[1];  // Get A2M3 and A2 Hour
     // put A2M3 bit in position 5 of DS3231_AlarmBits.
     AlarmBits   = AlarmBits | (temp_buffer & 0x80)>>2;
     // determine A2 12/24 mode
@@ -350,7 +337,7 @@
         A2Hour  = bcdToDec(temp_buffer & 0x3F);   // 24-hour
     }
 
-    temp_buffer = _i2c.read(uint8_t(CLOCK_ADDRESS));  // Get A2M4 and A1 Day/Date
+    temp_buffer = data[2];  // Get A2M4 and A1 Day/Date
     // put A2M4 bit in position 6 of DS3231_AlarmBits.
     AlarmBits   = AlarmBits | (temp_buffer & 0x80)>>1;
     // determine A2 day or date flag
@@ -367,20 +354,12 @@
 void DS3231::setA1Time(uint8_t A1Day, uint8_t A1Hour, uint8_t A1Minute, uint8_t A1Second, uint8_t AlarmBits, bool A1Dy, bool A1h12, bool A1PM) {
     //  Sets the alarm-1 date and time on the DS3231, using A1* information
     uint8_t temp_buffer;
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    //I2C.write(uint8_t(0x07));  // A1 starts at 07h
-    _i2c.start();
-    _i2c.write(uint8_t(CLOCK_ADDRESS));
-    _i2c.write(uint8_t(0x07));
     
-    // Send A1 second and A1M1
-    //I2C.write(decToBcd(A1Second) | ((AlarmBits & 0x01) << 7));
-    _i2c.write(decToBcd(A1Second) | ((AlarmBits & 0x01) << 7));
-    
-    // Send A1 Minute and A1M2
-    //I2C.write(decToBcd(A1Minute) | ((AlarmBits & 0x02) << 6));
-    _i2c.write(decToBcd(A1Minute) | ((AlarmBits & 0x02) << 6));
-    
+    char data[4];
+    data[0] = 0x07; // A1 starts at 07h
+    data[1] = decToBcd(A1Second) | ((AlarmBits & 0x01) << 7);  // Send A1 second and A1M1
+    data[2] = decToBcd(A1Minute) | ((AlarmBits & 0x02) << 6);  // Send A1 Minute and A1M2
+     
     // Figure out A1 hour 
     if (A1h12) {
         // Start by converting existing time to h12 if it was given in 24h.
@@ -413,26 +392,18 @@
         // Set A1 Day/Date flag (Otherwise it's zero)
         temp_buffer = temp_buffer | 0x40;
     }
-    //I2C.write(temp_buffer);
-    _i2c.write(temp_buffer);
-    
-    // All done!
-    //I2C.endTransmission();
-    _i2c.stop();
+    data[3] = temp_buffer;
+    _i2c.write(ADDR, data, 4);    
+
 }
 
 void DS3231::setA2Time(uint8_t A2Day, uint8_t A2Hour, uint8_t A2Minute, uint8_t AlarmBits, bool A2Dy, bool A2h12, bool A2PM) {
     //  Sets the alarm-2 date and time on the DS3231, using A2* information
     uint8_t temp_buffer;
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    //I2C.write(uint8_t(0x0b));  // A1 starts at 0bh
-    _i2c.start();
-    _i2c.write(uint8_t(CLOCK_ADDRESS));
-    _i2c.write(uint8_t(0x0b));
     
-    // Send A2 Minute and A2M2
-    //I2C.write(decToBcd(A2Minute) | ((AlarmBits & 0x10) << 3));
-    _i2c.write(decToBcd(A2Minute) | ((AlarmBits & 0x10) << 3));
+    char data[4];
+    data[0] = 0x0B; // A2 starts at 0Bh
+    data[1] = decToBcd(A2Minute) | ((AlarmBits & 0x10) << 3);  // Send A2 Minute and A2M2
     
     // Figure out A2 hour 
     if (A2h12) {
@@ -458,8 +429,7 @@
     // add in A2M3 bit
     temp_buffer = temp_buffer | ((AlarmBits & 0x20)<<2);
     // A2 hour is figured out, send it
-    //I2C.write(temp_buffer); 
-    _i2c.write(temp_buffer);
+    data[2] = temp_buffer;
     
     // Figure out A2 day/date and A2M4
     temp_buffer = ((AlarmBits & 0x40)<<1) | decToBcd(A2Day);
@@ -467,12 +437,9 @@
         // Set A2 Day/Date flag (Otherwise it's zero)
         temp_buffer = temp_buffer | 0x40;
     }
-    //I2C.write(temp_buffer);
-    _i2c.write(temp_buffer);
-    
-    // All done!
-    //I2C.endTransmission();
-    _i2c.stop();
+    data[3] = temp_buffer;
+    _i2c.write(ADDR, data, 4);   
+
 }
 
 void DS3231::turnOnAlarm(uint8_t Alarm) {
@@ -607,47 +574,37 @@
 uint8_t DS3231::readControlByte(bool which) {
     // Read selected control byte
     // first byte (0) is 0x0e, second (1) is 0x0f
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    _i2c.start();
-    _i2c.write(uint8_t(CLOCK_ADDRESS));
-    
+    char data;
+    char cmd[2];
+    cmd[0] = 0x0F;
+    cmd[1] = 0x0E;
+        
     if (which) {
         // second control byte
-        //I2C.write(uint8_t(0x0f));
-        _i2c.write(uint8_t(0x0f));
-        
+        _i2c.write(ADDR, &cmd[0], 1 );        
     } else {
         // first control byte
-        //I2C.write(uint8_t(0x0e));
-        _i2c.write(uint8_t(0x0e));
-        
+        _i2c.write(ADDR, &cmd[1], 1 );
     }
-    //I2C.endTransmission();
-    _i2c.stop();
-    
-    //I2C.requestFrom(CLOCK_ADDRESS, 1);
-    return _i2c.read(uint8_t(CLOCK_ADDRESS)); 
+    _i2c.read( ADDR, &data, 1 );
+    return data; 
 }
 
 void DS3231::writeControlByte(uint8_t control, bool which) {
     // Write the selected control byte.
     // which=false -> 0x0e, true->0x0f.
-    //I2C.beginTransmission(CLOCK_ADDRESS);
-    _i2c.start();
-    _i2c.write(uint8_t(CLOCK_ADDRESS));
+    char data1[2];
+    char data2[2];
+    data1[0] = 0x0F;
+    data1[1] = control;
+    data2[0] = 0x0E;
+    data2[1] = control;
     
     if (which) {
-        //I2C.write(uint8_t(0x0f));
-        _i2c.write(uint8_t(0x0f));
-        
+        _i2c.read( ADDR, data1, 2);
     } else {
-        //I2C.write(uint8_t(0x0e));
-        _i2c.write(uint8_t(0x0e));
-        
+        _i2c.read( ADDR, data2, 2);
     }
-    //I2C.write(control);
-    //I2C.endTransmission();
-    _i2c.write(control);
-    _i2c.stop();
+
 }