Skip to content

Commit

Permalink
Replace TMP006 library with Adafruit_TMP006 library
Browse files Browse the repository at this point in the history
  • Loading branch information
robertinant committed Nov 6, 2014
1 parent 0c3eec0 commit 9e4d133
Show file tree
Hide file tree
Showing 18 changed files with 1,065 additions and 1,352 deletions.
187 changes: 187 additions & 0 deletions hardware/cc3200/libraries/Adafruit_TMP006/Adafruit_TMP006.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/***************************************************
This is a library for the TMP006 Temp Sensor
Designed specifically to work with the Adafruit TMP006 Breakout
----> https://www.adafruit.com/products/1296
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/

#include "Adafruit_TMP006.h"

//#define TESTDIE 0x0C78
//#define TESTVOLT 0xFEED

Adafruit_TMP006::Adafruit_TMP006(uint8_t i2caddr) {
_addr = i2caddr;
}


boolean Adafruit_TMP006::begin(uint8_t samplerate) {
Wire.begin();

write16(TMP006_CONFIG, TMP006_CFG_MODEON | TMP006_CFG_DRDYEN | samplerate);

uint16_t mid, did;
mid = read16(TMP006_MANID);
did = read16(TMP006_DEVID);
#ifdef TMP006_DEBUG
Serial.print("mid = 0x"); Serial.println(mid, HEX);
Serial.print("did = 0x"); Serial.println(did, HEX);
#endif
if (mid != 0x5449) return false;
if (did != 0x67) return false;
return true;
}

void Adafruit_TMP006::sleep() {
// Read the control register and update it so bits 12-14 are zero to enter sleep mode.
uint16_t control = read16(TMP006_CONFIG);
control &= ~(TMP006_CFG_MODEON);
write16(TMP006_CONFIG, control);
}

void Adafruit_TMP006::wake() {
// Read the control register and update it so bits 12-14 are one to enter full operation.
uint16_t control = read16(TMP006_CONFIG);
control |= TMP006_CFG_MODEON;
write16(TMP006_CONFIG, control);
}

//////////////////////////////////////////////////////

double Adafruit_TMP006::readDieTempC(void) {
double Tdie = readRawDieTemperature();
Tdie *= 0.03125; // convert to celsius
#ifdef TMP006_DEBUG
Serial.print("Tdie = "); Serial.print(Tdie); Serial.println(" C");
#endif
return Tdie;
}

double Adafruit_TMP006::readObjTempC(void) {
double Tdie = readRawDieTemperature();
double Vobj = readRawVoltage();
Vobj *= 156.25; // 156.25 nV per LSB
Vobj /= 1000; // nV -> uV
Vobj /= 1000; // uV -> mV
Vobj /= 1000; // mV -> V
Tdie *= 0.03125; // convert to celsius
Tdie += 273.15; // convert to kelvin

#ifdef TMP006_DEBUG
Serial.print("Vobj = "); Serial.print(Vobj * 1000000); Serial.println("uV");
Serial.print("Tdie = "); Serial.print(Tdie); Serial.println(" C");
#endif

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;
}



int16_t Adafruit_TMP006::readRawDieTemperature(void) {
int16_t raw = read16(TMP006_TAMB);

#if TMP006_DEBUG == 1

#ifdef TESTDIE
raw = TESTDIE;
#endif

Serial.print("Raw Tambient: 0x"); Serial.print (raw, HEX);


float v = raw/4;
v *= 0.03125;
Serial.print(" ("); Serial.print(v); Serial.println(" *C)");
#endif
raw >>= 2;
return raw;
}

int16_t Adafruit_TMP006::readRawVoltage(void) {
int16_t raw;

raw = read16(TMP006_VOBJ);

#if TMP006_DEBUG == 1

#ifdef TESTVOLT
raw = TESTVOLT;
#endif

Serial.print("Raw voltage: 0x"); Serial.print (raw, HEX);
float v = raw;
v *= 156.25;
v /= 1000;
Serial.print(" ("); Serial.print(v); Serial.println(" uV)");
#endif
return raw;
}


/*********************************************************************/

uint16_t Adafruit_TMP006::read16(uint8_t a) {
uint16_t ret;

Wire.beginTransmission(_addr); // start transmission to device
#if (ARDUINO >= 100)
Wire.write(a); // sends register address to read from
#else
Wire.send(a); // sends register address to read from
#endif
Wire.endTransmission(); // end transmission

Wire.beginTransmission(_addr); // start transmission to device
Wire.requestFrom(_addr, (uint8_t)2);// send data n-bytes read
#if (ARDUINO >= 100)
ret = Wire.read(); // receive DATA
ret <<= 8;
ret |= Wire.read(); // receive DATA
#else
ret = Wire.receive(); // receive DATA
ret <<= 8;
ret |= Wire.receive(); // receive DATA
#endif
Wire.endTransmission(); // end transmission

return ret;
}

