DS1820-EM

Fork of DS1820 by HM Yoong

Files at this revision

API Documentation at this revision

Comitter:
liviur2
Date:
Tue May 27 17:03:27 2014 +0000
Parent:
1:78f2fa466c55
Commit message:
small changes

Changed in this revision

DS18B20.cpp Show annotated file Show diff for this revision Revisions of this file
DS18B20.h Show annotated file Show diff for this revision Revisions of this file
--- a/DS18B20.cpp	Mon Dec 03 22:58:27 2012 +0000
+++ b/DS18B20.cpp	Tue May 27 17:03:27 2014 +0000
@@ -22,72 +22,89 @@
 
 DigitalOut conversionInProgress(LED4);  // conversion in progress
 DigitalOut resetFailure(LED1);          // for error reporting
-extern DigitalInOut sensor;     // sensor pin
+//extern DigitalInOut sensor;     // sensor pin
 
-static void inError() {
+static void inError()
+{
     while (1) {
         resetFailure = !resetFailure;
         wait(0.2);
     }
 }
 
-void DoConversion() {
-    if (Reset(sensor) != 0) {
+void DoConversion(DigitalInOut& pin)
+{
+    if (Reset(pin) != 0) {
         inError();
     } else {
         conversionInProgress = 1;       // led on
-        WriteByte(sensor, SKIP_ROM);            // Skip ROM
-        WriteByte(sensor, CONVERT);             // Convert
-        while (ReadBit(sensor) == 0) {
+        WriteByte(pin, SKIP_ROM);            // Skip ROM
+        WriteByte(pin, CONVERT);             // Convert
+        while (ReadBit(pin) == 0) {
             // wait for conversion to complete
         }
         conversionInProgress = 0;       // led off
     }
 }
 
-uint32_t GetTemperature() {
+uint32_t GetTemperature(DigitalInOut &pin)
+{
     uint32_t result = 0;
-    if (Reset(sensor) != 0) {
+    if (Reset(pin) != 0) {
         inError();
     } else {
         ScratchPad_t scratchpad;
-        WriteByte(sensor, SKIP_ROM);    // Skip ROM
-        WriteByte(sensor, READ_SCRATCHPAD);    // Read Scrachpad
-        scratchpad.LSB = ReadByte(sensor);
-        scratchpad.MSB = ReadByte(sensor);
-        Reset(sensor);    // terminate read as we only want temperature
+        WriteByte(pin, SKIP_ROM);    // Skip ROM
+        WriteByte(pin, READ_SCRATCHPAD);    // Read Scrachpad
+        scratchpad.LSB = ReadByte(pin);
+        scratchpad.MSB = ReadByte(pin);
+        Reset(pin);    // terminate read as we only want temperature
+
         result = ((scratchpad.MSB << 8) | scratchpad.LSB);
+        
     }
     return result;
 }
 
-ROM_Code_t ReadROM() {
+ROM_Code_t ReadROM(DigitalInOut &pin)
+{
     ROM_Code_t ROM_Code;
-    if (Reset(sensor) != 0) {
+    if (Reset(pin) != 0) {
         inError();
     } else {
-        
-        WriteByte(sensor, READ_ROM);    // Read ROM
+
+        WriteByte(pin, READ_ROM);    // Read ROM
         for (uint32_t i = 0; i < 8; ++i) {
-            ROM_Code.rom[i] = ReadByte(sensor);
+            ROM_Code.rom[i] = ReadByte(pin);
         }
     }
     return ROM_Code;
 }
 
 // temperature is store as 7.4 fixed point format (assuming 12 bit conversion)
-void displayTemperature(Serial& s) {
-    DoConversion();
-    uint32_t temp = GetTemperature();
+void displayTemperature(Serial& s, DigitalInOut& pin)
+{
+    DoConversion(pin);
+    uint32_t temp = GetTemperature(pin);
     float f = (temp & 0x0F) * 0.0625;    // calculate .4 part
     f += (temp >> 4);    // add 7.0 part to it
-    s.printf("Temp is %2.1fC\n\r", f);    // display in 2.1 format
+    s.printf("Temp is %2.2fC\n\r", f);    // display in 2.1 format
 }
 
-float getTemp(){
-    DoConversion();
-    uint32_t temp = GetTemperature();
-    float f = (temp & 0x0F) * 0.0625;    // calculate .4 part
-    f += (temp >> 4);    // add 7.0 part to it
+float getTemp(DigitalInOut &pin)
+{
+    DoConversion(pin);
+    uint32_t temp = GetTemperature(pin);
+    float f;
+    if((temp & 0xff00)== 0xff00){
+       temp = ~temp + 1;
+       f = (temp & 0x000F) * 0.0625;    // calculate .4 part
+       f += ((temp >> 4)&0x000f);    // add 7.0 part to it
+       f = -f;
+    }else{
+       f = (temp & 0x000F) * 0.0625;    // calculate .4 part
+       f += (temp >> 4);    // add 7.0 part to it
+    }
+   // s.printf("T = %x | mask = %x | %2.2f\n", temp, (temp & 0xff00), f);
     return f;
 }
--- a/DS18B20.h	Mon Dec 03 22:58:27 2012 +0000
+++ b/DS18B20.h	Tue May 27 17:03:27 2014 +0000
@@ -14,10 +14,9 @@
     } BYTES;
 } ROM_Code_t;
 
-ROM_Code_t ReadROM() ;
+ROM_Code_t ReadROM(DigitalInOut &pin);
 
 // temperature is store as 7.4 fixed point format (assuming 12 bit conversion)
 void displayTemperature(Serial& s) ;
-
-float getTemp();
+float getTemp(DigitalInOut &pin);
 #endif
\ No newline at end of file