Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:6fbf67893ca2, committed 2013-04-10
- Comitter:
- LievenHollevoet
- Date:
- Wed Apr 10 08:43:56 2013 +0000
- Commit message:
- First working version of a library to interface to a Silicon Labs Si7005 relative humidity sensor.
Changed in this revision
| Si7005.cpp | Show annotated file Show diff for this revision Revisions of this file |
| Si7005.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Si7005.cpp Wed Apr 10 08:43:56 2013 +0000
@@ -0,0 +1,136 @@
+/* mbed Si7005 relative humidity sensor Library
+ *
+ * Copyright (c) 2013, Lieven Hollevoet (http://likatronix.be)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+
+#include "Si7005.h"
+#include "mbed.h"
+
+/*
+ Constructor, pin names for I2C of the device
+ */
+Si7005::Si7005(PinName scl, PinName sda)
+ : _i2c(scl, sda) {
+
+ _i2c.frequency(50000);
+ _addr = 0x80;
+}
+
+
+int Si7005::readID(void) {
+ int data;
+
+ data = _read(SI7005_REG_ID);
+
+ return data;
+
+}
+
+void Si7005::startMeasurement(char type) {
+ char command;
+
+ switch (type) {
+ case SI7005_RH:
+ command = 0x01; // Start relative humidity measurement
+ break;
+ case SI7005_T:
+ command = 0x11; // Start temperature measurement
+ break;
+ default:
+ return;
+ }
+
+ _write(SI7005_REG_CONFIG, command);
+
+ return;
+}
+
+bool Si7005::conversionDone(void) {
+ int result;
+ result = _read(SI7005_REG_STATUS);
+
+ if (result == 0) {
+ return true;
+ } else {
+ return false;
+ }
+
+}
+
+int Si7005::readResult(char type) {
+ int result;
+ float f_res;
+ float f_lin;
+
+ result = _read_int(SI7005_REG_DATAH);
+
+ switch (type) {
+ case SI7005_RH:
+ // Keep upper 12 bits
+ result = result >> 4;
+ // Calculate %
+ f_res = ((float)result / 16) - 24;
+ // Linearize
+ f_lin = f_res - ((f_res * f_res) * - 0.00393 + f_res * 0.4008 - 4.7844 );
+ return (int)f_lin;
+ case SI7005_T:
+ // Keep upper 14 bits
+ result = result >> 2;
+ // Calculate %
+ result = (result / 32) - 50;
+ return result;
+ default:
+ return -1;
+ }
+}
+// private functions for low level IO
+// The Si7005 needs a restart bit when doing read accesses
+
+void Si7005::_write(int reg, int data) {
+ char args[2];
+ args[0] = reg;
+ args[1] = data;
+ _i2c.write(_addr, args,2);
+}
+
+int Si7005::_read(int reg) {
+ char args[2];
+ args[0] = reg;
+ _i2c.write(_addr, args, 1, true); // No stop condition here
+ _i2c.read(_addr, args, 1, true); // And a restart condition here
+ _i2c.stop();
+ return(args[0]);
+}
+
+int Si7005::_read_int(int reg) {
+ char data[2];
+ data[0] = reg;
+ _i2c.write(_addr, data, 1, true); // No stop condition here
+ _i2c.read(_addr, data, 2, true); // And a restart condition here
+ _i2c.stop();
+
+ // Data is read out MSB first
+
+ return ((data[0] << 8) + data[1]);
+
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Si7005.h Wed Apr 10 08:43:56 2013 +0000
@@ -0,0 +1,137 @@
+/* mbed Si7005 relative humidity sensor Library
+ *
+ * Copyright (c) 2013, Lieven Hollevoet (http://likatronix.be)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef SI7005_H
+#define SI7005_H
+
+#include "mbed.h"
+
+/*
+ * register names
+ */
+
+#define SI7005_REG_STATUS 0
+#define SI7005_REG_DATAH 1
+#define SI7005_REG_DATAL 2
+#define SI7005_REG_CONFIG 3
+#define SI7005_REG_ID 17
+
+/*
+ * measurement types
+ */
+
+#define SI7005_RH 0
+#define SI7005_T 1
+
+/** Class to interface to a Silicon Labs Si7005 relative humidity sensor.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "Si7005.h"
+ *
+ * Si7005 rh(p28, p27); // sda, scl
+ * DigitalOut rh_cs_n(p26); // Chip select signal
+ * Serial uart(USBTX, USBRX); // tx, rx
+
+ * int main() {
+ *
+ * // Enable RH sensor
+ * rh_cs_n = 0;
+ * // Let it startup!
+ * wait(0.05);
+ *
+ * char id;
+ * id = rh.readID();
+ * uart.printf("Sensor type: %02X\n", id);
+
+ * // Relative humidity measurement
+ * rh.startMeasurement(SI7005_RH);
+ * while (!rh.conversionDone()) {
+ * wait(0.01);
+ * }
+ * int measurement = rh.readResult(SI7005_RH);
+ * uart.printf("RH = %i procent\n", measurement);
+ *
+ * // Start temperature measurement
+ * rh.startMeasurement(SI7005_T);
+ * while (!rh.conversionDone()){
+ * wait (0.01);
+ * }
+ * measurement = rh.readResult(SI7005_T);
+ * uart.printf("Temp = %i degrees C\n", measurement);
+ *
+ * // Disable the sensor
+ * rh_cs_n = 1;
+ *
+ * }
+ * @endcode
+ */
+class Si7005 {
+
+public:
+
+ /** Creates the sensor interface instance
+ * @param sda I2C data pin
+ * @param scl I2C clock pin
+ */
+ Si7005(PinName sda, PinName scl);
+
+ /** Read the chip ID
+ * @returns The chip ID register contents. Should be in the format 0x5y for a Si7005 sensor where 'y' is the chip revision.
+ */
+ int readID (void);
+
+ /** Start a measurement for relative humidity or temperature
+ * Initiate a measurement. The type can be either SI7005_RH for a relative humidity measurement or SI7005_T for a temperature measurement.
+ * This function starts the conversion in the sensor. Before reading out the
+ * result, ensure the conversion is done by calling conversionDone().
+ * @param type Either SI7005_RH for humidity measurement or SI7005_T for temperature
+ */
+ void startMeasurement (char type);
+
+ /** Checks if the measurement is done.
+ * Reports true if the conversion is finished. This is detected by reading the DONE bit in the STATUS register of the sensor.
+ * @returns true if the conversion is done
+ */
+ bool conversionDone (void);
+
+ /** Reads the measurement result.
+ * @param type Either SI7005_RH for humidity measurement or SI7005_T for temperature
+ * @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.
+ */
+ int readResult (char type);
+
+protected:
+
+ void _write(int reg, int data);
+ int _read(int reg);
+ int _read_int(int reg);
+ int _addr;
+ I2C _i2c;
+
+};
+
+
+#endif
+