A library for STMicroelectronics STTS751 I2C temperature sensor
STTS751.cpp
- Committer:
- takuo
- Date:
- 2014-01-19
- Revision:
- 2:3116fe4a0079
- Parent:
- 1:b0a3645a3dba
- Child:
- 3:f9d3008f7e8f
File content as of revision 2:3116fe4a0079:
/* A library for STMicroelectronics STTS751 I2C temperature sensor * Copyright 2014, Takuo WATANABE (wtakuo) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "STTS751.h" #define SET_BITS(x,m,b) (((x) & !(m)) | (b)) STTS751::STTS751(PinName sda, PinName scl, Address addr, Model model): _i2c(sda, scl), _addr(addr | model) { init(); } STTS751::STTS751(I2C &i2c, Address addr, Model model): _i2c(i2c), _addr(addr | model) { init(); } void STTS751::init() { write8(REG_CONFIGURATION, 0x00); write8(REG_CONV_RATE, 0x04); } int STTS751::addr() { return _addr; } STTS751::Resolution STTS751::resolution() { char conf = read8(REG_CONFIGURATION); return (STTS751::Resolution)(conf & RES_MASK); } void STTS751::setResolution(STTS751::Resolution res) { char conf = read8(REG_CONFIGURATION); conf = SET_BITS(conf, RES_MASK, res); write8(REG_CONFIGURATION, conf); } float STTS751::temp() { signed char h = read8(REG_TEMPERATURE_H); unsigned char l = read8(REG_TEMPERATURE_L); return ((h << 8) | l) / 256.0; } #ifdef MBED_OPERATORS STTS751::operator float() { return temp(); } #endif char STTS751::read8(char reg) { _i2c.write(_addr, ®, 1, true); _i2c.read(_addr, ®, 1); return reg; } void STTS751::write8(char reg, char data) { char buff[2] = { reg, data }; _i2c.write(_addr, buff, 2); }