TMP102 published as a library that can be imported independent

Dependents:   TMP102HelloWorld TCPServer BLE_Health_Thermometer_IRC BLE_Health_Thermometer_HeartRateMonitor ... more

Committer:
chris
Date:
Thu Mar 07 10:43:04 2013 +0000
Revision:
2:40e813248518
Parent:
1:7585766ad401
Child:
3:694792b93731
Updated read function to work correctly with negative numbers.; As per : http://mbed.org/cookbook/TMP102-Temperature-Sensor#c4313;

Who changed what in which revision?

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