mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 #include "TMP102.h"
elessair 0:f269e3021894 2
elessair 0:f269e3021894 3 #define TEMP_REG_ADDR 0x00
elessair 0:f269e3021894 4
elessair 0:f269e3021894 5 TMP102::TMP102(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
elessair 0:f269e3021894 6 m_i2c.frequency(400000);
elessair 0:f269e3021894 7 }
elessair 0:f269e3021894 8 TMP102::~TMP102() { }
elessair 0:f269e3021894 9
elessair 0:f269e3021894 10 float TMP102::read() {
elessair 0:f269e3021894 11 const char tempRegAddr = TEMP_REG_ADDR;
elessair 0:f269e3021894 12
elessair 0:f269e3021894 13 m_i2c.write(m_addr, &tempRegAddr, 1);
elessair 0:f269e3021894 14
elessair 0:f269e3021894 15 char reg[2] = {0,0};
elessair 0:f269e3021894 16 m_i2c.read(m_addr, reg, 2);
elessair 0:f269e3021894 17
elessair 0:f269e3021894 18 unsigned short res = (reg[0] << 4) | (reg[1] >> 4);
elessair 0:f269e3021894 19
elessair 0:f269e3021894 20 float temp = (float) ((float)res * 0.0625);
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 return temp;
elessair 0:f269e3021894 23 }