A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

Committer:
takuo
Date:
Sun Jan 19 03:13:38 2014 +0000
Revision:
2:3116fe4a0079
Parent:
1:b0a3645a3dba
Child:
3:f9d3008f7e8f
The device initialization method is now available.; Now we can get/set the temperature resolution.

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 2:3116fe4a0079 19 #define SET_BITS(x,m,b) (((x) & !(m)) | (b))
takuo 2:3116fe4a0079 20
takuo 0:4211e78bfa5d 21 STTS751::STTS751(PinName sda, PinName scl, Address addr, Model model): _i2c(sda, scl), _addr(addr | model)
takuo 0:4211e78bfa5d 22 {
takuo 0:4211e78bfa5d 23 init();
takuo 0:4211e78bfa5d 24 }
takuo 0:4211e78bfa5d 25
takuo 0:4211e78bfa5d 26 STTS751::STTS751(I2C &i2c, Address addr, Model model): _i2c(i2c), _addr(addr | model)
takuo 0:4211e78bfa5d 27 {
takuo 0:4211e78bfa5d 28 init();
takuo 0:4211e78bfa5d 29 }
takuo 0:4211e78bfa5d 30
takuo 0:4211e78bfa5d 31 void STTS751::init()
takuo 0:4211e78bfa5d 32 {
takuo 2:3116fe4a0079 33 write8(REG_CONFIGURATION, 0x00);
takuo 2:3116fe4a0079 34 write8(REG_CONV_RATE, 0x04);
takuo 2:3116fe4a0079 35 }
takuo 2:3116fe4a0079 36
takuo 2:3116fe4a0079 37 int STTS751::addr() {
takuo 2:3116fe4a0079 38 return _addr;
takuo 2:3116fe4a0079 39 }
takuo 2:3116fe4a0079 40
takuo 2:3116fe4a0079 41 STTS751::Resolution STTS751::resolution() {
takuo 2:3116fe4a0079 42 char conf = read8(REG_CONFIGURATION);
takuo 2:3116fe4a0079 43 return (STTS751::Resolution)(conf & RES_MASK);
takuo 2:3116fe4a0079 44 }
takuo 2:3116fe4a0079 45
takuo 2:3116fe4a0079 46 void STTS751::setResolution(STTS751::Resolution res) {
takuo 2:3116fe4a0079 47 char conf = read8(REG_CONFIGURATION);
takuo 2:3116fe4a0079 48 conf = SET_BITS(conf, RES_MASK, res);
takuo 2:3116fe4a0079 49 write8(REG_CONFIGURATION, conf);
takuo 0:4211e78bfa5d 50 }
takuo 0:4211e78bfa5d 51
takuo 0:4211e78bfa5d 52 float STTS751::temp()
takuo 0:4211e78bfa5d 53 {
takuo 0:4211e78bfa5d 54 signed char h = read8(REG_TEMPERATURE_H);
takuo 0:4211e78bfa5d 55 unsigned char l = read8(REG_TEMPERATURE_L);
takuo 0:4211e78bfa5d 56 return ((h << 8) | l) / 256.0;
takuo 0:4211e78bfa5d 57 }
takuo 0:4211e78bfa5d 58
takuo 2:3116fe4a0079 59 #ifdef MBED_OPERATORS
takuo 0:4211e78bfa5d 60 STTS751::operator float()
takuo 0:4211e78bfa5d 61 {
takuo 0:4211e78bfa5d 62 return temp();
takuo 0:4211e78bfa5d 63 }
takuo 2:3116fe4a0079 64 #endif
takuo 0:4211e78bfa5d 65
takuo 0:4211e78bfa5d 66 char STTS751::read8(char reg) {
takuo 0:4211e78bfa5d 67 _i2c.write(_addr, &reg, 1, true);
takuo 0:4211e78bfa5d 68 _i2c.read(_addr, &reg, 1);
takuo 0:4211e78bfa5d 69 return reg;
takuo 0:4211e78bfa5d 70 }
takuo 2:3116fe4a0079 71
takuo 2:3116fe4a0079 72 void STTS751::write8(char reg, char data) {
takuo 2:3116fe4a0079 73 char buff[2] = { reg, data };
takuo 2:3116fe4a0079 74 _i2c.write(_addr, buff, 2);
takuo 2:3116fe4a0079 75 }