void Adafruit_TMP006::write16(uint8_t a, uint16_t d) {
Wire.beginTransmission(_addr); // start transmission to device
#if (ARDUINO >= 100)
Wire.write(a); // sends register address to read from
Wire.write(d>>8); // write data
Wire.write(d); // write data
#else
Wire.send(a); // sends register address to read from
Wire.send(d>>8); // write data
Wire.send(d); // write data
#endif
Wire.endTransmission(); // end transmission
}

80 changes: 80 additions & 0 deletions hardware/cc3200/libraries/Adafruit_TMP006/Adafruit_TMP006.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/***************************************************
This is a library for the TMP006 Temp Sensor
Designed specifically to work with the Adafruit TMP006 Breakout
----> https://www.adafruit.com/products/1296
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/

#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

#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
//#include <Adafruit_Sensor.h>

#ifdef __AVR_ATtiny85__
#include "TinyWireM.h"
#define Wire TinyWireM
#else
#include "Wire.h"
#endif

// uncomment for debugging!
//#define TMP006_DEBUG 1

#define TMP006_CONFIG 0x02

#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

#define TMP006_I2CADDR 0x40
#define TMP006_MANID 0xFE
#define TMP006_DEVID 0xFF

#define TMP006_VOBJ 0x0
#define TMP006_TAMB 0x01

class Adafruit_TMP006 {
public:
Adafruit_TMP006(uint8_t addr = TMP006_I2CADDR);
boolean begin(uint8_t samplerate = TMP006_CFG_16SAMPLE); // by default go highres

void sleep(); // Put chip into low power mode (disables temperature measurements).
void wake(); // Wake from low power mode.

int16_t readRawDieTemperature(void);
int16_t readRawVoltage(void);
double readObjTempC(void);
double readDieTempC(void);

private:
uint8_t _addr;
uint16_t read16(uint8_t addr);
void write16(uint8_t addr, uint16_t data);
};

22 changes: 22 additions & 0 deletions hardware/cc3200/libraries/Adafruit_TMP006/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
This is a library for the Adafruit TMP006 Infrared Thermopile Sensor

Designed specifically to work with the Adafruit TMP006 Breakout
----> https://www.adafruit.com/products/1296

These displays use I2C to communicate, 2 pins are required to interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Check out the links above for our tutorials and wiring diagrams

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution

To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_TMP006. Check that the Adafruit_TMP006 folder contains Adafruit_TMP006.cpp and Adafruit_TMP006.h

Place the Adafruit_TMP006 library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/***************************************************
This is an example for the TMP006 Barometric Pressure & Temp Sensor
Designed specifically to work with the Adafruit TMP006 Breakout
----> https://www.adafruit.com/products/1296
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/

#include <Wire.h>
#include "Adafruit_TMP006.h"

// Connect VCC to +3V (its a quieter supply than the 5V supply on an Arduino
// Gnd -> Gnd
// SCL connects to the I2C clock pin. On newer boards this is labeled with SCL
// otherwise, on the Uno, this is A5 on the Mega it is 21 and on the Leonardo/Micro digital 3
// SDA connects to the I2C data pin. On newer boards this is labeled with SDA
// otherwise, on the Uno, this is A4 on the Mega it is 20 and on the Leonardo/Micro digital 2

Adafruit_TMP006 tmp006;
//Adafruit_TMP006 tmp006(0x41); // start with a diferent i2c address!

void setup() {
Serial.begin(9600);
Serial.println("Adafruit TMP006 example");

// you can also use tmp006.begin(TMP006_CFG_1SAMPLE) or 2SAMPLE/4SAMPLE/8SAMPLE to have
// lower precision, higher rate sampling. default is TMP006_CFG_16SAMPLE which takes
// 4 seconds per reading (16 samples)
if (! tmp006.begin()) {
Serial.println("No sensor found");
while (1);
}

Serial.println("Send s to enter sleep mode, or w to wake up. Measurements are not updated while asleep!");
}

void loop() {
// Check for sleep/wake command.
while (Serial.available() > 0) {
char c = Serial.read();
if (c == 'w') {
Serial.println("Waking up!");
tmp006.wake();
}
else if (c == 's') {
Serial.println("Going to sleep!");
tmp006.sleep();
}
}

// Grab temperature measurements and print them.
float objt = tmp006.readObjTempC();
Serial.print("Object Temperature: "); Serial.print(objt); Serial.println("*C");
float diet = tmp006.readDieTempC();
Serial.print("Die Temperature: "); Serial.print(diet); Serial.println("*C");

delay(4000); // 4 seconds per reading for 16 samples per reading
}
Loading

0 comments on commit 9e4d133

Please sign in to comment.