Drivber for Ti's TMP007 Infrared Thermopile Sensor with Integrated Math Engine
src/TMP007.cpp@1:93710a3abf0a, 2016-06-05 (annotated)
- Committer:
- messi1
- Date:
- Sun Jun 05 19:53:43 2016 +0000
- Revision:
- 1:93710a3abf0a
- Parent:
- 0:a1ed9ce625d4
Add TMP007_Defs.h file
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 | 1:93710a3abf0a | 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 | 1:93710a3abf0a | 53 | //-------------------------------------------------------------------------------- |
messi1 | 1:93710a3abf0a | 54 | void TMP007::writeStatusMask(const uint16_t value) |
messi1 | 1:93710a3abf0a | 55 | { |
messi1 | 1:93710a3abf0a | 56 | writeWord(TMP007_STATUS_MASK, value); |
messi1 | 1:93710a3abf0a | 57 | } |
messi1 | 1:93710a3abf0a | 58 | |
messi1 | 0:a1ed9ce625d4 | 59 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 60 | /** TMP007 Config method. |
messi1 | 0:a1ed9ce625d4 | 61 | * Used for configuring the sensor with desired samples. |
messi1 | 0:a1ed9ce625d4 | 62 | */ |
messi1 | 0:a1ed9ce625d4 | 63 | void TMP007::setSamples(uint16_t samples) |
messi1 | 0:a1ed9ce625d4 | 64 | { |
messi1 | 0:a1ed9ce625d4 | 65 | char data[2]; |
messi1 | 0:a1ed9ce625d4 | 66 | |
messi1 | 0:a1ed9ce625d4 | 67 | data[0] = TMP007_CONFIG; |
messi1 | 0:a1ed9ce625d4 | 68 | data[1] = samples | TMP007_CFG_MODEON | TMP007_CFG_ALRTEN; |
messi1 | 0:a1ed9ce625d4 | 69 | |
messi1 | 0:a1ed9ce625d4 | 70 | _i2cPort.write( _addr, data, 1 ); |
messi1 | 0:a1ed9ce625d4 | 71 | } |
messi1 | 0:a1ed9ce625d4 | 72 | |
messi1 | 0:a1ed9ce625d4 | 73 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 74 | /** TMP007 Read Raw Temperature method. |
messi1 | 0:a1ed9ce625d4 | 75 | * Used for getting the raw data off the chip. |
messi1 | 0:a1ed9ce625d4 | 76 | */ |
messi1 | 0:a1ed9ce625d4 | 77 | uint16_t TMP007::readRawLocalTemperature() |
messi1 | 0:a1ed9ce625d4 | 78 | { |
messi1 | 1:93710a3abf0a | 79 | uint16_t raw = readWord(TMP007_LOCAL_TEMP); |
messi1 | 0:a1ed9ce625d4 | 80 | |
messi1 | 0:a1ed9ce625d4 | 81 | raw >>= 2; |
messi1 | 0:a1ed9ce625d4 | 82 | return raw; |
messi1 | 0:a1ed9ce625d4 | 83 | } |
messi1 | 0:a1ed9ce625d4 | 84 | |
messi1 | 0:a1ed9ce625d4 | 85 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 86 | /** TMP007 Read Raw Voltage method. |
messi1 | 0:a1ed9ce625d4 | 87 | * Used for reading the raw voltage from the thermopile. |
messi1 | 0:a1ed9ce625d4 | 88 | */ |
messi1 | 0:a1ed9ce625d4 | 89 | uint16_t TMP007::readRawSensorVoltage() |
messi1 | 0:a1ed9ce625d4 | 90 | { |
messi1 | 1:93710a3abf0a | 91 | return readWord(TMP007_SENSOR_VOLT); |
messi1 | 0:a1ed9ce625d4 | 92 | } |
messi1 | 0:a1ed9ce625d4 | 93 | |
messi1 | 0:a1ed9ce625d4 | 94 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 95 | /** TMP007 Read Object Temperature(C) method. |
messi1 | 0:a1ed9ce625d4 | 96 | * Used for calculating the object temperature in celcius. |
messi1 | 0:a1ed9ce625d4 | 97 | */ |
messi1 | 0:a1ed9ce625d4 | 98 | double TMP007::readObjTempC() |
messi1 | 0:a1ed9ce625d4 | 99 | { |
messi1 | 0:a1ed9ce625d4 | 100 | double Tdie = readRawLocalTemperature(); |
messi1 | 0:a1ed9ce625d4 | 101 | double Vobj = readRawSensorVoltage(); |
messi1 | 0:a1ed9ce625d4 | 102 | Vobj *= 156.25; // 156.25 nV per LSB |
messi1 | 0:a1ed9ce625d4 | 103 | Vobj /= 1000000000; // nV -> V |
messi1 | 0:a1ed9ce625d4 | 104 | Tdie *= 0.03125; // convert to celsius |
messi1 | 0:a1ed9ce625d4 | 105 | Tdie += 273.15; // convert to kelvin |
messi1 | 0:a1ed9ce625d4 | 106 | |
messi1 | 0:a1ed9ce625d4 | 107 | // Equations for calculating temperature found in section 7.3.4 in the user guide |
messi1 | 0:a1ed9ce625d4 | 108 | double tdie_tref = Tdie - TMP007_TREF; |
messi1 | 1:93710a3abf0a | 109 | double S = (1 + TMP007_A1*tdie_tref + TMP007_A2*tdie_tref*tdie_tref); |
messi1 | 0:a1ed9ce625d4 | 110 | S *= TMP007_S0; |
messi1 | 0:a1ed9ce625d4 | 111 | S /= 10000000; |
messi1 | 0:a1ed9ce625d4 | 112 | S /= 10000000; |
messi1 | 0:a1ed9ce625d4 | 113 | |
messi1 | 0:a1ed9ce625d4 | 114 | double Vos = TMP007_B0 + TMP007_B1*tdie_tref + TMP007_B2*tdie_tref*tdie_tref; |
messi1 | 0:a1ed9ce625d4 | 115 | |
messi1 | 0:a1ed9ce625d4 | 116 | double fVobj = (Vobj - Vos) + TMP007_C2*(Vobj-Vos)*(Vobj-Vos); |
messi1 | 0:a1ed9ce625d4 | 117 | |
messi1 | 0:a1ed9ce625d4 | 118 | double Tobj = sqrt(sqrt(Tdie * Tdie * Tdie * Tdie + fVobj/S)); |
messi1 | 0:a1ed9ce625d4 | 119 | |
messi1 | 0:a1ed9ce625d4 | 120 | Tobj -= 273.15; // Kelvin -> *C |
messi1 | 0:a1ed9ce625d4 | 121 | return Tobj; |
messi1 | 0:a1ed9ce625d4 | 122 | } |
messi1 | 0:a1ed9ce625d4 | 123 | |
messi1 | 0:a1ed9ce625d4 | 124 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 125 | /** TMP007 Read Object Temperature(F) method. |
messi1 | 0:a1ed9ce625d4 | 126 | * Used for calculating the object temperature in fahrenheit. |
messi1 | 0:a1ed9ce625d4 | 127 | */ |
messi1 | 0:a1ed9ce625d4 | 128 | double TMP007::readObjTempF() |
messi1 | 0:a1ed9ce625d4 | 129 | { |
messi1 | 0:a1ed9ce625d4 | 130 | double Tobj = readObjTempC(); |
messi1 | 0:a1ed9ce625d4 | 131 | Tobj = Tobj * 9.0/5.0 + 32.0; // convert to fahrenheit |
messi1 | 0:a1ed9ce625d4 | 132 | return Tobj; |
messi1 | 0:a1ed9ce625d4 | 133 | } |
messi1 | 0:a1ed9ce625d4 | 134 | |
messi1 | 0:a1ed9ce625d4 | 135 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 136 | /** TMP007 Read Die Temperature(C) method. |
messi1 | 0:a1ed9ce625d4 | 137 | * Used for calculating the die temperature in celcius. |
messi1 | 0:a1ed9ce625d4 | 138 | */ |
messi1 | 0:a1ed9ce625d4 | 139 | double TMP007::readDieTempC() |
messi1 | 0:a1ed9ce625d4 | 140 | { |
messi1 | 0:a1ed9ce625d4 | 141 | double Tdie = readRawLocalTemperature(); |
messi1 | 0:a1ed9ce625d4 | 142 | Tdie *= 0.03125; // convert to celsius |
messi1 | 0:a1ed9ce625d4 | 143 | return Tdie; |
messi1 | 0:a1ed9ce625d4 | 144 | } |
messi1 | 0:a1ed9ce625d4 | 145 | |
messi1 | 0:a1ed9ce625d4 | 146 | //-------------------------------------------------------------------------------- |
messi1 | 0:a1ed9ce625d4 | 147 | /** TMP007 Read Die Temperature(F) method. |
messi1 | 0:a1ed9ce625d4 | 148 | * Used for calculating the die temperature in fahrenheit. |
messi1 | 0:a1ed9ce625d4 | 149 | */ |
messi1 | 0:a1ed9ce625d4 | 150 | double TMP007::readDieTempF() |
messi1 | 0:a1ed9ce625d4 | 151 | { |
messi1 | 0:a1ed9ce625d4 | 152 | double Tdie = readDieTempC(); |
messi1 | 0:a1ed9ce625d4 | 153 | Tdie = Tdie * 9.0/5.0 + 32.0; // convert to fahrenheit |
messi1 | 0:a1ed9ce625d4 | 154 | return Tdie; |
messi1 | 0:a1ed9ce625d4 | 155 | } |