Simple DS1621 temperature acquisition.

Dependents:   S3_DS1621

Revision:
1:bef707045d41
Parent:
0:d1667b4536dd
Child:
2:3160631567f2
--- a/ds1621.cpp	Sat Mar 05 20:35:01 2011 +0000
+++ b/ds1621.cpp	Mon Mar 07 08:11:25 2011 +0000
@@ -6,34 +6,38 @@
 #include "ds1621.h"
 
 DS1621::DS1621(I2C* interface, uint8_t address, Serial* pc) {
-    if (dbx) dbx->printf("Constructor DS1621\n\r");
     i2c = interface;
     dbx = pc;
+    if (dbx) dbx->printf("Constructor DS1621\n\r");
 }
 
 float DS1621::getTemp(uint8_t address) { // Return degrees C (-55 to +125)
     if (dbx) dbx->printf("DS1621::getTemp DS1621 address=0x%X\n\r",address);
-    float temperature;
+    float temperature = 0.0;
     uint16_t temp16;
     char temp8[2];
     int8_t data;
-    address &= 0x0E;
-    data = 0xEE;
-    i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
-    data = 0xAA;
-    i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
-    i2c->read(DS1621_Read|address, temp8 ,sizeof(temp8), false);
-    // Format temperature
-    temp16 = temp8[0];
-    temp16 = temp16 << 1;
-    temp16 = temp16 + (temp8[1] >> 7);
-    if ((temp16 & 0x100) == 0) { // +
-        temperature = ((float) temp16 / 2);
-    } else {  // -
-        temp16 = ~temp16 & 0xFF;
-        temperature = ((float) temp16 / 2);
-        temperature = -1.0 * temperature;
+    if (i2c) {
+        address &= 0x0E;
+        data = 0xEE; // Start Convert T [EEh]
+        i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
+        data = 0xAA; // Read Temperature [AAh]
+        i2c->write(DS1621_Write|address,(char *)&data,sizeof(data));
+        i2c->read(DS1621_Read|address, temp8 ,sizeof(temp8), false);
+        // Format temperature
+        temp16 = temp8[0];
+        temp16 = temp16 << 1;
+        temp16 = temp16 + (temp8[1] >> 7);
+        if ((temp16 & 0x100) == 0) { // +
+            temperature = ((float) temp16 / 2);
+        } else {  // -
+            temp16 = ~temp16 & 0xFF;
+            temperature = ((float) temp16 / 2);
+            temperature = -1.0 * temperature;
+        }
+        if (dbx)  dbx->printf("DS1621::getTemp temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16);
+    } else {
+       if (dbx)  dbx->printf("DS1621::getTemp interfave I2C is not initialized\n\r");
     }
-    if (dbx)  dbx->printf("temp8[0]=0x%X, temp8[1]=0x%X, temp16=0x%X\n\r",temp8[0],temp8[1],temp16);
     return temperature;
 }
\ No newline at end of file