Driver to read and control a serial (i2c) temperature sensor, The Microchip MCP9808 is the digital sensor to control, can be read it, set its resolution, shutdown and also set alarms.

Dependents:   Hotboards_temp_alarms Hotboards_temp_fahrenheit Hotboards_temp_reading_temperature LCD_Temperatura

Revision:
1:f850ee1083ba
Parent:
0:83da47b7ed26
diff -r 83da47b7ed26 -r f850ee1083ba Hotboards_temp.cpp
--- a/Hotboards_temp.cpp	Thu Mar 17 04:57:00 2016 +0000
+++ b/Hotboards_temp.cpp	Tue Mar 22 20:39:49 2016 +0000
@@ -21,7 +21,7 @@
 Hotboards_temp::Hotboards_temp( I2C &i2c, uint8_t address, uint8_t resolution )
     : _i2c(i2c) 
 {
-    _address = address | 0x18;
+    _address = (address<<1)|0x30;
     _resolution = resolution;
 }
 
@@ -32,7 +32,7 @@
     if( val == 0x0054 )
     {
         // device is presence, default set resolution
-        writeReg( REG_RESOLUTION, _resolution );
+        writeReg( REG_RESOLUTION, _resolution);
         // clear configuration register (alarms included)
         writeReg( REG_CONFIG, 0x00 );
         flag = 1;
@@ -40,6 +40,7 @@
     return flag;
 }
 
+
 float Hotboards_temp::read( void )
 {
     uint16_t val;
@@ -51,8 +52,9 @@
     // small algorithm to calculate tmeperature in Celcius
     // borrowed from https://github.com/adafruit/Adafruit_MCP9808_Library/blob/master/Adafruit_MCP9808.cpp
     temp = val & 0x0FFF;
+    
     temp /=  (float)16.0;
-    // check if a negative temperature
+     //check if a negative temperature
     if( val & 0x1000 ) temp -= 256;
 
     return temp;
@@ -109,11 +111,13 @@
 
 uint16_t Hotboards_temp::readReg( uint8_t reg )
 {
-    int val;
-    char buffer[3];
-
+    uint16_t val;
+    char buffer[2];
+    
+    _address &= 0x3E;
     buffer[0] = reg;
     _i2c.write( _address, buffer, 1, true );
+    _address |= 0x01;
     _i2c.read( _address, buffer, 2, false );
     
     val = buffer[0] << 8;
@@ -125,7 +129,7 @@
 void Hotboards_temp::writeReg( uint8_t reg, uint16_t val )
 {
     char buffer[3] = { reg, val >> 8, val & 0x00FF };
-    
+    _address &= 0x3E;
     _i2c.write( _address, buffer, 3 );
 }