Jurica Resetar / Si7006A20

Dependents:   acd52832_Humidity_Temp_Example iBeacon acnsensa acnSENSA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Si7006A20.cpp Source File

Si7006A20.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  *
00015  *******************************************************************************
00016  */
00017 
00018  /*
00019  * The library is modified to suite Si7006A20 Humidity and Temperature Sensor
00020  * by Jurica Resetar @ aconno
00021  * More info and contact: aconno.de
00022  * jurica_resetar@yahoo.com
00023  *
00024  */
00025 
00026 #include "Si7006A20.h"
00027 #include "aconno_i2c.h"
00028 
00029 #define TEMPERATURE_DELAY_MS   (10)
00030 #define HUMIDITY_DELAY_MS   (20)
00031 
00032 char cmd_meas_humid = 0xE5;
00033 char cmd_meas_humid_no_hold = 0xF5;
00034 char cmd_meas_temp = 0xE3;
00035 char cmd_meas_temp_no_hold = 0xF3;
00036 char cmd_meas_prev_temp = 0xE0;
00037 char cmd_rst = 0xFE;
00038 char cmd_write_user1 = 0xE6;
00039 char cmd_read_user1 = 0xE7;
00040 char cmd_id_1[] = {0xFA, 0x0F};
00041 char cmd_id_2[] = {0xFC, 0xC9};
00042 char cmd_fw_ver[] = {0x84, 0xB8};
00043 
00044 
00045 Si7006::Si7006(I2C *i2c) : i2c(i2c, char(I2C_ADDR)){
00046     
00047 }
00048 
00049 uint8_t Si7006::getElectronicId(char* id){
00050     uint8_t success;
00051     
00052     success = i2c.sendCommand(cmd_id_1, sizeof(cmd_id_1), id, 4);
00053     success = i2c.sendCommand(cmd_id_2, sizeof(cmd_id_2), (id+4), 4);
00054     
00055     return success;
00056 }
00057 
00058 int Si7006::configResolution(Si7006::resolution_t resolution){
00059     char data;
00060     
00061     // Read User1 Register
00062     i2c.readFromReg(cmd_read_user1, &data, 1);
00063     switch (resolution) {
00064         case RH_12b_TEMP_14b:
00065             data &= ~0x81;
00066             break;
00067         case RH_8b_TEMP_12b:
00068             data = (data & ~0x80) | 0x01;
00069             break;
00070         case RH_10b_TEMP_13b:
00071             data = (data & ~0x01) | 0x80;
00072             break;
00073         case RH_11b_TEMP_11b:
00074             data |= 0x81;
00075             break;
00076         default:
00077             return -1;
00078     }
00079 
00080     return i2c.writeToReg(cmd_write_user1, &data, 1);    
00081 }
00082 
00083 float Si7006::getTemperature(){
00084     char temper[2];
00085     
00086     i2c.sendCommand(&cmd_meas_temp_no_hold, sizeof(cmd_meas_temp_no_hold));
00087     wait_ms(TEMPERATURE_DELAY_MS);
00088     i2c.readBus(temper, 2);
00089 
00090     return calculateTemperature(temper);
00091 }
00092 
00093 float Si7006::calculateTemperature(char *rawTemp){
00094     float temperature;
00095     int16_t code;
00096     
00097     code = (uint16_t)((*(rawTemp+0)<<8) + (*(rawTemp+1)<<0));
00098     temperature = (((float)175.72 * code) / 65536.0f) - 46.85;
00099     
00100     //return temperature;
00101     return temperature;
00102 }
00103 
00104 float Si7006::getHumidity(){
00105     char humid[2];
00106     
00107     i2c.sendCommand(&cmd_meas_humid, sizeof(cmd_meas_humid));
00108     wait_ms(HUMIDITY_DELAY_MS);
00109     i2c.readBus(humid, 2);
00110     
00111     return checkHumidity(humid);
00112 }
00113 
00114 float Si7006::checkHumidity(char *rawHumidity){
00115     float humidity;
00116     uint16_t code;
00117 
00118     // Get 16-bit value from bytes read
00119     code = (uint16_t)((*(rawHumidity+0)<<8) + (*(rawHumidity+1)<<0));
00120     // Calculate the humidity using the formula from the datasheet
00121     humidity = ((((float)125 * code)) / 65536.0f) - 6;
00122 
00123     return humidity;
00124 }
00125 
00126 int Si7006::getPrevTemperature(float *tempC) {
00127     /*
00128     if (i2c.write(i2cADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) {
00129         return -1;
00130     }
00131     if(checkTemperature(tempC) != 0) {
00132         return -1;
00133     }
00134     return 0;
00135     */
00136 }
00137 
00138 int Si7006::getPrevTemperature(int16_t *tempCx10) {
00139     /*
00140     if (i2c.write(i2cADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) {
00141         return -1;
00142     }
00143     if(checkTemperature(tempCx10) != 0) {
00144         return -1;
00145     }
00146     return 0;
00147     */
00148 }
00149 
00150 int Si7006::getRev(char *rev){
00151     /*
00152     if (i2c.write(i2cADDR, cmd_fw_ver, sizeof(cmd_fw_ver)) != 0) {
00153         return -1;
00154     }
00155     if (i2c.read(i2cADDR, rev, 1) != 0) {
00156         return -1;
00157     }
00158     
00159     return 0;
00160     */
00161 }
00162 
00163 int Si7006::heater(bool enable){
00164     char data[2];
00165     /*
00166     if (i2c.write(i2cADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) {
00167         return -1;
00168     }
00169 
00170     if (i2c.read(i2cADDR, &data[1], 1) != 0) {
00171         return -1;
00172     }
00173 
00174     if (enable) {
00175         data[1] |= 0x04;
00176     } else {
00177         data[1] &= ~0x04;
00178     }
00179 
00180     data[0] = cmd_write_user1[0];
00181 
00182     if (i2c.write(i2cADDR, data, 2) != 0) {
00183         return -1;
00184     }
00185 
00186     return 0;
00187     */
00188 }
00189 
00190 char Si7006::crc8(char value, char seed){
00191     int i;
00192 
00193     for (i = 0; i < 8; i++) {
00194 
00195         if ((value ^ seed) & 0x80) {
00196             seed <<= 1;
00197             seed ^= POLYVAL;
00198         } else {
00199             seed <<= 1;
00200         }
00201         value <<= 1;
00202     }
00203     return seed;
00204 }