Drivber for Ti's TMP007 Infrared Thermopile Sensor with Integrated Math Engine
src/TMP007.cpp@0:a1ed9ce625d4, 2016-06-05 (annotated)
- Committer:
- messi1
- Date:
- Sun Jun 05 19:38:48 2016 +0000
- Revision:
- 0:a1ed9ce625d4
- Child:
- 1:93710a3abf0a
Init version of TMP007 which is based on TMP006 class with a lot of rewriting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
messi1 | 0:a1ed9ce625d4 | 1 | #include <math.h> |
messi1 | 0:a1ed9ce625d4 | 2 | #include "TMP007.h" |
messi1 | 0:a1ed9ce625d4 | 3 | |
messi1 | 0:a1ed9ce625d4 | 4 | TMP007::TMP007(PinName sda, PinName scl, int addr) |
messi1 | 0:a1ed9ce625d4 | 5 | :_i2cPort(sda, scl), |
messi1 | 0:a1ed9ce625d4 | 6 | _addr(addr<<1) |
messi1 | 0:a1ed9ce625d4 | 7 | {} |
messi1 | 0:a1ed9ce625d4 | 8 | |
messi1 | 0:a1ed9ce625d4 | 9 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 10 | bool TMP007::writeWord(const uint8_t reg, const uint16_t word) |
messi1 | 0:a1ed9ce625d4 | 11 | { |
messi1 | 0:a1ed9ce625d4 | 12 | const char cmd[] = { reg, word & 0xff, (word&0xff00)>>8 }; |
messi1 | 0:a1ed9ce625d4 | 13 | |
messi1 | 0:a1ed9ce625d4 | 14 | if( 0 == _i2cPort.write(_addr, cmd, 3 )) |
messi1 | 0:a1ed9ce625d4 | 15 | return true; |
messi1 | 0:a1ed9ce625d4 | 16 | else |
messi1 | 0:a1ed9ce625d4 | 17 | return false; |
messi1 | 0:a1ed9ce625d4 | 18 | } |
messi1 | 0:a1ed9ce625d4 | 19 | |
messi1 | 0:a1ed9ce625d4 | 20 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 21 | uint16_t TMP007::readWord(const uint8_t reg) |
messi1 | 0:a1ed9ce625d4 | 22 | { |
messi1 | 0:a1ed9ce625d4 | 23 | uint16_t res = 0; |
messi1 | 0:a1ed9ce625d4 | 24 | char data[] = {0, 0}; |
messi1 | 0:a1ed9ce625d4 | 25 | const char cmd = reg; |
messi1 | 0:a1ed9ce625d4 | 26 | |
messi1 | 0:a1ed9ce625d4 | 27 | _i2cPort.write(_addr, &cmd, 1); |
messi1 | 0:a1ed9ce625d4 | 28 | _i2cPort.read(_addr, data, 2); |
messi1 | 0:a1ed9ce625d4 | 29 | res = data[1]<<8 & data[0]; |
messi1 | 0:a1ed9ce625d4 | 30 | |
messi1 | 0:a1ed9ce625d4 | 31 | return res; |
messi1 | 0:a1ed9ce625d4 | 32 | } |
messi1 | 0:a1ed9ce625d4 | 33 | |
messi1 | 0:a1ed9ce625d4 | 34 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 35 | void TMP007::writeConfig(const uint16_t value) |
messi1 | 0:a1ed9ce625d4 | 36 | { |
messi1 | 0:a1ed9ce625d4 | 37 | writeWord(TMP007_CONFIG, value); |
messi1 | 0:a1ed9ce625d4 | 38 | } |
messi1 | 0:a1ed9ce625d4 | 39 | |
messi1 | 0:a1ed9ce625d4 | 40 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 41 | uint16_t TMP007::readConfig() |
messi1 | 0:a1ed9ce625d4 | 42 | { |
messi1 | 0:a1ed9ce625d4 | 43 | uint16_t res = readWord(TMP007_CONFIG); |
messi1 | 0:a1ed9ce625d4 | 44 | return res; |
messi1 | 0:a1ed9ce625d4 | 45 | } |
messi1 | 0:a1ed9ce625d4 | 46 | |
messi1 | 0:a1ed9ce625d4 | 47 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 48 | uint16_t TMP007::readStatus() |
messi1 | 0:a1ed9ce625d4 | 49 | { |
messi1 | 0:a1ed9ce625d4 | 50 | return readWord(TMP007_STATUS); |
messi1 | 0:a1ed9ce625d4 | 51 | } |
messi1 | 0:a1ed9ce625d4 | 52 | |
messi1 | 0:a1ed9ce625d4 | 53 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 54 | /** TMP007 Config method. |
messi1 | 0:a1ed9ce625d4 | 55 | * Used for configuring the sensor with desired samples. |
messi1 | 0:a1ed9ce625d4 | 56 | */ |
messi1 | 0:a1ed9ce625d4 | 57 | void TMP007::setSamples(uint16_t samples) |
messi1 | 0:a1ed9ce625d4 | 58 | { |
messi1 | 0:a1ed9ce625d4 | 59 | char data[2]; |
messi1 | 0:a1ed9ce625d4 | 60 | |
messi1 | 0:a1ed9ce625d4 | 61 | data[0] = TMP007_CONFIG; |
messi1 | 0:a1ed9ce625d4 | 62 | data[1] = samples | TMP007_CFG_MODEON | TMP007_CFG_ALRTEN; |
messi1 | 0:a1ed9ce625d4 | 63 | |
messi1 | 0:a1ed9ce625d4 | 64 | _i2cPort.write( _addr, data, 1 ); |
messi1 | 0:a1ed9ce625d4 | 65 | } |
messi1 | 0:a1ed9ce625d4 | 66 | |
messi1 | 0:a1ed9ce625d4 | 67 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 68 | /** TMP007 Read Raw Temperature method. |
messi1 | 0:a1ed9ce625d4 | 69 | * Used for getting the raw data off the chip. |
messi1 | 0:a1ed9ce625d4 | 70 | */ |
messi1 | 0:a1ed9ce625d4 | 71 | uint16_t TMP007::readRawLocalTemperature() |
messi1 | 0:a1ed9ce625d4 | 72 | { |
messi1 | 0:a1ed9ce625d4 | 73 | int16_t raw = 0; |
messi1 | 0:a1ed9ce625d4 | 74 | char reg = TMP007_LOCAL_TEMP; |
messi1 | 0:a1ed9ce625d4 | 75 | char data[2]; |
messi1 | 0:a1ed9ce625d4 | 76 | |
messi1 | 0:a1ed9ce625d4 | 77 | _i2cPort.write(_addr, ®, 1); |
messi1 | 0:a1ed9ce625d4 | 78 | _i2cPort.read(_addr, data, 2); |
messi1 | 0:a1ed9ce625d4 | 79 | |
messi1 | 0:a1ed9ce625d4 | 80 | raw = (data[0] << 8) | data[1] ; |
messi1 | 0:a1ed9ce625d4 | 81 | |
messi1 | 0:a1ed9ce625d4 | 82 | raw >>= 2; |
messi1 | 0:a1ed9ce625d4 | 83 | return raw; |
messi1 | 0:a1ed9ce625d4 | 84 | } |
messi1 | 0:a1ed9ce625d4 | 85 | |
messi1 | 0:a1ed9ce625d4 | 86 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 87 | /** TMP007 Read Raw Voltage method. |
messi1 | 0:a1ed9ce625d4 | 88 | * Used for reading the raw voltage from the thermopile. |
messi1 | 0:a1ed9ce625d4 | 89 | */ |
messi1 | 0:a1ed9ce625d4 | 90 | uint16_t TMP007::readRawSensorVoltage() |
messi1 | 0:a1ed9ce625d4 | 91 | { |
messi1 | 0:a1ed9ce625d4 | 92 | char reg=TMP007_SENSOR_VOLT; |
messi1 | 0:a1ed9ce625d4 | 93 | char data[2]; |
messi1 | 0:a1ed9ce625d4 | 94 | |
messi1 | 0:a1ed9ce625d4 | 95 | _i2cPort.write(_addr, ®, 1); |
messi1 | 0:a1ed9ce625d4 | 96 | _i2cPort.read(_addr, data, 2); |
messi1 | 0:a1ed9ce625d4 | 97 | |
messi1 | 0:a1ed9ce625d4 | 98 | int16_t raw = (data[0] << 8) | data[1] ; |
messi1 | 0:a1ed9ce625d4 | 99 | |
messi1 | 0:a1ed9ce625d4 | 100 | return raw; |
messi1 | 0:a1ed9ce625d4 | 101 | } |
messi1 | 0:a1ed9ce625d4 | 102 | |
messi1 | 0:a1ed9ce625d4 | 103 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 104 | /** TMP007 Read Object Temperature(C) method. |
messi1 | 0:a1ed9ce625d4 | 105 | * Used for calculating the object temperature in celcius. |
messi1 | 0:a1ed9ce625d4 | 106 | */ |
messi1 | 0:a1ed9ce625d4 | 107 | double TMP007::readObjTempC() |
messi1 | 0:a1ed9ce625d4 | 108 | { |
messi1 | 0:a1ed9ce625d4 | 109 | double Tdie = readRawLocalTemperature(); |
messi1 | 0:a1ed9ce625d4 | 110 | double Vobj = readRawSensorVoltage(); |
messi1 | 0:a1ed9ce625d4 | 111 | Vobj *= 156.25; // 156.25 nV per LSB |
messi1 | 0:a1ed9ce625d4 | 112 | Vobj /= 1000000000; // nV -> V |
messi1 | 0:a1ed9ce625d4 | 113 | Tdie *= 0.03125; // convert to celsius |
messi1 | 0:a1ed9ce625d4 | 114 | Tdie += 273.15; // convert to kelvin |
messi1 | 0:a1ed9ce625d4 | 115 | |
messi1 | 0:a1ed9ce625d4 | 116 | // Equations for calculating temperature found in section 7.3.4 in the user guide |
messi1 | 0:a1ed9ce625d4 | 117 | double tdie_tref = Tdie - TMP007_TREF; |
messi1 | 0:a1ed9ce625d4 | 118 | double S = (1 + TMP007_A1*tdie_tref + |
messi1 | 0:a1ed9ce625d4 | 119 | TMP007_A2*tdie_tref*tdie_tref); |
messi1 | 0:a1ed9ce625d4 | 120 | S *= TMP007_S0; |
messi1 | 0:a1ed9ce625d4 | 121 | S /= 10000000; |
messi1 | 0:a1ed9ce625d4 | 122 | S /= 10000000; |
messi1 | 0:a1ed9ce625d4 | 123 | |
messi1 | 0:a1ed9ce625d4 | 124 | double Vos = TMP007_B0 + TMP007_B1*tdie_tref + TMP007_B2*tdie_tref*tdie_tref; |
messi1 | 0:a1ed9ce625d4 | 125 | |
messi1 | 0:a1ed9ce625d4 | 126 | double fVobj = (Vobj - Vos) + TMP007_C2*(Vobj-Vos)*(Vobj-Vos); |
messi1 | 0:a1ed9ce625d4 | 127 | |
messi1 | 0:a1ed9ce625d4 | 128 | double Tobj = sqrt(sqrt(Tdie * Tdie * Tdie * Tdie + fVobj/S)); |
messi1 | 0:a1ed9ce625d4 | 129 | |
messi1 | 0:a1ed9ce625d4 | 130 | Tobj -= 273.15; // Kelvin -> *C |
messi1 | 0:a1ed9ce625d4 | 131 | return Tobj; |
messi1 | 0:a1ed9ce625d4 | 132 | } |
messi1 | 0:a1ed9ce625d4 | 133 | |
messi1 | 0:a1ed9ce625d4 | 134 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 135 | /** TMP007 Read Object Temperature(F) method. |
messi1 | 0:a1ed9ce625d4 | 136 | * Used for calculating the object temperature in fahrenheit. |
messi1 | 0:a1ed9ce625d4 | 137 | */ |
messi1 | 0:a1ed9ce625d4 | 138 | double TMP007::readObjTempF() |
messi1 | 0:a1ed9ce625d4 | 139 | { |
messi1 | 0:a1ed9ce625d4 | 140 | double Tobj = readObjTempC(); |
messi1 | 0:a1ed9ce625d4 | 141 | Tobj = Tobj * 9.0/5.0 + 32.0; // convert to fahrenheit |
messi1 | 0:a1ed9ce625d4 | 142 | return Tobj; |
messi1 | 0:a1ed9ce625d4 | 143 | } |
messi1 | 0:a1ed9ce625d4 | 144 | |
messi1 | 0:a1ed9ce625d4 | 145 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 146 | /** TMP007 Read Die Temperature(C) method. |
messi1 | 0:a1ed9ce625d4 | 147 | * Used for calculating the die temperature in celcius. |
messi1 | 0:a1ed9ce625d4 | 148 | */ |
messi1 | 0:a1ed9ce625d4 | 149 | double TMP007::readDieTempC() |
messi1 | 0:a1ed9ce625d4 | 150 | { |
messi1 | 0:a1ed9ce625d4 | 151 | double Tdie = readRawLocalTemperature(); |
messi1 | 0:a1ed9ce625d4 | 152 | Tdie *= 0.03125; // convert to celsius |
messi1 | 0:a1ed9ce625d4 | 153 | return Tdie; |
messi1 | 0:a1ed9ce625d4 | 154 | } |
messi1 | 0:a1ed9ce625d4 | 155 | |
messi1 | 0:a1ed9ce625d4 | 156 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 157 | /** TMP007 Read Die Temperature(F) method. |
messi1 | 0:a1ed9ce625d4 | 158 | * Used for calculating the die temperature in fahrenheit. |
messi1 | 0:a1ed9ce625d4 | 159 | */ |
messi1 | 0:a1ed9ce625d4 | 160 | double TMP007::readDieTempF() |
messi1 | 0:a1ed9ce625d4 | 161 | { |
messi1 | 0:a1ed9ce625d4 | 162 | double Tdie = readDieTempC(); |
messi1 | 0:a1ed9ce625d4 | 163 | Tdie = Tdie * 9.0/5.0 + 32.0; // convert to fahrenheit |
messi1 | 0:a1ed9ce625d4 | 164 | return Tdie; |
messi1 | 0:a1ed9ce625d4 | 165 | } |