Library for the LM75A Temperature Sensor

Dependents:   LM75A_Test_Code MQTT_and_Temperature_Sensore_LM75 2021_Temp_y_Acelerom_V4enlace_serial

Files at this revision

API Documentation at this revision

Comitter:
edodm85
Date:
Wed Jun 27 21:20:27 2012 +0000
Child:
1:19dc98c810a5
Commit message:
Rev 3

Changed in this revision

LM75A.cpp Show annotated file Show diff for this revision Revisions of this file
LM75A.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75A.cpp	Wed Jun 27 21:20:27 2012 +0000
@@ -0,0 +1,81 @@
+/* Author: Edoardo De Marchi */
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+
+
+#include "LM75A.h"
+
+
+
+LM75A::LM75A(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr)
+{
+
+}
+
+
+LM75A::~LM75A()
+{
+
+}
+
+
+
+float LM75A::read_T()
+{
+
+  m_i2c.write(m_addr, TEMP_REG_ADDR, 1);                // Pointer to the temperature register
+  char cmd[2] = {0,0};
+  m_i2c.read(m_addr, cmd, 2);                           // read temperature register
+  
+  unsigned short val = ((cmd[0] << 8) + cmd[1]) >> 7;     //val = (cmd[ 1 ] << 1) | ( cmd[ 0 ] >> 7 ) ;
+   
+  float temp = (float) ((float)val * 0.5);  
+  
+  return temp;
+}
+
+
+
+char LM75A::read_reg(char addr)
+{
+
+    char data[1] = {0};
+    char tmp = addr;
+    m_i2c.write(m_addr, &tmp, 1);           
+    m_i2c.read(m_addr, data, 1);              // Read register content
+    
+    return data[0];
+
+}
+
+
+
+void LM75A::write_reg(char addr, char data)
+{
+
+    char data2[2] = {0, 0};
+    
+    data2[0] = addr;
+    data2[1] = data;
+    
+    m_i2c.write(m_addr, data2, 2);             
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75A.h	Wed Jun 27 21:20:27 2012 +0000
@@ -0,0 +1,78 @@
+/* Author: Edoardo De Marchi */
+/* Copyright (C) 2012 mbed.org, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+
+
+#pragma once 
+ 
+#include "mbed.h"
+ 
+ 
+#define TEMP_REG_ADDR 0x00          // Temperature address
+ 
+ 
+/* Library for the LM75A temperature sensor.
+The LM75A is an I2C digital temperature sensor in a small SOP-8 package, 
+with a 0.5C resolution and 2C accuracy
+*/
+ 
+class LM75A{        // Creates an instance of the class
+    public:
+        
+           /** Create an LM75A object connected to the specified I2C object and using the specified deviceAddress
+            *
+            * @param sda The I2C port data
+            * @param scl The I2C port clock
+            * @param addr The address of the MMA7660FC
+            */
+      LM75A(PinName sda, PinName scl, int addr);
+    
+    
+            /** Destroys an LM75A object
+            *
+            */
+      ~LM75A();
+    
+            /** Reads the current temperature
+            *
+            * @param returns Return the Temperature value 
+            */
+      float read_T();
+      
+            /** Reads from specified LM75A register
+            *
+            * @param addr Pointer register
+            * @param returns Return the data from the register 
+            */
+      char read_reg(char addr);
+      
+            /** Writes to specified LM75A register
+            *
+            * @param addr Pointer register
+            * @param data Data to write
+            */ 
+      void write_reg(char addr, char data);
+      
+    
+    private:
+      I2C m_i2c;
+      int m_addr;   
+};
+