Dependencies:   dispBoB mbed PCA9635

Committer:
d_worrall
Date:
Mon Jul 11 13:57:57 2011 +0000
Revision:
1:1d49d49b77f8
Parent:
0:ba39353df5e7

        

Who changed what in which revision?

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