A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

Committer:
takuo
Date:
Wed Feb 05 04:24:36 2014 +0000
Revision:
3:f9d3008f7e8f
Parent:
2:3116fe4a0079
Standby mode is now supported.; Conversion rate can be changed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takuo 1:b0a3645a3dba 1 /* A library for STMicroelectronics STTS751 I2C temperature sensor
takuo 0:4211e78bfa5d 2 * Copyright 2014, Takuo WATANABE (wtakuo)
takuo 0:4211e78bfa5d 3 *
takuo 0:4211e78bfa5d 4 * Licensed under the Apache License, Version 2.0 (the "License");
takuo 0:4211e78bfa5d 5 * you may not use this file except in compliance with the License.
takuo 0:4211e78bfa5d 6 * You may obtain a copy of the License at
takuo 0:4211e78bfa5d 7 *
takuo 0:4211e78bfa5d 8 * http://www.apache.org/licenses/LICENSE-2.0
takuo 0:4211e78bfa5d 9 *
takuo 0:4211e78bfa5d 10 * Unless required by applicable law or agreed to in writing, software
takuo 0:4211e78bfa5d 11 * distributed under the License is distributed on an "AS IS" BASIS,
takuo 0:4211e78bfa5d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
takuo 0:4211e78bfa5d 13 * See the License for the specific language governing permissions and
takuo 0:4211e78bfa5d 14 * limitations under the License.
takuo 0:4211e78bfa5d 15 */
takuo 0:4211e78bfa5d 16
takuo 0:4211e78bfa5d 17 #include "STTS751.h"
takuo 0:4211e78bfa5d 18
takuo 3:f9d3008f7e8f 19 #define SET_BITS(x,m,b) (((x) & ~(m)) | (b))
takuo 2:3116fe4a0079 20
takuo 3:f9d3008f7e8f 21 STTS751::STTS751(PinName sda, PinName scl, bool standby, Address addr, Model model) :
takuo 3:f9d3008f7e8f 22 _i2c(sda, scl), _addr(addr | model), _standby(standby) {
takuo 0:4211e78bfa5d 23 init();
takuo 0:4211e78bfa5d 24 }
takuo 0:4211e78bfa5d 25
takuo 3:f9d3008f7e8f 26 STTS751::STTS751(I2C &i2c, bool standby, Address addr, Model model) :
takuo 3:f9d3008f7e8f 27 _i2c(i2c), _addr(addr | model), _standby(standby) {
takuo 0:4211e78bfa5d 28 init();
takuo 0:4211e78bfa5d 29 }
takuo 0:4211e78bfa5d 30
takuo 3:f9d3008f7e8f 31 void STTS751::init() {
takuo 3:f9d3008f7e8f 32 char conf = 0x00;
takuo 3:f9d3008f7e8f 33 if (_standby)
takuo 3:f9d3008f7e8f 34 conf |= CONF_RUNSTOP;
takuo 3:f9d3008f7e8f 35 write8(REG_CONFIGURATION, conf);
takuo 3:f9d3008f7e8f 36 // conversion rate = 1/sec
takuo 2:3116fe4a0079 37 write8(REG_CONV_RATE, 0x04);
takuo 2:3116fe4a0079 38 }
takuo 2:3116fe4a0079 39
takuo 2:3116fe4a0079 40 int STTS751::addr() {
takuo 2:3116fe4a0079 41 return _addr;
takuo 2:3116fe4a0079 42 }
takuo 2:3116fe4a0079 43
takuo 2:3116fe4a0079 44 STTS751::Resolution STTS751::resolution() {
takuo 2:3116fe4a0079 45 char conf = read8(REG_CONFIGURATION);
takuo 3:f9d3008f7e8f 46 return (STTS751::Resolution) (conf & CONF_RES_MASK);
takuo 2:3116fe4a0079 47 }
takuo 2:3116fe4a0079 48
takuo 2:3116fe4a0079 49 void STTS751::setResolution(STTS751::Resolution res) {
takuo 2:3116fe4a0079 50 char conf = read8(REG_CONFIGURATION);
takuo 3:f9d3008f7e8f 51 conf = SET_BITS(conf, CONF_RES_MASK, res);
takuo 2:3116fe4a0079 52 write8(REG_CONFIGURATION, conf);
takuo 0:4211e78bfa5d 53 }
takuo 0:4211e78bfa5d 54
takuo 3:f9d3008f7e8f 55 int STTS751::conversionRate() {
takuo 3:f9d3008f7e8f 56 char conv = read8(REG_CONV_RATE);
takuo 3:f9d3008f7e8f 57 return conv & CONV_RATE_MASK;
takuo 3:f9d3008f7e8f 58 }
takuo 3:f9d3008f7e8f 59
takuo 3:f9d3008f7e8f 60 void STTS751::setConversionRate(int rate) {
takuo 3:f9d3008f7e8f 61 write8(REG_CONV_RATE, (char)(rate & CONV_RATE_MASK));
takuo 3:f9d3008f7e8f 62 }
takuo 3:f9d3008f7e8f 63
takuo 3:f9d3008f7e8f 64 void STTS751::setStandbyMode(bool standby) {
takuo 3:f9d3008f7e8f 65 _standby = standby;
takuo 3:f9d3008f7e8f 66 char conf = read8(REG_CONFIGURATION);
takuo 3:f9d3008f7e8f 67 if (_standby)
takuo 3:f9d3008f7e8f 68 conf |= CONF_RUNSTOP;
takuo 3:f9d3008f7e8f 69 else
takuo 3:f9d3008f7e8f 70 conf &= ~CONF_RUNSTOP;
takuo 3:f9d3008f7e8f 71 write8(REG_CONFIGURATION, conf);
takuo 3:f9d3008f7e8f 72 }
takuo 3:f9d3008f7e8f 73
takuo 3:f9d3008f7e8f 74 void STTS751::start() {
takuo 3:f9d3008f7e8f 75 if (ready())
takuo 3:f9d3008f7e8f 76 write8(REG_ONESHOT, 1);
takuo 3:f9d3008f7e8f 77 }
takuo 3:f9d3008f7e8f 78
takuo 3:f9d3008f7e8f 79 bool STTS751::ready() {
takuo 3:f9d3008f7e8f 80 char status = read8(REG_STATUS);
takuo 3:f9d3008f7e8f 81 return (status & STATUS_BUSY) == 0;
takuo 3:f9d3008f7e8f 82 }
takuo 3:f9d3008f7e8f 83
takuo 3:f9d3008f7e8f 84 float STTS751::temp(bool nowait) {
takuo 3:f9d3008f7e8f 85 if (_standby && !nowait) {
takuo 3:f9d3008f7e8f 86 start();
takuo 3:f9d3008f7e8f 87 while (!ready())
takuo 3:f9d3008f7e8f 88 wait(0.01);
takuo 3:f9d3008f7e8f 89 }
takuo 0:4211e78bfa5d 90 signed char h = read8(REG_TEMPERATURE_H);
takuo 0:4211e78bfa5d 91 unsigned char l = read8(REG_TEMPERATURE_L);
takuo 0:4211e78bfa5d 92 return ((h << 8) | l) / 256.0;
takuo 0:4211e78bfa5d 93 }
takuo 0:4211e78bfa5d 94
takuo 2:3116fe4a0079 95 #ifdef MBED_OPERATORS
takuo 3:f9d3008f7e8f 96 STTS751::operator float() {
takuo 0:4211e78bfa5d 97 return temp();
takuo 0:4211e78bfa5d 98 }
takuo 2:3116fe4a0079 99 #endif
takuo 0:4211e78bfa5d 100
takuo 0:4211e78bfa5d 101 char STTS751::read8(char reg) {
takuo 0:4211e78bfa5d 102 _i2c.write(_addr, &reg, 1, true);
takuo 0:4211e78bfa5d 103 _i2c.read(_addr, &reg, 1);
takuo 0:4211e78bfa5d 104 return reg;
takuo 0:4211e78bfa5d 105 }
takuo 2:3116fe4a0079 106
takuo 2:3116fe4a0079 107 void STTS751::write8(char reg, char data) {
takuo 2:3116fe4a0079 108 char buff[2] = { reg, data };
takuo 2:3116fe4a0079 109 _i2c.write(_addr, buff, 2);
takuo 2:3116fe4a0079 110 }