Method of reading from DS1825 thermometer, including the OneWire method. Specific for the use of just 1 sensor.

Dependencies:   OneWire

Dependents:   Inductive_Sensor Inductive_Sensor_Jasper Inductive_Sensor_3

Revision:
0:abb33be87221
Child:
1:ef7e5efc8794
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS1825.cpp	Thu Mar 24 16:26:24 2016 +0000
@@ -0,0 +1,49 @@
+#include "DS1825.h"
+#include "OneWire.h"
+#include "mbed.h"
+
+
+//DS1825::DS1825( PinName pin ) : _onewire( pin )
+DS1825::DS1825( PinName pin )
+{   
+    _onewire = new OneWire( pin );
+}
+
+DS1825::~DS1825( )
+{   
+    _onewire->depower();            // Does not work perfectly: 1,1 V remains...
+    delete _onewire;
+    _onewire = NULL;
+}
+
+bool DS1825::validateTemperature( uint8_t d[9] )
+{
+    return (d[8] == _onewire->crc8(d, 8));     
+}
+
+float DS1825::getTemperature( void )
+{
+    _onewire->reset();
+    _onewire->skip();                // Skip ROM  --> because there is only one sensor, no need to search for the correct ID
+    _onewire->write( 0x44, 1 );      // Convert T --> initiate temperature conversion, data is stored on scratchpad, after writing put the wire high to power the DS1825
+    wait_ms( 750 );                 // with 12-bit resolution, t_conv = 750 ms
+    
+    _onewire->reset();
+    _onewire->skip();                // Skip ROM
+    _onewire->write( 0xBE );         // Command to read scratchpad
+    
+    uint8_t Tdata[9];
+    for (int i = 0; i < 9; i++) 
+        Tdata[i] = _onewire->read(); // read scratchpad    
+    
+    _onewire->depower();
+    
+    if( validateTemperature( Tdata ) )
+    {
+        int16_t T_bin;
+        T_bin = (Tdata[1] << 8) | Tdata[0];
+        T = T_bin / 16.0;
+        return T;
+    }
+    return 0;
+}
\ No newline at end of file