Lieven Hollevoet / Si7005

Dependents:   Si7005_example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Si7005.cpp Source File

Si7005.cpp

00001 /* mbed Si7005 relative humidity sensor Library
00002  *
00003  * Copyright (c) 2013, Lieven Hollevoet (http://likatronix.be)
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 
00025 #include "Si7005.h"
00026 #include "mbed.h"
00027 
00028 /*
00029  Constructor, pin names for I2C of the device
00030  */
00031 Si7005::Si7005(PinName scl, PinName sda)
00032         : _i2c(scl, sda) {
00033 
00034     _i2c.frequency(50000);
00035       _addr = 0x80;
00036 }
00037 
00038 
00039 int Si7005::readID(void) {
00040     int data;
00041 
00042     data = _read(SI7005_REG_ID);
00043 
00044     return data;
00045 
00046 }
00047 
00048 void Si7005::startMeasurement(char type) {
00049     char command;
00050 
00051     switch (type) {
00052     case SI7005_RH:
00053         command = 0x01; // Start relative humidity measurement
00054         break;
00055     case SI7005_T:
00056         command = 0x11; // Start temperature measurement
00057         break;
00058     default:
00059         return;
00060     }
00061 
00062     _write(SI7005_REG_CONFIG, command);
00063 
00064     return;
00065 }
00066 
00067 bool Si7005::conversionDone(void) {
00068     int result;
00069     result = _read(SI7005_REG_STATUS);
00070 
00071     if (result == 0) {
00072         return true;
00073     } else {
00074         return false;
00075     }
00076 
00077 }
00078 
00079 int Si7005::readResult(char type) {
00080     int result;
00081     float f_res;
00082     float f_lin;
00083 
00084     result = _read_int(SI7005_REG_DATAH);
00085 
00086     switch (type) {
00087     case SI7005_RH:
00088         // Keep upper 12 bits
00089         result = result >> 4;
00090         // Calculate %
00091         f_res = ((float)result / 16) - 24;
00092         // Linearize
00093         f_lin = f_res - ((f_res * f_res) * - 0.00393 + f_res * 0.4008 - 4.7844 );
00094         return (int)f_lin;
00095     case SI7005_T:
00096         // Keep upper 14 bits
00097         result = result >> 2;
00098         // Calculate %
00099         result = (result / 32) - 50;
00100         return result;
00101     default:
00102         return -1;
00103     }
00104 }
00105 // private functions for low level IO
00106 // The Si7005 needs a restart bit when doing read accesses
00107 
00108 void Si7005::_write(int reg, int data) {
00109     char args[2];
00110     args[0] = reg;
00111     args[1] = data;
00112     _i2c.write(_addr, args,2);
00113 }
00114 
00115 int Si7005::_read(int reg) {
00116     char args[2];
00117     args[0] = reg;
00118     _i2c.write(_addr, args, 1, true); // No stop condition here
00119     _i2c.read(_addr, args, 1, true);  // And a restart condition here
00120     _i2c.stop();
00121     return(args[0]);
00122 }
00123 
00124 int Si7005::_read_int(int reg) {
00125     char data[2];
00126     data[0] = reg;
00127     _i2c.write(_addr, data, 1, true); // No stop condition here
00128     _i2c.read(_addr, data, 2, true);  // And a restart condition here
00129     _i2c.stop();
00130 
00131     // Data is read out MSB first
00132 
00133     return ((data[0] << 8) + data[1]);
00134 
00135 }
00136