A library for STMicroelectronics STTS751 I2C temperature sensor
STTS751.h@3:f9d3008f7e8f, 2014-02-05 (annotated)
- 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?
User | Revision | Line number | New 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 | 3:f9d3008f7e8f | 23 | * |
takuo | 0:4211e78bfa5d | 24 | * http://www.st.com/web/catalog/sense_power/FM89/SC294/PF220116 |
takuo | 0:4211e78bfa5d | 25 | * |
takuo | 0:4211e78bfa5d | 26 | * Example: |
takuo | 0:4211e78bfa5d | 27 | * @code |
takuo | 0:4211e78bfa5d | 28 | * #include "mbed.h" |
takuo | 0:4211e78bfa5d | 29 | * #include "STTS751.h" |
takuo | 0:4211e78bfa5d | 30 | * |
takuo | 0:4211e78bfa5d | 31 | * // I2C pins: p9 = sda, p10 = scl |
takuo | 3:f9d3008f7e8f | 32 | * STTS751 sensor(p9, p10); |
takuo | 0:4211e78bfa5d | 33 | * |
takuo | 0:4211e78bfa5d | 34 | * // You can specify an I2C object instead. |
takuo | 0:4211e78bfa5d | 35 | * // I2C i2c(p2, p10); |
takuo | 3:f9d3008f7e8f | 36 | * // STTS751 sensor(i2c); |
takuo | 0:4211e78bfa5d | 37 | * |
takuo | 0:4211e78bfa5d | 38 | * int main() { |
takuo | 2:3116fe4a0079 | 39 | * // set the temperature resolution to 12bits |
takuo | 3:f9d3008f7e8f | 40 | * sensor.setResolution(STTS751::RES_12); |
takuo | 0:4211e78bfa5d | 41 | * while (true) { |
takuo | 3:f9d3008f7e8f | 42 | * printf("tmp: %.4f\n", (float)sensor); |
takuo | 0:4211e78bfa5d | 43 | * wait(1); |
takuo | 0:4211e78bfa5d | 44 | * } |
takuo | 0:4211e78bfa5d | 45 | * } |
takuo | 0:4211e78bfa5d | 46 | * @endcode |
takuo | 0:4211e78bfa5d | 47 | */ |
takuo | 3:f9d3008f7e8f | 48 | class STTS751 { |
takuo | 0:4211e78bfa5d | 49 | public: |
takuo | 0:4211e78bfa5d | 50 | /** Device models |
takuo | 0:4211e78bfa5d | 51 | */ |
takuo | 0:4211e78bfa5d | 52 | enum Model { |
takuo | 3:f9d3008f7e8f | 53 | MODEL_0 = 0x00, /**< Model 0 (default) */ |
takuo | 3:f9d3008f7e8f | 54 | MODEL_1 = 0x40 /**< Model 1 */ |
takuo | 0:4211e78bfa5d | 55 | }; |
takuo | 0:4211e78bfa5d | 56 | |
takuo | 3:f9d3008f7e8f | 57 | /** I2C slave address selectors |
takuo | 3:f9d3008f7e8f | 58 | * The I2C slave address of the device is determined by the pull up register for Addr/~Therm pin. |
takuo | 3:f9d3008f7e8f | 59 | * |
takuo | 0:4211e78bfa5d | 60 | */ |
takuo | 0:4211e78bfa5d | 61 | enum Address { |
takuo | 0:4211e78bfa5d | 62 | ADDRESS_0 = (0x48 << 1), /**< pull up resistor = 7.5K */ |
takuo | 0:4211e78bfa5d | 63 | ADDRESS_1 = (0x49 << 1), /**< pull up resistor = 12K */ |
takuo | 0:4211e78bfa5d | 64 | ADDRESS_2 = (0x38 << 1), /**< pull up resistor = 20K */ |
takuo | 0:4211e78bfa5d | 65 | ADDRESS_3 = (0x39 << 1) /**< pull up resistor = 33K or GND (default) */ |
takuo | 0:4211e78bfa5d | 66 | }; |
takuo | 0:4211e78bfa5d | 67 | |
takuo | 2:3116fe4a0079 | 68 | /** Temperature resolutions |
takuo | 2:3116fe4a0079 | 69 | */ |
takuo | 2:3116fe4a0079 | 70 | enum Resolution { |
takuo | 3:f9d3008f7e8f | 71 | RES_9 = 0x08, /**< 9 bits */ |
takuo | 2:3116fe4a0079 | 72 | RES_10 = 0x00, /**< 10 bits (default) */ |
takuo | 2:3116fe4a0079 | 73 | RES_11 = 0x04, /**< 11 bits */ |
takuo | 3:f9d3008f7e8f | 74 | RES_12 = 0x0c /**< 12 bits */ |
takuo | 2:3116fe4a0079 | 75 | }; |
takuo | 2:3116fe4a0079 | 76 | |
takuo | 0:4211e78bfa5d | 77 | /** Create an STTS751 object connected to the specified I2C pins. |
takuo | 0:4211e78bfa5d | 78 | * |
takuo | 3:f9d3008f7e8f | 79 | * @param sda I2C data pin |
takuo | 3:f9d3008f7e8f | 80 | * @param scl I2C clock pin |
takuo | 3:f9d3008f7e8f | 81 | * @param standby Standby mode (defaults to false) |
takuo | 3:f9d3008f7e8f | 82 | * @param addr I2C slave address (defaults to ADDRESS_3) |
takuo | 3:f9d3008f7e8f | 83 | * @param model Device model (defaults to MODEL_0) |
takuo | 0:4211e78bfa5d | 84 | */ |
takuo | 3:f9d3008f7e8f | 85 | STTS751(PinName sda, PinName scl, |
takuo | 3:f9d3008f7e8f | 86 | bool standby = false, Address addr = ADDRESS_3, Model model = MODEL_0); |
takuo | 3:f9d3008f7e8f | 87 | |
takuo | 0:4211e78bfa5d | 88 | |
takuo | 0:4211e78bfa5d | 89 | /** Create an STTS751 object connected to the specified I2C port. |
takuo | 0:4211e78bfa5d | 90 | * |
takuo | 3:f9d3008f7e8f | 91 | * @param i2c I2C port |
takuo | 3:f9d3008f7e8f | 92 | * @param standby Standby mode (defaults to false) |
takuo | 3:f9d3008f7e8f | 93 | * @param addr I2C slave address (defaults to ADDRESS_3) |
takuo | 3:f9d3008f7e8f | 94 | * @param model Device model (defaults to MODEL_0) |
takuo | 0:4211e78bfa5d | 95 | */ |
takuo | 3:f9d3008f7e8f | 96 | STTS751(I2C &_i2c, |
takuo | 3:f9d3008f7e8f | 97 | bool standby = false, Address addr = ADDRESS_3, Model model = MODEL_0); |
takuo | 0:4211e78bfa5d | 98 | |
takuo | 2:3116fe4a0079 | 99 | /** Initialize the device |
takuo | 2:3116fe4a0079 | 100 | */ |
takuo | 2:3116fe4a0079 | 101 | void init(); |
takuo | 2:3116fe4a0079 | 102 | |
takuo | 3:f9d3008f7e8f | 103 | /** The I2C slave address (8bit) of the device |
takuo | 3:f9d3008f7e8f | 104 | * |
takuo | 3:f9d3008f7e8f | 105 | * @returns The I2C slave address (8bit) of the device |
takuo | 2:3116fe4a0079 | 106 | */ |
takuo | 2:3116fe4a0079 | 107 | int addr(); |
takuo | 2:3116fe4a0079 | 108 | |
takuo | 2:3116fe4a0079 | 109 | /** Get the current temperature resolution |
takuo | 2:3116fe4a0079 | 110 | * |
takuo | 2:3116fe4a0079 | 111 | * @returns The current temperature resolution |
takuo | 2:3116fe4a0079 | 112 | */ |
takuo | 2:3116fe4a0079 | 113 | Resolution resolution(); |
takuo | 2:3116fe4a0079 | 114 | |
takuo | 2:3116fe4a0079 | 115 | /** Set the temperature resolution |
takuo | 2:3116fe4a0079 | 116 | * |
takuo | 2:3116fe4a0079 | 117 | * @param res Resolution (defaults to RES_10) |
takuo | 2:3116fe4a0079 | 118 | */ |
takuo | 2:3116fe4a0079 | 119 | void setResolution(Resolution res = RES_10); |
takuo | 2:3116fe4a0079 | 120 | |
takuo | 3:f9d3008f7e8f | 121 | /** Get the current conversion rate |
takuo | 3:f9d3008f7e8f | 122 | * |
takuo | 3:f9d3008f7e8f | 123 | * @returns The current conversion rate |
takuo | 3:f9d3008f7e8f | 124 | */ |
takuo | 3:f9d3008f7e8f | 125 | int conversionRate(); |
takuo | 3:f9d3008f7e8f | 126 | |
takuo | 3:f9d3008f7e8f | 127 | /** Set the conversion rate |
takuo | 3:f9d3008f7e8f | 128 | * Set the number of times the temperature value is updated each second. |
takuo | 3:f9d3008f7e8f | 129 | * The conversion rate (times/second) can be calculated by 2^(rate - 4). |
takuo | 3:f9d3008f7e8f | 130 | * Note that rate should be less than 8 if the temperature resolution is 12-bit and |
takuo | 3:f9d3008f7e8f | 131 | * should be less than 9 if the temperature resolution is greater than 10-bit |
takuo | 3:f9d3008f7e8f | 132 | * |
takuo | 3:f9d3008f7e8f | 133 | * @param rate Conversion rate (0 .. 9) |
takuo | 3:f9d3008f7e8f | 134 | */ |
takuo | 3:f9d3008f7e8f | 135 | void setConversionRate(int rate); |
takuo | 3:f9d3008f7e8f | 136 | |
takuo | 3:f9d3008f7e8f | 137 | /** Set the device mode |
takuo | 3:f9d3008f7e8f | 138 | * |
takuo | 3:f9d3008f7e8f | 139 | * @param standby true : standby mode, false : continuous conversion mode |
takuo | 3:f9d3008f7e8f | 140 | */ |
takuo | 3:f9d3008f7e8f | 141 | void setStandbyMode(bool standby); |
takuo | 3:f9d3008f7e8f | 142 | |
takuo | 3:f9d3008f7e8f | 143 | /** Start a temperature conversion (in standby mode) |
takuo | 3:f9d3008f7e8f | 144 | */ |
takuo | 3:f9d3008f7e8f | 145 | void start(); |
takuo | 3:f9d3008f7e8f | 146 | |
takuo | 3:f9d3008f7e8f | 147 | /** Checks that a temperature conversion has finished and the temperature value is available |
takuo | 3:f9d3008f7e8f | 148 | * |
takuo | 3:f9d3008f7e8f | 149 | * @returns true if a temperature conversion has finished and the temperature value is available |
takuo | 3:f9d3008f7e8f | 150 | */ |
takuo | 3:f9d3008f7e8f | 151 | bool ready(); |
takuo | 3:f9d3008f7e8f | 152 | |
takuo | 0:4211e78bfa5d | 153 | /** Obtain the current temperature measurement |
takuo | 0:4211e78bfa5d | 154 | * |
takuo | 3:f9d3008f7e8f | 155 | * @param nowait If true, read the temperature value without waiting for finishing a conversion (in standby mode) |
takuo | 3:f9d3008f7e8f | 156 | * @returns The current temperature measurement in Celsius. |
takuo | 0:4211e78bfa5d | 157 | */ |
takuo | 3:f9d3008f7e8f | 158 | float temp(bool nowait = false); |
takuo | 3:f9d3008f7e8f | 159 | |
takuo | 0:4211e78bfa5d | 160 | #ifdef MBED_OPERATORS |
takuo | 0:4211e78bfa5d | 161 | /** A shorthand for temp() |
takuo | 0:4211e78bfa5d | 162 | * |
takuo | 0:4211e78bfa5d | 163 | * @returns The current temperature measurement in Celsius. |
takuo | 0:4211e78bfa5d | 164 | */ |
takuo | 0:4211e78bfa5d | 165 | operator float(); |
takuo | 0:4211e78bfa5d | 166 | #endif |
takuo | 0:4211e78bfa5d | 167 | |
takuo | 0:4211e78bfa5d | 168 | protected: |
takuo | 3:f9d3008f7e8f | 169 | /** I2C registers |
takuo | 3:f9d3008f7e8f | 170 | */ |
takuo | 2:3116fe4a0079 | 171 | enum { |
takuo | 0:4211e78bfa5d | 172 | REG_TEMPERATURE_H = 0x00, |
takuo | 0:4211e78bfa5d | 173 | REG_STATUS = 0x01, |
takuo | 0:4211e78bfa5d | 174 | REG_TEMPERATURE_L = 0x02, |
takuo | 0:4211e78bfa5d | 175 | REG_CONFIGURATION = 0x03, |
takuo | 0:4211e78bfa5d | 176 | REG_CONV_RATE = 0x04, |
takuo | 0:4211e78bfa5d | 177 | REG_HIGH_LIMIT_H = 0x05, |
takuo | 0:4211e78bfa5d | 178 | REG_HIGH_LIMIT_L = 0x06, |
takuo | 0:4211e78bfa5d | 179 | REG_LOW_LIMIT_H = 0x07, |
takuo | 0:4211e78bfa5d | 180 | REG_LOW_LIMIT_L = 0x08, |
takuo | 0:4211e78bfa5d | 181 | REG_ONESHOT = 0x0f, |
takuo | 0:4211e78bfa5d | 182 | REG_THERM = 0x20, |
takuo | 0:4211e78bfa5d | 183 | REG_THERM_HYSTERESIS = 0x21, |
takuo | 0:4211e78bfa5d | 184 | REG_SMBUS_TIMEOUT = 0x22, |
takuo | 0:4211e78bfa5d | 185 | REG_PRODUCT_ID = 0xfd, |
takuo | 0:4211e78bfa5d | 186 | REG_MFG_ID = 0xfe, |
takuo | 2:3116fe4a0079 | 187 | REG_REVISION_ID = 0xff, |
takuo | 3:f9d3008f7e8f | 188 | }; |
takuo | 3:f9d3008f7e8f | 189 | |
takuo | 3:f9d3008f7e8f | 190 | /** Configuration register (default 0x00) |
takuo | 3:f9d3008f7e8f | 191 | * - b7 : MASK1 |
takuo | 3:f9d3008f7e8f | 192 | * - b6 : ~RUN/STOP (0 : continuous conversion mode, 1 : standby mode) |
takuo | 3:f9d3008f7e8f | 193 | * - b5 : 0 |
takuo | 3:f9d3008f7e8f | 194 | * - b4 : RFU |
takuo | 3:f9d3008f7e8f | 195 | * - b3 : Tres1 |
takuo | 3:f9d3008f7e8f | 196 | * - b2 : Tres0 |
takuo | 3:f9d3008f7e8f | 197 | * - b1-b0 : RFU |
takuo | 3:f9d3008f7e8f | 198 | */ |
takuo | 3:f9d3008f7e8f | 199 | enum { |
takuo | 3:f9d3008f7e8f | 200 | CONF_MASK1 = 0x80, |
takuo | 3:f9d3008f7e8f | 201 | CONF_RUNSTOP = 0x40, |
takuo | 3:f9d3008f7e8f | 202 | CONF_RES_MASK = 0x0c, |
takuo | 3:f9d3008f7e8f | 203 | }; |
takuo | 3:f9d3008f7e8f | 204 | |
takuo | 3:f9d3008f7e8f | 205 | /** Status Register (default undefined) |
takuo | 3:f9d3008f7e8f | 206 | * - b7 : Busy |
takuo | 3:f9d3008f7e8f | 207 | * - b6 : T_HIGH |
takuo | 3:f9d3008f7e8f | 208 | * - b5 : T_LOW |
takuo | 3:f9d3008f7e8f | 209 | * - b4-b1 : RFU |
takuo | 3:f9d3008f7e8f | 210 | * - b0 : THRM |
takuo | 3:f9d3008f7e8f | 211 | */ |
takuo | 3:f9d3008f7e8f | 212 | enum { |
takuo | 3:f9d3008f7e8f | 213 | STATUS_BUSY = 0x80, |
takuo | 3:f9d3008f7e8f | 214 | STATUS_THIGH = 0x40, |
takuo | 3:f9d3008f7e8f | 215 | STATUS_LOW = 0x20, |
takuo | 3:f9d3008f7e8f | 216 | STATUS_THURM = 0x01 |
takuo | 3:f9d3008f7e8f | 217 | }; |
takuo | 3:f9d3008f7e8f | 218 | |
takuo | 3:f9d3008f7e8f | 219 | /** Conversion Rate Register (default 4) |
takuo | 3:f9d3008f7e8f | 220 | * - b7-b4: 0 |
takuo | 3:f9d3008f7e8f | 221 | * - b3-b0: Conversion rate (0..9) |
takuo | 3:f9d3008f7e8f | 222 | */ |
takuo | 3:f9d3008f7e8f | 223 | enum { |
takuo | 3:f9d3008f7e8f | 224 | CONV_RATE_MASK = 0x0f |
takuo | 0:4211e78bfa5d | 225 | }; |
takuo | 0:4211e78bfa5d | 226 | |
takuo | 0:4211e78bfa5d | 227 | I2C _i2c; |
takuo | 0:4211e78bfa5d | 228 | const int _addr; |
takuo | 3:f9d3008f7e8f | 229 | bool _standby; |
takuo | 0:4211e78bfa5d | 230 | |
takuo | 0:4211e78bfa5d | 231 | char read8(char reg); |
takuo | 2:3116fe4a0079 | 232 | void write8(char reg, char data); |
takuo | 0:4211e78bfa5d | 233 | }; |
takuo | 0:4211e78bfa5d | 234 | |
takuo | 2:3116fe4a0079 | 235 | #endif |