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
TMP006.cpp@0:48c0564d877a, 2013-10-16 (annotated)
- Committer:
- sammacjunkie
- Date:
- Wed Oct 16 19:59:52 2013 +0000
- Revision:
- 0:48c0564d877a
comments added;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sammacjunkie | 0:48c0564d877a | 1 | #include "TMP006.h" |
sammacjunkie | 0:48c0564d877a | 2 | |
sammacjunkie | 0:48c0564d877a | 3 | #define TEMP_REG_ADDR 0x00 |
sammacjunkie | 0:48c0564d877a | 4 | |
sammacjunkie | 0:48c0564d877a | 5 | TMP006::TMP006(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { |
sammacjunkie | 0:48c0564d877a | 6 | } |
sammacjunkie | 0:48c0564d877a | 7 | |
sammacjunkie | 0:48c0564d877a | 8 | TMP006::~TMP006() { |
sammacjunkie | 0:48c0564d877a | 9 | } |
sammacjunkie | 0:48c0564d877a | 10 | |
sammacjunkie | 0:48c0564d877a | 11 | |
sammacjunkie | 0:48c0564d877a | 12 | /** TMP006 Config method. |
sammacjunkie | 0:48c0564d877a | 13 | * Used for configuring the sensor with desired samples. |
sammacjunkie | 0:48c0564d877a | 14 | */ |
sammacjunkie | 0:48c0564d877a | 15 | void TMP006::config(uint8_t addr, uint16_t samples) |
sammacjunkie | 0:48c0564d877a | 16 | { |
sammacjunkie | 0:48c0564d877a | 17 | char data[2]; |
sammacjunkie | 0:48c0564d877a | 18 | |
sammacjunkie | 0:48c0564d877a | 19 | data[0] = 0x02; |
sammacjunkie | 0:48c0564d877a | 20 | data[1] = samples | TMP006_CFG_MODEON | TMP006_CFG_DRDYEN; |
sammacjunkie | 0:48c0564d877a | 21 | |
sammacjunkie | 0:48c0564d877a | 22 | m_i2c.write( addr, data, 1 ); // writes the config (0x02) address with the sample rate |
sammacjunkie | 0:48c0564d877a | 23 | |
sammacjunkie | 0:48c0564d877a | 24 | } |
sammacjunkie | 0:48c0564d877a | 25 | |
sammacjunkie | 0:48c0564d877a | 26 | /** TMP006 Read Raw Temperature method. |
sammacjunkie | 0:48c0564d877a | 27 | * Used for getting the raw data off the chip. |
sammacjunkie | 0:48c0564d877a | 28 | */ |
sammacjunkie | 0:48c0564d877a | 29 | int16_t TMP006::readRawDieTemperature(uint8_t addr) |
sammacjunkie | 0:48c0564d877a | 30 | { |
sammacjunkie | 0:48c0564d877a | 31 | |
sammacjunkie | 0:48c0564d877a | 32 | char reg[1]; |
sammacjunkie | 0:48c0564d877a | 33 | char data[2]; |
sammacjunkie | 0:48c0564d877a | 34 | |
sammacjunkie | 0:48c0564d877a | 35 | reg[0] = TMP006_TAMB; |
sammacjunkie | 0:48c0564d877a | 36 | |
sammacjunkie | 0:48c0564d877a | 37 | m_i2c.write(addr, reg, 1); |
sammacjunkie | 0:48c0564d877a | 38 | m_i2c.read(addr, data, 2); |
sammacjunkie | 0:48c0564d877a | 39 | |
sammacjunkie | 0:48c0564d877a | 40 | int16_t raw = (data[0] << 8) | data[1] ; |
sammacjunkie | 0:48c0564d877a | 41 | |
sammacjunkie | 0:48c0564d877a | 42 | raw >>= 2; |
sammacjunkie | 0:48c0564d877a | 43 | return raw; |
sammacjunkie | 0:48c0564d877a | 44 | } |
sammacjunkie | 0:48c0564d877a | 45 | |
sammacjunkie | 0:48c0564d877a | 46 | /** TMP006 Read Raw Voltage method. |
sammacjunkie | 0:48c0564d877a | 47 | * Used for reading the raw voltage from the thermopile. |
sammacjunkie | 0:48c0564d877a | 48 | */ |
sammacjunkie | 0:48c0564d877a | 49 | int16_t TMP006::readRawVoltage(uint8_t addr) |
sammacjunkie | 0:48c0564d877a | 50 | { |
sammacjunkie | 0:48c0564d877a | 51 | char reg[1]; |
sammacjunkie | 0:48c0564d877a | 52 | char data[2]; |
sammacjunkie | 0:48c0564d877a | 53 | |
sammacjunkie | 0:48c0564d877a | 54 | reg[0] = TMP006_VOBJ; |
sammacjunkie | 0:48c0564d877a | 55 | |
sammacjunkie | 0:48c0564d877a | 56 | m_i2c.write(addr, reg, 1); |
sammacjunkie | 0:48c0564d877a | 57 | m_i2c.read(addr, data, 2); |
sammacjunkie | 0:48c0564d877a | 58 | |
sammacjunkie | 0:48c0564d877a | 59 | int16_t raw = (data[0] << 8) | data[1] ; |
sammacjunkie | 0:48c0564d877a | 60 | |
sammacjunkie | 0:48c0564d877a | 61 | return raw; |
sammacjunkie | 0:48c0564d877a | 62 | } |
sammacjunkie | 0:48c0564d877a | 63 | |
sammacjunkie | 0:48c0564d877a | 64 | /** TMP006 Read Object Temperature(C) method. |
sammacjunkie | 0:48c0564d877a | 65 | * Used for calculating the object temperature in celcius. |
sammacjunkie | 0:48c0564d877a | 66 | */ |
sammacjunkie | 0:48c0564d877a | 67 | double TMP006::readObjTempC(uint8_t addr) |
sammacjunkie | 0:48c0564d877a | 68 | { |
sammacjunkie | 0:48c0564d877a | 69 | double Tdie = readRawDieTemperature(addr); |
sammacjunkie | 0:48c0564d877a | 70 | double Vobj = readRawVoltage(addr); |
sammacjunkie | 0:48c0564d877a | 71 | Vobj *= 156.25; // 156.25 nV per LSB |
sammacjunkie | 0:48c0564d877a | 72 | Vobj /= 1000000000; // nV -> V |
sammacjunkie | 0:48c0564d877a | 73 | Tdie *= 0.03125; // convert to celsius |
sammacjunkie | 0:48c0564d877a | 74 | Tdie += 273.15; // convert to kelvin |
sammacjunkie | 0:48c0564d877a | 75 | |
sammacjunkie | 0:48c0564d877a | 76 | // Equations for calculating temperature found in section 5.1 in the user guide |
sammacjunkie | 0:48c0564d877a | 77 | double tdie_tref = Tdie - TMP006_TREF; |
sammacjunkie | 0:48c0564d877a | 78 | double S = (1 + TMP006_A1*tdie_tref + |
sammacjunkie | 0:48c0564d877a | 79 | TMP006_A2*tdie_tref*tdie_tref); |
sammacjunkie | 0:48c0564d877a | 80 | S *= TMP006_S0; |
sammacjunkie | 0:48c0564d877a | 81 | S /= 10000000; |
sammacjunkie | 0:48c0564d877a | 82 | S /= 10000000; |
sammacjunkie | 0:48c0564d877a | 83 | |
sammacjunkie | 0:48c0564d877a | 84 | double Vos = TMP006_B0 + TMP006_B1*tdie_tref + |
sammacjunkie | 0:48c0564d877a | 85 | TMP006_B2*tdie_tref*tdie_tref; |
sammacjunkie | 0:48c0564d877a | 86 | |
sammacjunkie | 0:48c0564d877a | 87 | double fVobj = (Vobj - Vos) + TMP006_C2*(Vobj-Vos)*(Vobj-Vos); |
sammacjunkie | 0:48c0564d877a | 88 | |
sammacjunkie | 0:48c0564d877a | 89 | double Tobj = sqrt(sqrt(Tdie * Tdie * Tdie * Tdie + fVobj/S)); |
sammacjunkie | 0:48c0564d877a | 90 | |
sammacjunkie | 0:48c0564d877a | 91 | Tobj -= 273.15; // Kelvin -> *C |
sammacjunkie | 0:48c0564d877a | 92 | return Tobj; |
sammacjunkie | 0:48c0564d877a | 93 | } |
sammacjunkie | 0:48c0564d877a | 94 | |
sammacjunkie | 0:48c0564d877a | 95 | /** TMP006 Read Object Temperature(F) method. |
sammacjunkie | 0:48c0564d877a | 96 | * Used for calculating the object temperature in fahrenheit. |
sammacjunkie | 0:48c0564d877a | 97 | */ |
sammacjunkie | 0:48c0564d877a | 98 | double TMP006::readObjTempF(uint8_t addr) |
sammacjunkie | 0:48c0564d877a | 99 | { |
sammacjunkie | 0:48c0564d877a | 100 | double Tobj = readObjTempC(addr); |
sammacjunkie | 0:48c0564d877a | 101 | Tobj = Tobj * 9.0/5.0 + 32.0; // convert to fahrenheit |
sammacjunkie | 0:48c0564d877a | 102 | return Tobj; |
sammacjunkie | 0:48c0564d877a | 103 | } |
sammacjunkie | 0:48c0564d877a | 104 | |
sammacjunkie | 0:48c0564d877a | 105 | /** TMP006 Read Die Temperature(C) method. |
sammacjunkie | 0:48c0564d877a | 106 | * Used for calculating the die temperature in celcius. |
sammacjunkie | 0:48c0564d877a | 107 | */ |
sammacjunkie | 0:48c0564d877a | 108 | double TMP006::readDieTempC(uint8_t addr) |
sammacjunkie | 0:48c0564d877a | 109 | { |
sammacjunkie | 0:48c0564d877a | 110 | double Tdie = readRawDieTemperature(addr); |
sammacjunkie | 0:48c0564d877a | 111 | Tdie *= 0.03125; // convert to celsius |
sammacjunkie | 0:48c0564d877a | 112 | return Tdie; |
sammacjunkie | 0:48c0564d877a | 113 | } |
sammacjunkie | 0:48c0564d877a | 114 | |
sammacjunkie | 0:48c0564d877a | 115 | /** TMP006 Read Die Temperature(F) method. |
sammacjunkie | 0:48c0564d877a | 116 | * Used for calculating the die temperature in fahrenheit. |
sammacjunkie | 0:48c0564d877a | 117 | */ |
sammacjunkie | 0:48c0564d877a | 118 | double TMP006::readDieTempF(uint8_t addr) |
sammacjunkie | 0:48c0564d877a | 119 | { |
sammacjunkie | 0:48c0564d877a | 120 | double Tdie = readDieTempC(addr); |
sammacjunkie | 0:48c0564d877a | 121 | Tdie = Tdie * 9.0/5.0 + 32.0; // convert to fahrenheit |
sammacjunkie | 0:48c0564d877a | 122 | return Tdie; |
sammacjunkie | 0:48c0564d877a | 123 | } |