Interface to a Silicon Labs Si7005 relative humidity sensor.
Si7005.cpp@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 | |
LievenHollevoet | 0:6fbf67893ca2 | 25 | #include "Si7005.h" |
LievenHollevoet | 0:6fbf67893ca2 | 26 | #include "mbed.h" |
LievenHollevoet | 0:6fbf67893ca2 | 27 | |
LievenHollevoet | 0:6fbf67893ca2 | 28 | /* |
LievenHollevoet | 0:6fbf67893ca2 | 29 | Constructor, pin names for I2C of the device |
LievenHollevoet | 0:6fbf67893ca2 | 30 | */ |
LievenHollevoet | 0:6fbf67893ca2 | 31 | Si7005::Si7005(PinName scl, PinName sda) |
LievenHollevoet | 0:6fbf67893ca2 | 32 | : _i2c(scl, sda) { |
LievenHollevoet | 0:6fbf67893ca2 | 33 | |
LievenHollevoet | 0:6fbf67893ca2 | 34 | _i2c.frequency(50000); |
LievenHollevoet | 0:6fbf67893ca2 | 35 | _addr = 0x80; |
LievenHollevoet | 0:6fbf67893ca2 | 36 | } |
LievenHollevoet | 0:6fbf67893ca2 | 37 | |
LievenHollevoet | 0:6fbf67893ca2 | 38 | |
LievenHollevoet | 0:6fbf67893ca2 | 39 | int Si7005::readID(void) { |
LievenHollevoet | 0:6fbf67893ca2 | 40 | int data; |
LievenHollevoet | 0:6fbf67893ca2 | 41 | |
LievenHollevoet | 0:6fbf67893ca2 | 42 | data = _read(SI7005_REG_ID); |
LievenHollevoet | 0:6fbf67893ca2 | 43 | |
LievenHollevoet | 0:6fbf67893ca2 | 44 | return data; |
LievenHollevoet | 0:6fbf67893ca2 | 45 | |
LievenHollevoet | 0:6fbf67893ca2 | 46 | } |
LievenHollevoet | 0:6fbf67893ca2 | 47 | |
LievenHollevoet | 0:6fbf67893ca2 | 48 | void Si7005::startMeasurement(char type) { |
LievenHollevoet | 0:6fbf67893ca2 | 49 | char command; |
LievenHollevoet | 0:6fbf67893ca2 | 50 | |
LievenHollevoet | 0:6fbf67893ca2 | 51 | switch (type) { |
LievenHollevoet | 0:6fbf67893ca2 | 52 | case SI7005_RH: |
LievenHollevoet | 0:6fbf67893ca2 | 53 | command = 0x01; // Start relative humidity measurement |
LievenHollevoet | 0:6fbf67893ca2 | 54 | break; |
LievenHollevoet | 0:6fbf67893ca2 | 55 | case SI7005_T: |
LievenHollevoet | 0:6fbf67893ca2 | 56 | command = 0x11; // Start temperature measurement |
LievenHollevoet | 0:6fbf67893ca2 | 57 | break; |
LievenHollevoet | 0:6fbf67893ca2 | 58 | default: |
LievenHollevoet | 0:6fbf67893ca2 | 59 | return; |
LievenHollevoet | 0:6fbf67893ca2 | 60 | } |
LievenHollevoet | 0:6fbf67893ca2 | 61 | |
LievenHollevoet | 0:6fbf67893ca2 | 62 | _write(SI7005_REG_CONFIG, command); |
LievenHollevoet | 0:6fbf67893ca2 | 63 | |
LievenHollevoet | 0:6fbf67893ca2 | 64 | return; |
LievenHollevoet | 0:6fbf67893ca2 | 65 | } |
LievenHollevoet | 0:6fbf67893ca2 | 66 | |
LievenHollevoet | 0:6fbf67893ca2 | 67 | bool Si7005::conversionDone(void) { |
LievenHollevoet | 0:6fbf67893ca2 | 68 | int result; |
LievenHollevoet | 0:6fbf67893ca2 | 69 | result = _read(SI7005_REG_STATUS); |
LievenHollevoet | 0:6fbf67893ca2 | 70 | |
LievenHollevoet | 0:6fbf67893ca2 | 71 | if (result == 0) { |
LievenHollevoet | 0:6fbf67893ca2 | 72 | return true; |
LievenHollevoet | 0:6fbf67893ca2 | 73 | } else { |
LievenHollevoet | 0:6fbf67893ca2 | 74 | return false; |
LievenHollevoet | 0:6fbf67893ca2 | 75 | } |
LievenHollevoet | 0:6fbf67893ca2 | 76 | |
LievenHollevoet | 0:6fbf67893ca2 | 77 | } |
LievenHollevoet | 0:6fbf67893ca2 | 78 | |
LievenHollevoet | 0:6fbf67893ca2 | 79 | int Si7005::readResult(char type) { |
LievenHollevoet | 0:6fbf67893ca2 | 80 | int result; |
LievenHollevoet | 0:6fbf67893ca2 | 81 | float f_res; |
LievenHollevoet | 0:6fbf67893ca2 | 82 | float f_lin; |
LievenHollevoet | 0:6fbf67893ca2 | 83 | |
LievenHollevoet | 0:6fbf67893ca2 | 84 | result = _read_int(SI7005_REG_DATAH); |
LievenHollevoet | 0:6fbf67893ca2 | 85 | |
LievenHollevoet | 0:6fbf67893ca2 | 86 | switch (type) { |
LievenHollevoet | 0:6fbf67893ca2 | 87 | case SI7005_RH: |
LievenHollevoet | 0:6fbf67893ca2 | 88 | // Keep upper 12 bits |
LievenHollevoet | 0:6fbf67893ca2 | 89 | result = result >> 4; |
LievenHollevoet | 0:6fbf67893ca2 | 90 | // Calculate % |
LievenHollevoet | 0:6fbf67893ca2 | 91 | f_res = ((float)result / 16) - 24; |
LievenHollevoet | 0:6fbf67893ca2 | 92 | // Linearize |
LievenHollevoet | 0:6fbf67893ca2 | 93 | f_lin = f_res - ((f_res * f_res) * - 0.00393 + f_res * 0.4008 - 4.7844 ); |
LievenHollevoet | 0:6fbf67893ca2 | 94 | return (int)f_lin; |
LievenHollevoet | 0:6fbf67893ca2 | 95 | case SI7005_T: |
LievenHollevoet | 0:6fbf67893ca2 | 96 | // Keep upper 14 bits |
LievenHollevoet | 0:6fbf67893ca2 | 97 | result = result >> 2; |
LievenHollevoet | 0:6fbf67893ca2 | 98 | // Calculate % |
LievenHollevoet | 0:6fbf67893ca2 | 99 | result = (result / 32) - 50; |
LievenHollevoet | 0:6fbf67893ca2 | 100 | return result; |
LievenHollevoet | 0:6fbf67893ca2 | 101 | default: |
LievenHollevoet | 0:6fbf67893ca2 | 102 | return -1; |
LievenHollevoet | 0:6fbf67893ca2 | 103 | } |
LievenHollevoet | 0:6fbf67893ca2 | 104 | } |
LievenHollevoet | 0:6fbf67893ca2 | 105 | // private functions for low level IO |
LievenHollevoet | 0:6fbf67893ca2 | 106 | // The Si7005 needs a restart bit when doing read accesses |
LievenHollevoet | 0:6fbf67893ca2 | 107 | |
LievenHollevoet | 0:6fbf67893ca2 | 108 | void Si7005::_write(int reg, int data) { |
LievenHollevoet | 0:6fbf67893ca2 | 109 | char args[2]; |
LievenHollevoet | 0:6fbf67893ca2 | 110 | args[0] = reg; |
LievenHollevoet | 0:6fbf67893ca2 | 111 | args[1] = data; |
LievenHollevoet | 0:6fbf67893ca2 | 112 | _i2c.write(_addr, args,2); |
LievenHollevoet | 0:6fbf67893ca2 | 113 | } |
LievenHollevoet | 0:6fbf67893ca2 | 114 | |
LievenHollevoet | 0:6fbf67893ca2 | 115 | int Si7005::_read(int reg) { |
LievenHollevoet | 0:6fbf67893ca2 | 116 | char args[2]; |
LievenHollevoet | 0:6fbf67893ca2 | 117 | args[0] = reg; |
LievenHollevoet | 0:6fbf67893ca2 | 118 | _i2c.write(_addr, args, 1, true); // No stop condition here |
LievenHollevoet | 0:6fbf67893ca2 | 119 | _i2c.read(_addr, args, 1, true); // And a restart condition here |
LievenHollevoet | 0:6fbf67893ca2 | 120 | _i2c.stop(); |
LievenHollevoet | 0:6fbf67893ca2 | 121 | return(args[0]); |
LievenHollevoet | 0:6fbf67893ca2 | 122 | } |
LievenHollevoet | 0:6fbf67893ca2 | 123 | |
LievenHollevoet | 0:6fbf67893ca2 | 124 | int Si7005::_read_int(int reg) { |
LievenHollevoet | 0:6fbf67893ca2 | 125 | char data[2]; |
LievenHollevoet | 0:6fbf67893ca2 | 126 | data[0] = reg; |
LievenHollevoet | 0:6fbf67893ca2 | 127 | _i2c.write(_addr, data, 1, true); // No stop condition here |
LievenHollevoet | 0:6fbf67893ca2 | 128 | _i2c.read(_addr, data, 2, true); // And a restart condition here |
LievenHollevoet | 0:6fbf67893ca2 | 129 | _i2c.stop(); |
LievenHollevoet | 0:6fbf67893ca2 | 130 | |
LievenHollevoet | 0:6fbf67893ca2 | 131 | // Data is read out MSB first |
LievenHollevoet | 0:6fbf67893ca2 | 132 | |
LievenHollevoet | 0:6fbf67893ca2 | 133 | return ((data[0] << 8) + data[1]); |
LievenHollevoet | 0:6fbf67893ca2 | 134 | |
LievenHollevoet | 0:6fbf67893ca2 | 135 | } |
LievenHollevoet | 0:6fbf67893ca2 | 136 |