fork of TMP102, added read_u16 functionality

Fork of TMP102 by Chris Styles

Committer:
atarbey
Date:
Thu Dec 08 11:07:51 2016 +0000
Revision:
5:4169d23725c6
Parent:
4:0319e5fee951
Fix uncompatible datatype uint / int

Who changed what in which revision?

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