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 #ifndef STTS751_H
takuo 0:4211e78bfa5d 18 #define STTS751_H
takuo 0:4211e78bfa5d 19
takuo 0:4211e78bfa5d 20 #include "mbed.h"
takuo 0:4211e78bfa5d 21
takuo 0:4211e78bfa5d 22 /** A library for STMicroelectronics STTS751 I2C temperature sensor
takuo 0:4211e78bfa5d 23 * http://www.st.com/web/catalog/sense_power/FM89/SC294/PF220116
takuo 0:4211e78bfa5d 24 *
takuo 0:4211e78bfa5d 25 * Example:
takuo 0:4211e78bfa5d 26 * @code
takuo 0:4211e78bfa5d 27 * #include "mbed.h"
takuo 0:4211e78bfa5d 28 * #include "STTS751.h"
takuo 0:4211e78bfa5d 29 *
takuo 0:4211e78bfa5d 30 * // I2C pins: p9 = sda, p10 = scl
takuo 0:4211e78bfa5d 31 * STTS751 temp(p9, p10);
takuo 0:4211e78bfa5d 32 *
takuo 0:4211e78bfa5d 33 * // You can specify an I2C object instead.
takuo 0:4211e78bfa5d 34 * // I2C i2c(p2, p10);
takuo 0:4211e78bfa5d 35 * // STTS751 temp(i2c);
takuo 0:4211e78bfa5d 36 *
takuo 0:4211e78bfa5d 37 * int main() {
takuo 2:3116fe4a0079 38 * // set the temperature resolution to 12bits
takuo 2:3116fe4a0079 39 * temp.setResolution(STTS751::RES_12);
takuo 0:4211e78bfa5d 40 * while (true) {
takuo 2:3116fe4a0079 41 * printf("tmp: %.4f\n", (float)temp);
takuo 0:4211e78bfa5d 42 * wait(1);
takuo 0:4211e78bfa5d 43 * }
takuo 0:4211e78bfa5d 44 * }
takuo 0:4211e78bfa5d 45 * @endcode
takuo 0:4211e78bfa5d 46 */
takuo 0:4211e78bfa5d 47 class STTS751
takuo 0:4211e78bfa5d 48 {
takuo 0:4211e78bfa5d 49 public:
takuo 0:4211e78bfa5d 50 /** Device models
takuo 0:4211e78bfa5d 51 */
takuo 0:4211e78bfa5d 52 enum Model {
takuo 0:4211e78bfa5d 53 MODEL_0 = 0x00, /**< Model 0 (default) */
takuo 0:4211e78bfa5d 54 MODEL_1 = 0x40 /**< Model 1 */
takuo 0:4211e78bfa5d 55 };
takuo 0:4211e78bfa5d 56
takuo 2:3116fe4a0079 57 /** I2C address selectors
takuo 0:4211e78bfa5d 58 */
takuo 0:4211e78bfa5d 59 enum Address {
takuo 0:4211e78bfa5d 60 ADDRESS_0 = (0x48 << 1), /**< pull up resistor = 7.5K */
takuo 0:4211e78bfa5d 61 ADDRESS_1 = (0x49 << 1), /**< pull up resistor = 12K */
takuo 0:4211e78bfa5d 62 ADDRESS_2 = (0x38 << 1), /**< pull up resistor = 20K */
takuo 0:4211e78bfa5d 63 ADDRESS_3 = (0x39 << 1) /**< pull up resistor = 33K or GND (default) */
takuo 0:4211e78bfa5d 64 };
takuo 0:4211e78bfa5d 65
takuo 2:3116fe4a0079 66 /** Temperature resolutions
takuo 2:3116fe4a0079 67 */
takuo 2:3116fe4a0079 68 enum Resolution {
takuo 2:3116fe4a0079 69 RES_9 = 0x08, /**< 9 bits */
takuo 2:3116fe4a0079 70 RES_10 = 0x00, /**< 10 bits (default) */
takuo 2:3116fe4a0079 71 RES_11 = 0x04, /**< 11 bits */
takuo 2:3116fe4a0079 72 RES_12 = 0x0c /**< 12 bits */
takuo 2:3116fe4a0079 73 };
takuo 2:3116fe4a0079 74
takuo 0:4211e78bfa5d 75 /** Create an STTS751 object connected to the specified I2C pins.
takuo 0:4211e78bfa5d 76 *
takuo 0:4211e78bfa5d 77 * @param sda I2C data pin
takuo 0:4211e78bfa5d 78 * @param scl I2C clock pin
takuo 0:4211e78bfa5d 79 * @param addr I2C slave address (defaults to ADDRESS_3)
takuo 0:4211e78bfa5d 80 * @param model Device model (defaults to MODEL_0)
takuo 0:4211e78bfa5d 81 */
takuo 0:4211e78bfa5d 82 STTS751(PinName sda, PinName scl, Address addr = ADDRESS_3, Model model = MODEL_0);
takuo 0:4211e78bfa5d 83
takuo 0:4211e78bfa5d 84 /** Create an STTS751 object connected to the specified I2C port.
takuo 0:4211e78bfa5d 85 *
takuo 0:4211e78bfa5d 86 * @param i2c I2C port
takuo 0:4211e78bfa5d 87 * @param addr I2C slave address (defaults to ADDRESS_3)
takuo 0:4211e78bfa5d 88 * @param model Device model (defaults to MODEL_0)
takuo 0:4211e78bfa5d 89 */
takuo 0:4211e78bfa5d 90 STTS751(I2C &_i2c, Address addr = ADDRESS_3, Model model = MODEL_0);
takuo 0:4211e78bfa5d 91
takuo 2:3116fe4a0079 92 /** Initialize the device
takuo 2:3116fe4a0079 93 */
takuo 2:3116fe4a0079 94 void init();
takuo 2:3116fe4a0079 95
takuo 2:3116fe4a0079 96 /** The I2C address (8bit address) of the device
takuo 2:3116fe4a0079 97 */
takuo 2:3116fe4a0079 98 int addr();
takuo 2:3116fe4a0079 99
takuo 2:3116fe4a0079 100 /** Get the current temperature resolution
takuo 2:3116fe4a0079 101 *
takuo 2:3116fe4a0079 102 * @returns The current temperature resolution
takuo 2:3116fe4a0079 103 */
takuo 2:3116fe4a0079 104 Resolution resolution();
takuo 2:3116fe4a0079 105
takuo 2:3116fe4a0079 106 /** Set the temperature resolution
takuo 2:3116fe4a0079 107 *
takuo 2:3116fe4a0079 108 * @param res Resolution (defaults to RES_10)
takuo 2:3116fe4a0079 109 */
takuo 2:3116fe4a0079 110 void setResolution(Resolution res = RES_10);
takuo 2:3116fe4a0079 111
takuo 0:4211e78bfa5d 112 /** Obtain the current temperature measurement
takuo 0:4211e78bfa5d 113 *
takuo 0:4211e78bfa5d 114 * @returns The current temperature measurement in Celsius.
takuo 0:4211e78bfa5d 115 */
takuo 0:4211e78bfa5d 116 float temp();
takuo 0:4211e78bfa5d 117
takuo 0:4211e78bfa5d 118 #ifdef MBED_OPERATORS
takuo 0:4211e78bfa5d 119 /** A shorthand for temp()
takuo 0:4211e78bfa5d 120 *
takuo 0:4211e78bfa5d 121 * @returns The current temperature measurement in Celsius.
takuo 0:4211e78bfa5d 122 */
takuo 0:4211e78bfa5d 123 operator float();
takuo 0:4211e78bfa5d 124 #endif
takuo 0:4211e78bfa5d 125
takuo 0:4211e78bfa5d 126 protected:
takuo 2:3116fe4a0079 127 enum {
takuo 0:4211e78bfa5d 128 REG_TEMPERATURE_H = 0x00,
takuo 0:4211e78bfa5d 129 REG_STATUS = 0x01,
takuo 0:4211e78bfa5d 130 REG_TEMPERATURE_L = 0x02,
takuo 0:4211e78bfa5d 131 REG_CONFIGURATION = 0x03,
takuo 0:4211e78bfa5d 132 REG_CONV_RATE = 0x04,
takuo 0:4211e78bfa5d 133 REG_HIGH_LIMIT_H = 0x05,
takuo 0:4211e78bfa5d 134 REG_HIGH_LIMIT_L = 0x06,
takuo 0:4211e78bfa5d 135 REG_LOW_LIMIT_H = 0x07,
takuo 0:4211e78bfa5d 136 REG_LOW_LIMIT_L = 0x08,
takuo 0:4211e78bfa5d 137 REG_ONESHOT = 0x0f,
takuo 0:4211e78bfa5d 138 REG_THERM = 0x20,
takuo 0:4211e78bfa5d 139 REG_THERM_HYSTERESIS = 0x21,
takuo 0:4211e78bfa5d 140 REG_SMBUS_TIMEOUT = 0x22,
takuo 0:4211e78bfa5d 141 REG_PRODUCT_ID = 0xfd,
takuo 0:4211e78bfa5d 142 REG_MFG_ID = 0xfe,
takuo 2:3116fe4a0079 143 REG_REVISION_ID = 0xff,
takuo 2:3116fe4a0079 144 RES_MASK = 0x0c
takuo 0:4211e78bfa5d 145 };
takuo 0:4211e78bfa5d 146
takuo 0:4211e78bfa5d 147 I2C _i2c;
takuo 0:4211e78bfa5d 148 const int _addr;
takuo 0:4211e78bfa5d 149
takuo 0:4211e78bfa5d 150 char read8(char reg);
takuo 2:3116fe4a0079 151 void write8(char reg, char data);
takuo 0:4211e78bfa5d 152 };
takuo 0:4211e78bfa5d 153
takuo 2:3116fe4a0079 154 #endif