Interface with TMP112 and TMP102 temperature sensor
TMP112.cpp@0:2fbb524f0609, 2016-05-12 (annotated)
- Committer:
- CamiloRuiz
- Date:
- Thu May 12 21:49:05 2016 +0000
- Revision:
- 0:2fbb524f0609
First working version.; ; extended mode, negative values and oneshot mode are not supported yet.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CamiloRuiz | 0:2fbb524f0609 | 1 | /* |
CamiloRuiz | 0:2fbb524f0609 | 2 | Copyright (c) 2016 Camilo Ruiz |
CamiloRuiz | 0:2fbb524f0609 | 3 | |
CamiloRuiz | 0:2fbb524f0609 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
CamiloRuiz | 0:2fbb524f0609 | 5 | of this software and associated documentation files (the "Software"), to deal |
CamiloRuiz | 0:2fbb524f0609 | 6 | in the Software without restriction, including without limitation the rights |
CamiloRuiz | 0:2fbb524f0609 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
CamiloRuiz | 0:2fbb524f0609 | 8 | copies of the Software, and to permit persons to whom the Software is |
CamiloRuiz | 0:2fbb524f0609 | 9 | furnished to do so, subject to the following conditions: |
CamiloRuiz | 0:2fbb524f0609 | 10 | |
CamiloRuiz | 0:2fbb524f0609 | 11 | The above copyright notice and this permission notice shall be included in |
CamiloRuiz | 0:2fbb524f0609 | 12 | all copies or substantial portions of the Software. |
CamiloRuiz | 0:2fbb524f0609 | 13 | |
CamiloRuiz | 0:2fbb524f0609 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
CamiloRuiz | 0:2fbb524f0609 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
CamiloRuiz | 0:2fbb524f0609 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
CamiloRuiz | 0:2fbb524f0609 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
CamiloRuiz | 0:2fbb524f0609 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
CamiloRuiz | 0:2fbb524f0609 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
CamiloRuiz | 0:2fbb524f0609 | 20 | THE SOFTWARE. |
CamiloRuiz | 0:2fbb524f0609 | 21 | */ |
CamiloRuiz | 0:2fbb524f0609 | 22 | |
CamiloRuiz | 0:2fbb524f0609 | 23 | //this should work with tmp102 too |
CamiloRuiz | 0:2fbb524f0609 | 24 | //I do not use negative values on my application, this code has to be tweaked to support it |
CamiloRuiz | 0:2fbb524f0609 | 25 | //I do not use extended mode, this code has to be tweaked to support it |
CamiloRuiz | 0:2fbb524f0609 | 26 | |
CamiloRuiz | 0:2fbb524f0609 | 27 | //greetings from Colombia :) |
CamiloRuiz | 0:2fbb524f0609 | 28 | |
CamiloRuiz | 0:2fbb524f0609 | 29 | #include "TMP112.h" |
CamiloRuiz | 0:2fbb524f0609 | 30 | |
CamiloRuiz | 0:2fbb524f0609 | 31 | #define TEMP_REG_ADDR 0x00 |
CamiloRuiz | 0:2fbb524f0609 | 32 | |
CamiloRuiz | 0:2fbb524f0609 | 33 | TMP112::TMP112(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) |
CamiloRuiz | 0:2fbb524f0609 | 34 | { |
CamiloRuiz | 0:2fbb524f0609 | 35 | extended_mode=0; //default, extended mode off |
CamiloRuiz | 0:2fbb524f0609 | 36 | } |
CamiloRuiz | 0:2fbb524f0609 | 37 | |
CamiloRuiz | 0:2fbb524f0609 | 38 | TMP112::~TMP112() |
CamiloRuiz | 0:2fbb524f0609 | 39 | { |
CamiloRuiz | 0:2fbb524f0609 | 40 | } |
CamiloRuiz | 0:2fbb524f0609 | 41 | |
CamiloRuiz | 0:2fbb524f0609 | 42 | |
CamiloRuiz | 0:2fbb524f0609 | 43 | /** TMP112 read one of the three available temperature registers |
CamiloRuiz | 0:2fbb524f0609 | 44 | * return in celsius |
CamiloRuiz | 0:2fbb524f0609 | 45 | */ |
CamiloRuiz | 0:2fbb524f0609 | 46 | float TMP112::readTmpReg(char reg) |
CamiloRuiz | 0:2fbb524f0609 | 47 | { |
CamiloRuiz | 0:2fbb524f0609 | 48 | m_i2c.write(m_addr, ®, 1); //Pointer to the temperature register |
CamiloRuiz | 0:2fbb524f0609 | 49 | char data[2] = {0,0}; |
CamiloRuiz | 0:2fbb524f0609 | 50 | m_i2c.read(m_addr, data, 2); //Read |
CamiloRuiz | 0:2fbb524f0609 | 51 | int16_t res = ((int8_t)data[0] << 4) | ((uint8_t)data[1] >> 4); |
CamiloRuiz | 0:2fbb524f0609 | 52 | float temp = (float) ((float)res * 0.0625); |
CamiloRuiz | 0:2fbb524f0609 | 53 | return temp; |
CamiloRuiz | 0:2fbb524f0609 | 54 | } |
CamiloRuiz | 0:2fbb524f0609 | 55 | |
CamiloRuiz | 0:2fbb524f0609 | 56 | /** TMP112 get the actual temperature of the sensor |
CamiloRuiz | 0:2fbb524f0609 | 57 | */ |
CamiloRuiz | 0:2fbb524f0609 | 58 | float TMP112::readTemperature() |
CamiloRuiz | 0:2fbb524f0609 | 59 | { |
CamiloRuiz | 0:2fbb524f0609 | 60 | return readTmpReg(0); |
CamiloRuiz | 0:2fbb524f0609 | 61 | } |
CamiloRuiz | 0:2fbb524f0609 | 62 | |
CamiloRuiz | 0:2fbb524f0609 | 63 | /** TMP112 read the hi temp of the thermostat stored in the sensor |
CamiloRuiz | 0:2fbb524f0609 | 64 | */ |
CamiloRuiz | 0:2fbb524f0609 | 65 | float TMP112::readTM_Htemp() |
CamiloRuiz | 0:2fbb524f0609 | 66 | { |
CamiloRuiz | 0:2fbb524f0609 | 67 | return readTmpReg(3); |
CamiloRuiz | 0:2fbb524f0609 | 68 | } |
CamiloRuiz | 0:2fbb524f0609 | 69 | |
CamiloRuiz | 0:2fbb524f0609 | 70 | /** TMP112 read the low temp of the thermostat stored in the sensor |
CamiloRuiz | 0:2fbb524f0609 | 71 | */ |
CamiloRuiz | 0:2fbb524f0609 | 72 | float TMP112::readTM_Ltemp() |
CamiloRuiz | 0:2fbb524f0609 | 73 | { |
CamiloRuiz | 0:2fbb524f0609 | 74 | return readTmpReg(2); |
CamiloRuiz | 0:2fbb524f0609 | 75 | } |
CamiloRuiz | 0:2fbb524f0609 | 76 | |
CamiloRuiz | 0:2fbb524f0609 | 77 | /** TMP112 read the cfg register |
CamiloRuiz | 0:2fbb524f0609 | 78 | */ |
CamiloRuiz | 0:2fbb524f0609 | 79 | uint16_t TMP112::readCFG() |
CamiloRuiz | 0:2fbb524f0609 | 80 | { |
CamiloRuiz | 0:2fbb524f0609 | 81 | char reg=1; |
CamiloRuiz | 0:2fbb524f0609 | 82 | m_i2c.write(m_addr, ®, 1); //Pointer to the cfg register |
CamiloRuiz | 0:2fbb524f0609 | 83 | char data[2] = {0,0}; |
CamiloRuiz | 0:2fbb524f0609 | 84 | m_i2c.read(m_addr, data, 2); //Read |
CamiloRuiz | 0:2fbb524f0609 | 85 | return (data[0] << 8) | data[1]; |
CamiloRuiz | 0:2fbb524f0609 | 86 | } |
CamiloRuiz | 0:2fbb524f0609 | 87 | |
CamiloRuiz | 0:2fbb524f0609 | 88 | /** TMP112 write the cfg register |
CamiloRuiz | 0:2fbb524f0609 | 89 | */ |
CamiloRuiz | 0:2fbb524f0609 | 90 | void TMP112::writeCFG(uint16_t cfg) |
CamiloRuiz | 0:2fbb524f0609 | 91 | { |
CamiloRuiz | 0:2fbb524f0609 | 92 | //char reg=1; |
CamiloRuiz | 0:2fbb524f0609 | 93 | //m_i2c.write(m_addr, ®, 1); //Pointer to the cfg register |
CamiloRuiz | 0:2fbb524f0609 | 94 | char data[3]; |
CamiloRuiz | 0:2fbb524f0609 | 95 | data[0]=1; //Pointer to the cfg register |
CamiloRuiz | 0:2fbb524f0609 | 96 | data[1]=cfg>>8; |
CamiloRuiz | 0:2fbb524f0609 | 97 | data[2]=cfg & 0xFF; |
CamiloRuiz | 0:2fbb524f0609 | 98 | m_i2c.write(m_addr, data, 3); //write |
CamiloRuiz | 0:2fbb524f0609 | 99 | } |
CamiloRuiz | 0:2fbb524f0609 | 100 | /** TMP112 write a threshold temp o thermostat into the sensor |
CamiloRuiz | 0:2fbb524f0609 | 101 | */ |
CamiloRuiz | 0:2fbb524f0609 | 102 | void TMP112::writeTmpReg(char reg, float temp){ |
CamiloRuiz | 0:2fbb524f0609 | 103 | uint16_t regValue= (uint16_t) (temp/0.0625); |
CamiloRuiz | 0:2fbb524f0609 | 104 | char data[3]; |
CamiloRuiz | 0:2fbb524f0609 | 105 | data[0]=reg; //Pointer to the cfg register |
CamiloRuiz | 0:2fbb524f0609 | 106 | data[1]=(regValue>>4)& 0xFF; |
CamiloRuiz | 0:2fbb524f0609 | 107 | data[2]=(regValue << 4) & 0xFF; |
CamiloRuiz | 0:2fbb524f0609 | 108 | m_i2c.write(m_addr, data, 3); //write |
CamiloRuiz | 0:2fbb524f0609 | 109 | } |
CamiloRuiz | 0:2fbb524f0609 | 110 | |
CamiloRuiz | 0:2fbb524f0609 | 111 | //write thermostat temperatures high |
CamiloRuiz | 0:2fbb524f0609 | 112 | void TMP112::write_TM_Htemp(float temp){ |
CamiloRuiz | 0:2fbb524f0609 | 113 | writeTmpReg(3,temp); |
CamiloRuiz | 0:2fbb524f0609 | 114 | } |
CamiloRuiz | 0:2fbb524f0609 | 115 | //write thermostat temperatures low |
CamiloRuiz | 0:2fbb524f0609 | 116 | void TMP112::write_TM_Ltemp(float temp){ |
CamiloRuiz | 0:2fbb524f0609 | 117 | writeTmpReg(2,temp); |
CamiloRuiz | 0:2fbb524f0609 | 118 | } |
CamiloRuiz | 0:2fbb524f0609 | 119 |