Interface to a Silicon Labs Si7005 relative humidity sensor.
Si7005.h@0:6fbf67893ca2, 2013-04-10 (annotated)
- Committer:
- LievenHollevoet
- Date:
- Wed Apr 10 08:43:56 2013 +0000
- Revision:
- 0:6fbf67893ca2
First working version of a library to interface to a Silicon Labs Si7005 relative humidity sensor.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LievenHollevoet | 0:6fbf67893ca2 | 1 | /* mbed Si7005 relative humidity sensor Library |
LievenHollevoet | 0:6fbf67893ca2 | 2 | * |
LievenHollevoet | 0:6fbf67893ca2 | 3 | * Copyright (c) 2013, Lieven Hollevoet (http://likatronix.be) |
LievenHollevoet | 0:6fbf67893ca2 | 4 | * |
LievenHollevoet | 0:6fbf67893ca2 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
LievenHollevoet | 0:6fbf67893ca2 | 6 | * of this software and associated documentation files (the "Software"), to deal |
LievenHollevoet | 0:6fbf67893ca2 | 7 | * in the Software without restriction, including without limitation the rights |
LievenHollevoet | 0:6fbf67893ca2 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
LievenHollevoet | 0:6fbf67893ca2 | 9 | * copies of the Software, and to permit persons to whom the Software is |
LievenHollevoet | 0:6fbf67893ca2 | 10 | * furnished to do so, subject to the following conditions: |
LievenHollevoet | 0:6fbf67893ca2 | 11 | * |
LievenHollevoet | 0:6fbf67893ca2 | 12 | * The above copyright notice and this permission notice shall be included in |
LievenHollevoet | 0:6fbf67893ca2 | 13 | * all copies or substantial portions of the Software. |
LievenHollevoet | 0:6fbf67893ca2 | 14 | * |
LievenHollevoet | 0:6fbf67893ca2 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
LievenHollevoet | 0:6fbf67893ca2 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
LievenHollevoet | 0:6fbf67893ca2 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
LievenHollevoet | 0:6fbf67893ca2 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
LievenHollevoet | 0:6fbf67893ca2 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
LievenHollevoet | 0:6fbf67893ca2 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
LievenHollevoet | 0:6fbf67893ca2 | 21 | * THE SOFTWARE. |
LievenHollevoet | 0:6fbf67893ca2 | 22 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 23 | |
LievenHollevoet | 0:6fbf67893ca2 | 24 | #ifndef SI7005_H |
LievenHollevoet | 0:6fbf67893ca2 | 25 | #define SI7005_H |
LievenHollevoet | 0:6fbf67893ca2 | 26 | |
LievenHollevoet | 0:6fbf67893ca2 | 27 | #include "mbed.h" |
LievenHollevoet | 0:6fbf67893ca2 | 28 | |
LievenHollevoet | 0:6fbf67893ca2 | 29 | /* |
LievenHollevoet | 0:6fbf67893ca2 | 30 | * register names |
LievenHollevoet | 0:6fbf67893ca2 | 31 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 32 | |
LievenHollevoet | 0:6fbf67893ca2 | 33 | #define SI7005_REG_STATUS 0 |
LievenHollevoet | 0:6fbf67893ca2 | 34 | #define SI7005_REG_DATAH 1 |
LievenHollevoet | 0:6fbf67893ca2 | 35 | #define SI7005_REG_DATAL 2 |
LievenHollevoet | 0:6fbf67893ca2 | 36 | #define SI7005_REG_CONFIG 3 |
LievenHollevoet | 0:6fbf67893ca2 | 37 | #define SI7005_REG_ID 17 |
LievenHollevoet | 0:6fbf67893ca2 | 38 | |
LievenHollevoet | 0:6fbf67893ca2 | 39 | /* |
LievenHollevoet | 0:6fbf67893ca2 | 40 | * measurement types |
LievenHollevoet | 0:6fbf67893ca2 | 41 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 42 | |
LievenHollevoet | 0:6fbf67893ca2 | 43 | #define SI7005_RH 0 |
LievenHollevoet | 0:6fbf67893ca2 | 44 | #define SI7005_T 1 |
LievenHollevoet | 0:6fbf67893ca2 | 45 | |
LievenHollevoet | 0:6fbf67893ca2 | 46 | /** Class to interface to a Silicon Labs Si7005 relative humidity sensor. |
LievenHollevoet | 0:6fbf67893ca2 | 47 | * |
LievenHollevoet | 0:6fbf67893ca2 | 48 | * Example: |
LievenHollevoet | 0:6fbf67893ca2 | 49 | * @code |
LievenHollevoet | 0:6fbf67893ca2 | 50 | * #include "mbed.h" |
LievenHollevoet | 0:6fbf67893ca2 | 51 | * #include "Si7005.h" |
LievenHollevoet | 0:6fbf67893ca2 | 52 | * |
LievenHollevoet | 0:6fbf67893ca2 | 53 | * Si7005 rh(p28, p27); // sda, scl |
LievenHollevoet | 0:6fbf67893ca2 | 54 | * DigitalOut rh_cs_n(p26); // Chip select signal |
LievenHollevoet | 0:6fbf67893ca2 | 55 | * Serial uart(USBTX, USBRX); // tx, rx |
LievenHollevoet | 0:6fbf67893ca2 | 56 | |
LievenHollevoet | 0:6fbf67893ca2 | 57 | * int main() { |
LievenHollevoet | 0:6fbf67893ca2 | 58 | * |
LievenHollevoet | 0:6fbf67893ca2 | 59 | * // Enable RH sensor |
LievenHollevoet | 0:6fbf67893ca2 | 60 | * rh_cs_n = 0; |
LievenHollevoet | 0:6fbf67893ca2 | 61 | * // Let it startup! |
LievenHollevoet | 0:6fbf67893ca2 | 62 | * wait(0.05); |
LievenHollevoet | 0:6fbf67893ca2 | 63 | * |
LievenHollevoet | 0:6fbf67893ca2 | 64 | * char id; |
LievenHollevoet | 0:6fbf67893ca2 | 65 | * id = rh.readID(); |
LievenHollevoet | 0:6fbf67893ca2 | 66 | * uart.printf("Sensor type: %02X\n", id); |
LievenHollevoet | 0:6fbf67893ca2 | 67 | |
LievenHollevoet | 0:6fbf67893ca2 | 68 | * // Relative humidity measurement |
LievenHollevoet | 0:6fbf67893ca2 | 69 | * rh.startMeasurement(SI7005_RH); |
LievenHollevoet | 0:6fbf67893ca2 | 70 | * while (!rh.conversionDone()) { |
LievenHollevoet | 0:6fbf67893ca2 | 71 | * wait(0.01); |
LievenHollevoet | 0:6fbf67893ca2 | 72 | * } |
LievenHollevoet | 0:6fbf67893ca2 | 73 | * int measurement = rh.readResult(SI7005_RH); |
LievenHollevoet | 0:6fbf67893ca2 | 74 | * uart.printf("RH = %i procent\n", measurement); |
LievenHollevoet | 0:6fbf67893ca2 | 75 | * |
LievenHollevoet | 0:6fbf67893ca2 | 76 | * // Start temperature measurement |
LievenHollevoet | 0:6fbf67893ca2 | 77 | * rh.startMeasurement(SI7005_T); |
LievenHollevoet | 0:6fbf67893ca2 | 78 | * while (!rh.conversionDone()){ |
LievenHollevoet | 0:6fbf67893ca2 | 79 | * wait (0.01); |
LievenHollevoet | 0:6fbf67893ca2 | 80 | * } |
LievenHollevoet | 0:6fbf67893ca2 | 81 | * measurement = rh.readResult(SI7005_T); |
LievenHollevoet | 0:6fbf67893ca2 | 82 | * uart.printf("Temp = %i degrees C\n", measurement); |
LievenHollevoet | 0:6fbf67893ca2 | 83 | * |
LievenHollevoet | 0:6fbf67893ca2 | 84 | * // Disable the sensor |
LievenHollevoet | 0:6fbf67893ca2 | 85 | * rh_cs_n = 1; |
LievenHollevoet | 0:6fbf67893ca2 | 86 | * |
LievenHollevoet | 0:6fbf67893ca2 | 87 | * } |
LievenHollevoet | 0:6fbf67893ca2 | 88 | * @endcode |
LievenHollevoet | 0:6fbf67893ca2 | 89 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 90 | class Si7005 { |
LievenHollevoet | 0:6fbf67893ca2 | 91 | |
LievenHollevoet | 0:6fbf67893ca2 | 92 | public: |
LievenHollevoet | 0:6fbf67893ca2 | 93 | |
LievenHollevoet | 0:6fbf67893ca2 | 94 | /** Creates the sensor interface instance |
LievenHollevoet | 0:6fbf67893ca2 | 95 | * @param sda I2C data pin |
LievenHollevoet | 0:6fbf67893ca2 | 96 | * @param scl I2C clock pin |
LievenHollevoet | 0:6fbf67893ca2 | 97 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 98 | Si7005(PinName sda, PinName scl); |
LievenHollevoet | 0:6fbf67893ca2 | 99 | |
LievenHollevoet | 0:6fbf67893ca2 | 100 | /** Read the chip ID |
LievenHollevoet | 0:6fbf67893ca2 | 101 | * @returns The chip ID register contents. Should be in the format 0x5y for a Si7005 sensor where 'y' is the chip revision. |
LievenHollevoet | 0:6fbf67893ca2 | 102 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 103 | int readID (void); |
LievenHollevoet | 0:6fbf67893ca2 | 104 | |
LievenHollevoet | 0:6fbf67893ca2 | 105 | /** Start a measurement for relative humidity or temperature |
LievenHollevoet | 0:6fbf67893ca2 | 106 | * Initiate a measurement. The type can be either SI7005_RH for a relative humidity measurement or SI7005_T for a temperature measurement. |
LievenHollevoet | 0:6fbf67893ca2 | 107 | * This function starts the conversion in the sensor. Before reading out the |
LievenHollevoet | 0:6fbf67893ca2 | 108 | * result, ensure the conversion is done by calling conversionDone(). |
LievenHollevoet | 0:6fbf67893ca2 | 109 | * @param type Either SI7005_RH for humidity measurement or SI7005_T for temperature |
LievenHollevoet | 0:6fbf67893ca2 | 110 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 111 | void startMeasurement (char type); |
LievenHollevoet | 0:6fbf67893ca2 | 112 | |
LievenHollevoet | 0:6fbf67893ca2 | 113 | /** Checks if the measurement is done. |
LievenHollevoet | 0:6fbf67893ca2 | 114 | * Reports true if the conversion is finished. This is detected by reading the DONE bit in the STATUS register of the sensor. |
LievenHollevoet | 0:6fbf67893ca2 | 115 | * @returns true if the conversion is done |
LievenHollevoet | 0:6fbf67893ca2 | 116 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 117 | bool conversionDone (void); |
LievenHollevoet | 0:6fbf67893ca2 | 118 | |
LievenHollevoet | 0:6fbf67893ca2 | 119 | /** Reads the measurement result. |
LievenHollevoet | 0:6fbf67893ca2 | 120 | * @param type Either SI7005_RH for humidity measurement or SI7005_T for temperature |
LievenHollevoet | 0:6fbf67893ca2 | 121 | * @returns The measurement result as specified in the datasheet, a 12-bit value for the relative humidity measurement and a 14-bit value for a temperature measurement. |
LievenHollevoet | 0:6fbf67893ca2 | 122 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 123 | int readResult (char type); |
LievenHollevoet | 0:6fbf67893ca2 | 124 | |
LievenHollevoet | 0:6fbf67893ca2 | 125 | protected: |
LievenHollevoet | 0:6fbf67893ca2 | 126 | |
LievenHollevoet | 0:6fbf67893ca2 | 127 | void _write(int reg, int data); |
LievenHollevoet | 0:6fbf67893ca2 | 128 | int _read(int reg); |
LievenHollevoet | 0:6fbf67893ca2 | 129 | int _read_int(int reg); |
LievenHollevoet | 0:6fbf67893ca2 | 130 | int _addr; |
LievenHollevoet | 0:6fbf67893ca2 | 131 | I2C _i2c; |
LievenHollevoet | 0:6fbf67893ca2 | 132 | |
LievenHollevoet | 0:6fbf67893ca2 | 133 | }; |
LievenHollevoet | 0:6fbf67893ca2 | 134 | |
LievenHollevoet | 0:6fbf67893ca2 | 135 | |
LievenHollevoet | 0:6fbf67893ca2 | 136 | #endif |
LievenHollevoet | 0:6fbf67893ca2 | 137 |