Adafruit_TMP007.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /***************************************************
  2. This is a library for the TMP007 Temp Sensor
  3. Designed specifically to work with the Adafruit TMP007 Breakout
  4. ----> https://www.adafruit.com/products/2023
  5. These displays use I2C to communicate, 2 pins are required to
  6. interface
  7. Adafruit invests time and resources providing this open source code,
  8. please support Adafruit and open-source hardware by purchasing
  9. products from Adafruit!
  10. Written by Limor Fried/Ladyada for Adafruit Industries.
  11. BSD license, all text above must be included in any redistribution
  12. ****************************************************/
  13. #include "Adafruit_TMP007.h"
  14. // #define TESTDIE 0x0C78
  15. // #define TESTVOLT 0xFEED
  16. /**************************************************************************/
  17. /*!
  18. @brief Constructor for TMP007 sensor object
  19. @param i2caddr The I2C sensor address to use
  20. */
  21. /**************************************************************************/
  22. Adafruit_TMP007::Adafruit_TMP007(uint8_t i2caddr) {
  23. i2c_dev = new Adafruit_I2CDevice(i2caddr);
  24. }
  25. /**************************************************************************/
  26. /*!
  27. @brief Initialize I2C and connect to the TMP007 sensor
  28. @param samplerate The value written to TMP007_CONFIG
  29. @returns True if sensor found
  30. */
  31. /**************************************************************************/
  32. bool Adafruit_TMP007::begin(uint16_t samplerate) {
  33. if (!i2c_dev || !i2c_dev->begin()) {
  34. return false;
  35. }
  36. Adafruit_BusIO_Register config_reg =
  37. Adafruit_BusIO_Register(i2c_dev, TMP007_CONFIG, 2, MSBFIRST);
  38. config_reg.write(TMP007_CFG_RESET);
  39. delay(10);
  40. config_reg.write(TMP007_CFG_MODEON | TMP007_CFG_ALERTEN | TMP007_CFG_TRANSC |
  41. samplerate);
  42. Adafruit_BusIO_Register stat_reg =
  43. Adafruit_BusIO_Register(i2c_dev, TMP007_STATMASK, 2, MSBFIRST);
  44. stat_reg.write(TMP007_STAT_ALERTEN | TMP007_STAT_CRTEN);
  45. // enable conversion ready alert
  46. Adafruit_BusIO_Register did_reg =
  47. Adafruit_BusIO_Register(i2c_dev, TMP007_DEVID, 2, MSBFIRST);
  48. uint16_t did = did_reg.read();
  49. #ifdef TMP007_DEBUG
  50. Serial.print("did = 0x");
  51. Serial.println(did, HEX);
  52. #endif
  53. if (did != 0x78)
  54. return false;
  55. return true;
  56. }
  57. /**************************************************************************/
  58. /*!
  59. @brief Read the calibrated die temperature
  60. @returns double The calculated temperature of the sensor itself
  61. */
  62. /**************************************************************************/
  63. double Adafruit_TMP007::readDieTempC(void) {
  64. double Tdie = readRawDieTemperature();
  65. Tdie *= 0.03125; // convert to celsius
  66. #ifdef TMP007_DEBUG
  67. Serial.print("Tdie = ");
  68. Serial.print(Tdie);
  69. Serial.println(" C");
  70. #endif
  71. return Tdie;
  72. }
  73. /**************************************************************************/
  74. /*!
  75. @brief Read the calibrated object temperature
  76. @returns double The calculated temperature of the object in front of sensor
  77. */
  78. /**************************************************************************/
  79. double Adafruit_TMP007::readObjTempC(void) {
  80. Adafruit_BusIO_Register tobj_reg =
  81. Adafruit_BusIO_Register(i2c_dev, TMP007_TOBJ, 2, MSBFIRST);
  82. int16_t raw = tobj_reg.read();
  83. // invalid
  84. if (raw & 0x1)
  85. return NAN;
  86. raw >>= 2;
  87. double Tobj = raw;
  88. Tobj *= 0.03125; // convert to celsius
  89. #ifdef TMP007_DEBUG
  90. Serial.print("Tobj = ");
  91. Serial.print(Tobj);
  92. Serial.println(" C");
  93. #endif
  94. return Tobj;
  95. }
  96. /**************************************************************************/
  97. /*!
  98. @brief Read the raw, uncalibrated die temperature
  99. @returns int16_t The raw data read from the TMP007_TDIE register
  100. */
  101. /**************************************************************************/
  102. int16_t Adafruit_TMP007::readRawDieTemperature(void) {
  103. Adafruit_BusIO_Register tdie_reg =
  104. Adafruit_BusIO_Register(i2c_dev, TMP007_TDIE, 2, MSBFIRST);
  105. int16_t raw = tdie_reg.read();
  106. #if TMP007_DEBUG == 1
  107. #ifdef TESTDIE
  108. raw = TESTDIE;
  109. #endif
  110. Serial.print("Raw Tambient: 0x");
  111. Serial.print(raw, HEX);
  112. float v = raw / 4;
  113. v *= 0.03125;
  114. Serial.print(" (");
  115. Serial.print(v);
  116. Serial.println(" *C)");
  117. #endif
  118. raw >>= 2;
  119. return raw;
  120. }
  121. /**************************************************************************/
  122. /*!
  123. @brief Read the raw, uncalibrated thermopile voltage
  124. @returns int16_t The raw data read from the TMP007_VOBJ register
  125. */
  126. /**************************************************************************/
  127. int16_t Adafruit_TMP007::readRawVoltage(void) {
  128. int16_t raw;
  129. Adafruit_BusIO_Register vobj_reg =
  130. Adafruit_BusIO_Register(i2c_dev, TMP007_VOBJ, 2, MSBFIRST);
  131. raw = vobj_reg.read();
  132. #if TMP007_DEBUG == 1
  133. #ifdef TESTVOLT
  134. raw = TESTVOLT;
  135. #endif
  136. Serial.print("Raw voltage: 0x");
  137. Serial.print(raw, HEX);
  138. float v = raw;
  139. v *= 156.25;
  140. v /= 1000;
  141. Serial.print(" (");
  142. Serial.print(v);
  143. Serial.println(" uV)");
  144. #endif
  145. return raw;
  146. }