Library for TMP102 temperature I2C sensor
Dependents: mbed_Shield_LCD_Temperature mbed_Shield_Temperature
Fork of TMP102 by
TMP102.cpp@1:7585766ad401, 2012-07-21 (annotated)
- Committer:
- chris
- Date:
- Sat Jul 21 22:40:40 2012 +0000
- Revision:
- 1:7585766ad401
- Parent:
- 0:374d9678d5ad
- Child:
- 2:40e813248518
Removed extra declaration of I2C interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 0:374d9678d5ad | 1 | |
chris | 0:374d9678d5ad | 2 | /* |
chris | 0:374d9678d5ad | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
chris | 0:374d9678d5ad | 4 | |
chris | 0:374d9678d5ad | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
chris | 0:374d9678d5ad | 6 | of this software and associated documentation files (the "Software"), to deal |
chris | 0:374d9678d5ad | 7 | in the Software without restriction, including without limitation the rights |
chris | 0:374d9678d5ad | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
chris | 0:374d9678d5ad | 9 | copies of the Software, and to permit persons to whom the Software is |
chris | 0:374d9678d5ad | 10 | furnished to do so, subject to the following conditions: |
chris | 0:374d9678d5ad | 11 | |
chris | 0:374d9678d5ad | 12 | The above copyright notice and this permission notice shall be included in |
chris | 0:374d9678d5ad | 13 | all copies or substantial portions of the Software. |
chris | 0:374d9678d5ad | 14 | |
chris | 0:374d9678d5ad | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
chris | 0:374d9678d5ad | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
chris | 0:374d9678d5ad | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
chris | 0:374d9678d5ad | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
chris | 0:374d9678d5ad | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
chris | 0:374d9678d5ad | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
chris | 0:374d9678d5ad | 21 | THE SOFTWARE. |
chris | 0:374d9678d5ad | 22 | */ |
chris | 0:374d9678d5ad | 23 | |
chris | 0:374d9678d5ad | 24 | #include "TMP102.h" |
chris | 0:374d9678d5ad | 25 | |
chris | 0:374d9678d5ad | 26 | #define TEMP_REG_ADDR 0x00 |
chris | 0:374d9678d5ad | 27 | |
chris | 0:374d9678d5ad | 28 | TMP102::TMP102(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) |
chris | 0:374d9678d5ad | 29 | { |
chris | 0:374d9678d5ad | 30 | |
chris | 0:374d9678d5ad | 31 | } |
chris | 0:374d9678d5ad | 32 | |
chris | 0:374d9678d5ad | 33 | TMP102::~TMP102() |
chris | 0:374d9678d5ad | 34 | { |
chris | 0:374d9678d5ad | 35 | |
chris | 0:374d9678d5ad | 36 | } |
chris | 0:374d9678d5ad | 37 | |
chris | 0:374d9678d5ad | 38 | float TMP102::read() |
chris | 0:374d9678d5ad | 39 | { |
chris | 0:374d9678d5ad | 40 | |
chris | 0:374d9678d5ad | 41 | const char tempRegAddr = TEMP_REG_ADDR; |
chris | 0:374d9678d5ad | 42 | |
chris | 0:374d9678d5ad | 43 | m_i2c.write(m_addr, &tempRegAddr, 1); //Pointer to the temperature register |
chris | 0:374d9678d5ad | 44 | |
chris | 0:374d9678d5ad | 45 | char reg[2] = {0,0}; |
chris | 0:374d9678d5ad | 46 | m_i2c.read(m_addr, reg, 2); //Rea |
chris | 0:374d9678d5ad | 47 | |
chris | 0:374d9678d5ad | 48 | unsigned short res = (reg[0] << 4) | (reg[1] >> 4); |
chris | 0:374d9678d5ad | 49 | |
chris | 0:374d9678d5ad | 50 | float temp = (float) ((float)res * 0.0625); |
chris | 0:374d9678d5ad | 51 | |
chris | 0:374d9678d5ad | 52 | return temp; |
chris | 0:374d9678d5ad | 53 | } |