These are the methods to interface the TMP006 sensor with the mbed controller.

Dependents:   mbed_blinky_Tmp006_No2 mbed_blinky_Tmp006_3rd completesensor TMP006IR_HelloWorld

Files at this revision

API Documentation at this revision

Comitter:
sammacjunkie
Date:
Wed Oct 16 19:59:52 2013 +0000
Commit message:
comments added;

Changed in this revision

TMP006.cpp Show annotated file Show diff for this revision Revisions of this file
TMP006.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 48c0564d877a TMP006.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP006.cpp	Wed Oct 16 19:59:52 2013 +0000
@@ -0,0 +1,123 @@
+#include "TMP006.h"
+ 
+#define TEMP_REG_ADDR 0x00
+ 
+TMP006::TMP006(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
+}
+ 
+TMP006::~TMP006() { 
+}
+
+
+/** TMP006 Config method.
+ *  Used for configuring the sensor with desired samples.
+ */
+void TMP006::config(uint8_t addr, uint16_t samples)
+{
+    char    data[2];
+
+    data[0]   = 0x02;
+    data[1]   = samples | TMP006_CFG_MODEON | TMP006_CFG_DRDYEN;
+
+    m_i2c.write( addr, data, 1 ); // writes the config (0x02) address with the sample rate
+  
+}
+
+/** TMP006 Read Raw Temperature method.
+ *  Used for getting the raw data off the chip.
+ */
+int16_t TMP006::readRawDieTemperature(uint8_t addr)
+{
+
+  char reg[1];
+  char data[2];
+  
+  reg[0] = TMP006_TAMB;
+  
+  m_i2c.write(addr, reg, 1);
+  m_i2c.read(addr, data, 2);
+   
+  int16_t raw = (data[0] << 8) | data[1] ;
+
+  raw >>= 2;
+  return raw;
+}
+
+/** TMP006 Read Raw Voltage method.
+ *  Used for reading the raw voltage from the thermopile.
+ */
+int16_t TMP006::readRawVoltage(uint8_t addr) 
+{  
+  char reg[1];
+  char data[2];
+  
+  reg[0] = TMP006_VOBJ;
+  
+  m_i2c.write(addr, reg, 1);
+  m_i2c.read(addr, data, 2);
+   
+  int16_t raw = (data[0] << 8) | data[1] ;
+  
+  return raw;
+}
+
+/** TMP006 Read Object Temperature(C) method.
+ *  Used for calculating the object temperature in celcius.
+ */
+double TMP006::readObjTempC(uint8_t addr) 
+{
+  double Tdie = readRawDieTemperature(addr);
+  double Vobj = readRawVoltage(addr);
+  Vobj *= 156.25;  // 156.25 nV per LSB
+  Vobj /= 1000000000; // nV -> V
+  Tdie *= 0.03125; // convert to celsius
+  Tdie += 273.15; // convert to kelvin
+
+  // Equations for calculating temperature found in section 5.1 in the user guide
+  double tdie_tref = Tdie - TMP006_TREF;
+  double S = (1 + TMP006_A1*tdie_tref + 
+      TMP006_A2*tdie_tref*tdie_tref);
+  S *= TMP006_S0;
+  S /= 10000000;
+  S /= 10000000;
+
+  double Vos = TMP006_B0 + TMP006_B1*tdie_tref + 
+    TMP006_B2*tdie_tref*tdie_tref;
+
+  double fVobj = (Vobj - Vos) + TMP006_C2*(Vobj-Vos)*(Vobj-Vos);
+
+  double Tobj = sqrt(sqrt(Tdie * Tdie * Tdie * Tdie + fVobj/S));
+
+  Tobj -= 273.15; // Kelvin -> *C
+  return Tobj;
+}
+
+/** TMP006 Read Object Temperature(F) method.
+ *  Used for calculating the object temperature in fahrenheit.
+ */
+double TMP006::readObjTempF(uint8_t addr) 
+{
+  double Tobj = readObjTempC(addr);
+  Tobj = Tobj * 9.0/5.0 + 32.0; // convert to fahrenheit
+  return Tobj;
+}
+
+/** TMP006 Read Die Temperature(C) method.
+ *  Used for calculating the die temperature in celcius.
+ */
+double TMP006::readDieTempC(uint8_t addr) 
+{
+  double Tdie = readRawDieTemperature(addr);
+  Tdie *= 0.03125; // convert to celsius
+  return Tdie;
+}
+
+/** TMP006 Read Die Temperature(F) method.
+ *  Used for calculating the die temperature in fahrenheit.
+ */
+double TMP006::readDieTempF(uint8_t addr) 
+{
+  double Tdie = readDieTempC(addr);
+  Tdie = Tdie * 9.0/5.0 + 32.0; // convert to fahrenheit
+  return Tdie;
+}
diff -r 000000000000 -r 48c0564d877a TMP006.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP006.h	Wed Oct 16 19:59:52 2013 +0000
@@ -0,0 +1,96 @@
+/** TMP006 Temperature methods.
+ *  Used for interfacing the TMP006 with the mbed.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "TMP006.h"
+
+ * #define Address 0x80 
+ *
+ * TMP006 sensor(p9, p10, Address); 
+ *
+ * int main()
+ * {
+ * while(1) {
+ *   printf("TTemperature: %f F \r \n", sensor.readObjTempF(Address));
+ *   wait(0.5);
+ * }
+ * }
+ * @endcode
+ */
+
+
+#ifndef TMP006_H
+#define TMP006_H
+ 
+#include "mbed.h"
+
+// Constants for calculating object temperature
+#define TMP006_B0 -0.0000294
+#define TMP006_B1 -0.00000057
+#define TMP006_B2 0.00000000463
+#define TMP006_C2 13.4
+#define TMP006_TREF 298.15
+#define TMP006_A2 -0.00001678
+#define TMP006_A1 0.00175
+#define TMP006_S0 6.4  // * 10^-14
+
+// Configuration Settings
+#define TMP006_CFG_RESET    0x8000
+#define TMP006_CFG_MODEON   0x7000
+#define TMP006_CFG_1SAMPLE  0x0000
+#define TMP006_CFG_2SAMPLE  0x0200
+#define TMP006_CFG_4SAMPLE  0x0400
+#define TMP006_CFG_8SAMPLE  0x0600
+#define TMP006_CFG_16SAMPLE 0x0800
+#define TMP006_CFG_DRDYEN   0x0100
+#define TMP006_CFG_DRDY     0x0080
+
+// Registers to read thermopile voltage and sensor temperature
+#define TMP006_VOBJ  0x00
+#define TMP006_TAMB 0x01
+#define TMP006_CONFIG 0x02
+
+class TMP006
+{
+public:
+
+    // Constructor
+    TMP006(PinName sda, PinName scl, int addr);
+
+    /** Configures sensor, use before reading from it */
+    void config(uint8_t addr, uint16_t samples);
+    
+    /** Read raw sensor temperature */
+    int16_t readRawDieTemperature(uint8_t addr);
+    
+    /** Read raw thermopile voltage */
+    int16_t readRawVoltage(uint8_t addr);
+    
+    /** Calculate object temperature (C) based on raw sensor temp and thermopile voltage */
+    double readObjTempC(uint8_t addr);
+    
+    /** Calculate object temperature (F) based on raw sensor temp and thermopile voltage */
+    double readObjTempF(uint8_t addr);
+    
+    /** Caculate sensor temperature (C) based on raw reading */
+    double readDieTempC(uint8_t addr);  
+    
+    /** Caculate sensor temperature (F) based on raw reading */
+    double readDieTempF(uint8_t addr);  
+
+    /*!
+    Destroys instance.
+    */ 
+    ~TMP006();
+    
+  
+  
+private:
+    I2C m_i2c;
+    int m_addr;
+ 
+};
+ 
+#endif
\ No newline at end of file