Library for Silicon Labs Si7006 digital temperature and humidity chip.

Dependents:   acd52832_Humidity_Temp_Example iBeacon acnsensa acnSENSA

Committer:
jurica238814
Date:
Mon Jul 10 17:14:44 2017 +0000
Revision:
0:cc2c5d49fe63
Child:
1:155934570c14
Humidity works. Temperature todo (maybe just check).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:cc2c5d49fe63 1 /*******************************************************************************
jurica238814 0:cc2c5d49fe63 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
jurica238814 0:cc2c5d49fe63 3 *
jurica238814 0:cc2c5d49fe63 4 * Permission is hereby granted, free of charge, to any person obtaining a
jurica238814 0:cc2c5d49fe63 5 * copy of this software and associated documentation files (the "Software"),
jurica238814 0:cc2c5d49fe63 6 * to deal in the Software without restriction, including without limitation
jurica238814 0:cc2c5d49fe63 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
jurica238814 0:cc2c5d49fe63 8 * and/or sell copies of the Software, and to permit persons to whom the
jurica238814 0:cc2c5d49fe63 9 * Software is furnished to do so, subject to the following conditions:
jurica238814 0:cc2c5d49fe63 10 *
jurica238814 0:cc2c5d49fe63 11 * The above copyright notice and this permission notice shall be included
jurica238814 0:cc2c5d49fe63 12 * in all copies or substantial portions of the Software.
jurica238814 0:cc2c5d49fe63 13 *
jurica238814 0:cc2c5d49fe63 14 *
jurica238814 0:cc2c5d49fe63 15 * The library is modified to suite Si7006A20 Humidity and Temperature Sesor
jurica238814 0:cc2c5d49fe63 16 * Made by Jurica Resetar @ aconno
jurica238814 0:cc2c5d49fe63 17 * More info and contact: aconno.de
jurica238814 0:cc2c5d49fe63 18 *
jurica238814 0:cc2c5d49fe63 19 *******************************************************************************
jurica238814 0:cc2c5d49fe63 20 */
jurica238814 0:cc2c5d49fe63 21
jurica238814 0:cc2c5d49fe63 22 #ifndef _Si70006A20_H_
jurica238814 0:cc2c5d49fe63 23 #define _Si70006A20_H_
jurica238814 0:cc2c5d49fe63 24
jurica238814 0:cc2c5d49fe63 25 #include "mbed.h"
jurica238814 0:cc2c5d49fe63 26
jurica238814 0:cc2c5d49fe63 27 /**
jurica238814 0:cc2c5d49fe63 28 * Silicon Labs Si7006 Humidity and Temperature Sensor
jurica238814 0:cc2c5d49fe63 29 *
jurica238814 0:cc2c5d49fe63 30 * @code
jurica238814 0:cc2c5d49fe63 31 * #include <stdio.h>
jurica238814 0:cc2c5d49fe63 32 * #include "mbed.h"
jurica238814 0:cc2c5d49fe63 33 * #include "Si7006A20.h"
jurica238814 0:cc2c5d49fe63 34 *
jurica238814 0:cc2c5d49fe63 35 * I2C i2c(I2C_SDA, I2C_SCL);
jurica238814 0:cc2c5d49fe63 36 * Si7006 si(&i2c);
jurica238814 0:cc2c5d49fe63 37 *
jurica238814 0:cc2c5d49fe63 38 * int main()
jurica238814 0:cc2c5d49fe63 39 * {
jurica238814 0:cc2c5d49fe63 40 * while(1) {
jurica238814 0:cc2c5d49fe63 41 *
jurica238814 0:cc2c5d49fe63 42 * float humid;
jurica238814 0:cc2c5d49fe63 43 * if(si.getHumidity(&humid) != 0) {
jurica238814 0:cc2c5d49fe63 44 * printf("Error getting humidity\n");
jurica238814 0:cc2c5d49fe63 45 * humid = -1;
jurica238814 0:cc2c5d49fe63 46 * }
jurica238814 0:cc2c5d49fe63 47 *
jurica238814 0:cc2c5d49fe63 48 * float temp;
jurica238814 0:cc2c5d49fe63 49 * if(si.getTemperature(&temp) != 0) {
jurica238814 0:cc2c5d49fe63 50 * printf("Error getting temperature");
jurica238814 0:cc2c5d49fe63 51 * temp = -1;
jurica238814 0:cc2c5d49fe63 52 * }
jurica238814 0:cc2c5d49fe63 53 * printf("Humidity = %f%% Temperature = %fC\n", humid, temp);
jurica238814 0:cc2c5d49fe63 54 *
jurica238814 0:cc2c5d49fe63 55 * wait(1);
jurica238814 0:cc2c5d49fe63 56 * }
jurica238814 0:cc2c5d49fe63 57 * }
jurica238814 0:cc2c5d49fe63 58 * @endcode
jurica238814 0:cc2c5d49fe63 59 */
jurica238814 0:cc2c5d49fe63 60 class Si7006{
jurica238814 0:cc2c5d49fe63 61 public:
jurica238814 0:cc2c5d49fe63 62
jurica238814 0:cc2c5d49fe63 63 /**
jurica238814 0:cc2c5d49fe63 64 * @brief Measurement resolution.
jurica238814 0:cc2c5d49fe63 65 * @details Controls the resolution of the humidity and temperarure readings.
jurica238814 0:cc2c5d49fe63 66 */
jurica238814 0:cc2c5d49fe63 67 typedef enum {
jurica238814 0:cc2c5d49fe63 68 RH_12b_TEMP_14b = 0x0, ///< 12 bits for RH, 14 bits for Temp
jurica238814 0:cc2c5d49fe63 69 RH_8b_TEMP_12b = 0x1, ///< 8 bits for RH, 12 bits for Temp
jurica238814 0:cc2c5d49fe63 70 RH_10b_TEMP_13b = 0x2, ///< 10 bits for RH, 13 bits for Temp
jurica238814 0:cc2c5d49fe63 71 RH_11b_TEMP_11b = 0x3, ///< 11 bits for RH, 11 bits for Temp
jurica238814 0:cc2c5d49fe63 72 } resolution_t;
jurica238814 0:cc2c5d49fe63 73
jurica238814 0:cc2c5d49fe63 74 /**
jurica238814 0:cc2c5d49fe63 75 * Si7006 constructor.
jurica238814 0:cc2c5d49fe63 76 *
jurica238814 0:cc2c5d49fe63 77 * @param sda mbed pin to use for SDA line of I2C interface.
jurica238814 0:cc2c5d49fe63 78 * @param scl mbed pin to use for SCL line of I2C interface.
jurica238814 0:cc2c5d49fe63 79 */
jurica238814 0:cc2c5d49fe63 80 Si7006(PinName sda, PinName scl);
jurica238814 0:cc2c5d49fe63 81
jurica238814 0:cc2c5d49fe63 82 /**
jurica238814 0:cc2c5d49fe63 83 * Si7006 constructor.
jurica238814 0:cc2c5d49fe63 84 *
jurica238814 0:cc2c5d49fe63 85 * @param i2c I2C object to use
jurica238814 0:cc2c5d49fe63 86 */
jurica238814 0:cc2c5d49fe63 87 Si7006(I2C *i2c);
jurica238814 0:cc2c5d49fe63 88
jurica238814 0:cc2c5d49fe63 89 /**
jurica238814 0:cc2c5d49fe63 90 * Si7006 destructor.
jurica238814 0:cc2c5d49fe63 91 */
jurica238814 0:cc2c5d49fe63 92 ~Si7006();
jurica238814 0:cc2c5d49fe63 93
jurica238814 0:cc2c5d49fe63 94 /**
jurica238814 0:cc2c5d49fe63 95 * @brief Reset.
jurica238814 0:cc2c5d49fe63 96 * @details Sends the rest command.
jurica238814 0:cc2c5d49fe63 97 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 98 */
jurica238814 0:cc2c5d49fe63 99 int reset(void);
jurica238814 0:cc2c5d49fe63 100
jurica238814 0:cc2c5d49fe63 101 /**
jurica238814 0:cc2c5d49fe63 102 * @brief Get Electronic ID.
jurica238814 0:cc2c5d49fe63 103 * @details Gets the Electronic ID of the connected device. Verifies the
jurica238814 0:cc2c5d49fe63 104 * ID with an 8-bit CRC.
jurica238814 0:cc2c5d49fe63 105 *
jurica238814 0:cc2c5d49fe63 106 * @param Character buffer to store the id. Needs to be at least 8 bytes.
jurica238814 0:cc2c5d49fe63 107 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 108 */
jurica238814 0:cc2c5d49fe63 109 int getElectronicId(char *id);
jurica238814 0:cc2c5d49fe63 110
jurica238814 0:cc2c5d49fe63 111 /**
jurica238814 0:cc2c5d49fe63 112 * @brief Configure sample resolution.
jurica238814 0:cc2c5d49fe63 113 * @details Sets the number of bits used for humidity and temperature readings.
jurica238814 0:cc2c5d49fe63 114 * @param resolution Enum for the resolution setting.
jurica238814 0:cc2c5d49fe63 115 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 116 */
jurica238814 0:cc2c5d49fe63 117 int configResolution(Si7006::resolution_t resolution);
jurica238814 0:cc2c5d49fe63 118
jurica238814 0:cc2c5d49fe63 119 /**
jurica238814 0:cc2c5d49fe63 120 * @brief Get temperature reading.
jurica238814 0:cc2c5d49fe63 121 * @details Initiates a temperature reading and blocks until reading has
jurica238814 0:cc2c5d49fe63 122 * been calculated.
jurica238814 0:cc2c5d49fe63 123 *
jurica238814 0:cc2c5d49fe63 124 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
jurica238814 0:cc2c5d49fe63 125 * for timing specifications.
jurica238814 0:cc2c5d49fe63 126 *
jurica238814 0:cc2c5d49fe63 127 * @param tempCx10 Pointer for temperature reading. Unit is 1/10th degree Celcius.
jurica238814 0:cc2c5d49fe63 128 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 129 */
jurica238814 0:cc2c5d49fe63 130 int getTemperature(int16_t *tempCx10);
jurica238814 0:cc2c5d49fe63 131
jurica238814 0:cc2c5d49fe63 132 /**
jurica238814 0:cc2c5d49fe63 133 * @brief Get temperature reading.
jurica238814 0:cc2c5d49fe63 134 * @details Initiates a temperature reading and blocks until reading has
jurica238814 0:cc2c5d49fe63 135 * been calculated.
jurica238814 0:cc2c5d49fe63 136 *
jurica238814 0:cc2c5d49fe63 137 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
jurica238814 0:cc2c5d49fe63 138 * for timing specifications.
jurica238814 0:cc2c5d49fe63 139 *
jurica238814 0:cc2c5d49fe63 140 * @param tempC Pointer for temperature reading. Unit is degree Celcius.
jurica238814 0:cc2c5d49fe63 141 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 142 */
jurica238814 0:cc2c5d49fe63 143 int getTemperature(float *tempC);
jurica238814 0:cc2c5d49fe63 144
jurica238814 0:cc2c5d49fe63 145 /**
jurica238814 0:cc2c5d49fe63 146 * @brief Start temperature reading.
jurica238814 0:cc2c5d49fe63 147 * @details Initiates a temperature reading. Will not hold the bus or block.
jurica238814 0:cc2c5d49fe63 148 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 149 */
jurica238814 0:cc2c5d49fe63 150 int startTemperature(void);
jurica238814 0:cc2c5d49fe63 151
jurica238814 0:cc2c5d49fe63 152 /**
jurica238814 0:cc2c5d49fe63 153 * @brief Check temperature reading.
jurica238814 0:cc2c5d49fe63 154 * @details Checks to see if the temperature reading has been completed.
jurica238814 0:cc2c5d49fe63 155 Returns temperature if reading complete.
jurica238814 0:cc2c5d49fe63 156 * @note Must call startTemperature() prior to calling this function.
jurica238814 0:cc2c5d49fe63 157 * @param tempCx10 Pointer for temperature reading. Unit is 1/10th degree Celcius.
jurica238814 0:cc2c5d49fe63 158 * @returns 0 if reading taken, -1 if reading pending.
jurica238814 0:cc2c5d49fe63 159 */
jurica238814 0:cc2c5d49fe63 160 int checkTemperature(int16_t *tempCx10);
jurica238814 0:cc2c5d49fe63 161
jurica238814 0:cc2c5d49fe63 162 /**
jurica238814 0:cc2c5d49fe63 163 * @brief Check temperature reading.
jurica238814 0:cc2c5d49fe63 164 * @details Checks to see if the temperature reading has been completed.
jurica238814 0:cc2c5d49fe63 165 Returns temperature if reading complete.
jurica238814 0:cc2c5d49fe63 166 * @note Must call startTemperature() prior to calling this function.
jurica238814 0:cc2c5d49fe63 167 * @param tempC Pointer for temperature reading. Unit is degree Celcius.
jurica238814 0:cc2c5d49fe63 168 * @returns 0 if reading taken, -1 if reading pending.
jurica238814 0:cc2c5d49fe63 169 */
jurica238814 0:cc2c5d49fe63 170 int checkTemperature(float *tempC);
jurica238814 0:cc2c5d49fe63 171
jurica238814 0:cc2c5d49fe63 172 /**
jurica238814 0:cc2c5d49fe63 173 * @brief Get humidity reading.
jurica238814 0:cc2c5d49fe63 174 * @details Initiates a humidity reading and blocks until reading has
jurica238814 0:cc2c5d49fe63 175 * been calculated.
jurica238814 0:cc2c5d49fe63 176 *
jurica238814 0:cc2c5d49fe63 177 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
jurica238814 0:cc2c5d49fe63 178 * for timing specifications.
jurica238814 0:cc2c5d49fe63 179 *
jurica238814 0:cc2c5d49fe63 180 * @param humidx10 Pointer for humidity reading. Unit is 1/10th percent.
jurica238814 0:cc2c5d49fe63 181 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 182 */
jurica238814 0:cc2c5d49fe63 183 int getHumidity(int16_t *humidx10);
jurica238814 0:cc2c5d49fe63 184
jurica238814 0:cc2c5d49fe63 185 /**
jurica238814 0:cc2c5d49fe63 186 * @brief Get humidity reading.
jurica238814 0:cc2c5d49fe63 187 * @details Initiates a humidity reading and blocks until reading has
jurica238814 0:cc2c5d49fe63 188 * been calculated.
jurica238814 0:cc2c5d49fe63 189 *
jurica238814 0:cc2c5d49fe63 190 * @note Will hold the I2C bus until reading is complete. Refer to datasheet
jurica238814 0:cc2c5d49fe63 191 * for timing specifications.
jurica238814 0:cc2c5d49fe63 192 *
jurica238814 0:cc2c5d49fe63 193 * @param humid Pointer for humidity reading. Unit is percent.
jurica238814 0:cc2c5d49fe63 194 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 195 */
jurica238814 0:cc2c5d49fe63 196 int getHumidity(float *humid);
jurica238814 0:cc2c5d49fe63 197
jurica238814 0:cc2c5d49fe63 198 /**
jurica238814 0:cc2c5d49fe63 199 * @brief Start humidity reading.
jurica238814 0:cc2c5d49fe63 200 * @details Initiates a humidity reading. Will not hold the bus or block.
jurica238814 0:cc2c5d49fe63 201 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 202 */
jurica238814 0:cc2c5d49fe63 203 int startHumidity(void);
jurica238814 0:cc2c5d49fe63 204
jurica238814 0:cc2c5d49fe63 205 /**
jurica238814 0:cc2c5d49fe63 206 * @brief Check humidity reading.
jurica238814 0:cc2c5d49fe63 207 * @details Checks to see if the humidity reading has been completed.
jurica238814 0:cc2c5d49fe63 208 Returns humidity if reading complete.
jurica238814 0:cc2c5d49fe63 209
jurica238814 0:cc2c5d49fe63 210 * @note Must call startHumidity() prior to calling this function.
jurica238814 0:cc2c5d49fe63 211 * @param humidCx10 Pointer for humidity reading. Unit is 1/10th percent.
jurica238814 0:cc2c5d49fe63 212 * @returns 0 if reading taken, -1 if reading pending.
jurica238814 0:cc2c5d49fe63 213 */
jurica238814 0:cc2c5d49fe63 214 int checkHumidity(int16_t *humidx10);
jurica238814 0:cc2c5d49fe63 215
jurica238814 0:cc2c5d49fe63 216 /**
jurica238814 0:cc2c5d49fe63 217 * @brief Check humidity reading.
jurica238814 0:cc2c5d49fe63 218 * @details Checks to see if the humidity reading has been completed.
jurica238814 0:cc2c5d49fe63 219 Returns humidity if reading complete.
jurica238814 0:cc2c5d49fe63 220
jurica238814 0:cc2c5d49fe63 221 * @note Must call startHumidity() prior to calling this function.
jurica238814 0:cc2c5d49fe63 222 * @param humid Pointer for humidity reading. Unit is percent.
jurica238814 0:cc2c5d49fe63 223 * @returns 0 if reading taken, -1 if reading pending.
jurica238814 0:cc2c5d49fe63 224 */
jurica238814 0:cc2c5d49fe63 225 int checkHumidity(float *humid);
jurica238814 0:cc2c5d49fe63 226
jurica238814 0:cc2c5d49fe63 227 /**
jurica238814 0:cc2c5d49fe63 228 * @brief Get temperature from humidity reading.
jurica238814 0:cc2c5d49fe63 229 * @details Gets temperature reading from previous humidity reading.
jurica238814 0:cc2c5d49fe63 230 * @note Must call startHumidity() prior to calling this function.
jurica238814 0:cc2c5d49fe63 231 * @param tempC Pointer for temperature reading. Unit is degree Celcius.
jurica238814 0:cc2c5d49fe63 232 * @returns 0 if reading taken, -1 if reading pending.
jurica238814 0:cc2c5d49fe63 233 */
jurica238814 0:cc2c5d49fe63 234 int getPrevTemperature(float* tempC);
jurica238814 0:cc2c5d49fe63 235
jurica238814 0:cc2c5d49fe63 236 /**
jurica238814 0:cc2c5d49fe63 237 * @brief Get temperature from humidity reading.
jurica238814 0:cc2c5d49fe63 238 * @details Gets temperature reading from previous humidity reading.
jurica238814 0:cc2c5d49fe63 239 * @note Must call startHumidity() prior to calling this function.
jurica238814 0:cc2c5d49fe63 240 * @param tempCx10 Pointer for temperature reading. Unit is 1/10th degree Celcius.
jurica238814 0:cc2c5d49fe63 241 * @returns 0 if reading taken, -1 if reading pending.
jurica238814 0:cc2c5d49fe63 242 */
jurica238814 0:cc2c5d49fe63 243 int getPrevTemperature(int16_t *tempCx10);
jurica238814 0:cc2c5d49fe63 244
jurica238814 0:cc2c5d49fe63 245 /**
jurica238814 0:cc2c5d49fe63 246 * @brief Get firmware revision.
jurica238814 0:cc2c5d49fe63 247 * @details Reads the firmware revision, refer to datasheet for codes.
jurica238814 0:cc2c5d49fe63 248 * @param rev Pointer to store firmware revision.
jurica238814 0:cc2c5d49fe63 249 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 250 */
jurica238814 0:cc2c5d49fe63 251 int getRev(char *rev);
jurica238814 0:cc2c5d49fe63 252
jurica238814 0:cc2c5d49fe63 253 /**
jurica238814 0:cc2c5d49fe63 254 * @brief Control heater.
jurica238814 0:cc2c5d49fe63 255 * @details Enable or disable the heater.
jurica238814 0:cc2c5d49fe63 256 * @param enable True to enable heater, false to disable.
jurica238814 0:cc2c5d49fe63 257 * @returns 0 if no errors, -1 if error.
jurica238814 0:cc2c5d49fe63 258 */
jurica238814 0:cc2c5d49fe63 259 int heater(bool enable);
jurica238814 0:cc2c5d49fe63 260
jurica238814 0:cc2c5d49fe63 261 private:
jurica238814 0:cc2c5d49fe63 262
jurica238814 0:cc2c5d49fe63 263 char crc8(char value, char seed);
jurica238814 0:cc2c5d49fe63 264 I2C *i2c_;
jurica238814 0:cc2c5d49fe63 265 bool i2c_owner;
jurica238814 0:cc2c5d49fe63 266 };
jurica238814 0:cc2c5d49fe63 267
jurica238814 0:cc2c5d49fe63 268 #endif /* _Si7006_H_ */