Library for Silicon Labs Si7006 digital temperature and humidity chip.
Dependents: acd52832_Humidity_Temp_Example iBeacon acnsensa acnSENSA
Si7006A20.cpp
- Committer:
- jurica238814
- Date:
- 2017-07-10
- Revision:
- 0:cc2c5d49fe63
- Child:
- 1:155934570c14
File content as of revision 0:cc2c5d49fe63:
/******************************************************************************* * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. * * 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 library is modified to suite Si7006A20 Humidity and Temperature Sesor * Made by Jurica Resetar @ aconno * More info and contact: aconno.de * ******************************************************************************* */ #include "Si7006A20.h" /***** Definitions *****/ #define I2C_ADDR (0x80) // 1000000x #define POLYVAL (0x131) /***** File Scope Data *****/ static const char cmd_meas_humid[] = { 0xE5 }; static const char cmd_meas_humid_no_hold[] = { 0xF5 }; static const char cmd_meas_temp[] = { 0xE3 }; static const char cmd_meas_temp_no_hold[] = { 0xF3 }; static const char cmd_meas_prev_temp[] = { 0xE0 }; static const char cmd_rst[] = { 0xFE }; static const char cmd_write_user1[] = { 0xE6 }; static const char cmd_read_user1[] = { 0xE7 }; static const char cmd_id_1[] = { 0xFA, 0x0F }; static const char cmd_id_2[] = { 0xFC, 0xC9 }; static const char cmd_fw_ver[] = { 0x84, 0xB8 }; //****************************************************************************** Si7006::Si7006(PinName sda, PinName scl){ i2c_ = new I2C(sda, scl); i2c_owner = true; // 400KHz, as specified by the datasheet. i2c_->frequency(400000); } //****************************************************************************** Si7006::Si7006(I2C *i2c) : i2c_(i2c){ i2c_owner = false; } //****************************************************************************** Si7006::~Si7006(){ if(i2c_owner) { delete i2c_; } } //****************************************************************************** int Si7006::reset(void){ if (i2c_->write(I2C_ADDR, cmd_rst, sizeof(cmd_rst)) != 0) { return -1; } return 0; } //****************************************************************************** int Si7006::getElectronicId(char* id){ // Send cmd with repeated start if (i2c_->write(I2C_ADDR, cmd_id_1, sizeof(cmd_id_1), true) != 0) { return -1; } // Read first portion of ID char temp[8]; if (i2c_->read(I2C_ADDR, temp, 8, false) != 0) { return -1; } // Check the CRC char crc = 0; int i; for(i = 0; i < 4; i++) { crc = crc8(temp[2*i], crc); if(crc != temp[2*i +1]) { return -1; } } // Save top portion of ID id[7] = temp [0]; id[6] = temp [2]; id[5] = temp [4]; id[4] = temp [6]; // Send cmd with repeated start if (i2c_->write(I2C_ADDR, cmd_id_2, sizeof(cmd_id_2), true) != 0) { return -1; } // Read rest of ID if (i2c_->read(I2C_ADDR, temp, 6, false) != 0) { return -1; } // Check the CRC crc = 0; for(i = 0; i < 2; i++) { crc = crc8(temp[3*i], crc); crc = crc8(temp[3*i + 1], crc); if(crc != temp[3*i + 2]) { return -1; } } // Save bottom portion of ID id[3] = temp [0]; id[2] = temp [1]; id[1] = temp [3]; id[0] = temp [4]; return 0; } //****************************************************************************** int Si7006::configResolution(Si7006::resolution_t resolution){ char data[2]; if (i2c_->write(I2C_ADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) { return -1; } if (i2c_->read(I2C_ADDR, &data[1], 1) != 0) { return -1; } switch (resolution) { case RH_12b_TEMP_14b: data[1] &= ~0x81; break; case RH_8b_TEMP_12b: data[1] = (data[1] & ~0x80) | 0x01; break; case RH_10b_TEMP_13b: data[1] = (data[1] & ~0x01) | 0x80; break; case RH_11b_TEMP_11b: data[1] |= 0x81; break; default: return -1; } data[0] = cmd_write_user1[0]; if (i2c_->write(I2C_ADDR, data, 2) != 0) { return -1; } return 0; } //****************************************************************************** int Si7006::getTemperature(int16_t *tempCx10){ if (i2c_->write(I2C_ADDR, cmd_meas_temp, sizeof(cmd_meas_temp)) != 0) { return -1; } return checkTemperature(tempCx10); } //****************************************************************************** int Si7006::getTemperature(float *tempC){ if (i2c_->write(I2C_ADDR, cmd_meas_temp, sizeof(cmd_meas_temp)) != 0) { return -1; } return checkTemperature(tempC); } //****************************************************************************** int Si7006::startTemperature(void){ if (i2c_->write(I2C_ADDR, cmd_meas_temp_no_hold, sizeof(cmd_meas_temp_no_hold)) != 0) { return -1; } return 0; } //****************************************************************************** int Si7006::checkTemperature(int16_t *tempCx10){ char data[3]; uint16_t code; int temp; if (i2c_->read(I2C_ADDR, data, 3) != 0) { return -1; } // Get 16-bit value from bytes read code = ((uint16_t)data[0] << 8) + data[1]; // Calculate the temperature using the formula from the datasheet // Scaled by 100 temp = ((((int)17572 * code) + 0x8000) >> 16) - 4685; // Return value is to be scaled by 10 *tempCx10 = (temp + 5) / 10; return 0; } //****************************************************************************** int Si7006::checkTemperature(float *tempC){ char data[3]; uint16_t code; if (i2c_->read(I2C_ADDR, data, 3) != 0) { return -1; } // Get 16-bit value from bytes read code = ((uint16_t)data[0] << 8) + data[1]; // Calculate the temperature using the formula from the datasheet // Scaled by 100 *tempC = (((float)175.72 * code) / 65536) - 46.85; return 0; } //****************************************************************************** int Si7006::getHumidity(int16_t *humidx10){ if (i2c_->write(I2C_ADDR, cmd_meas_humid, sizeof(cmd_meas_humid), true) != 0) { //return -1; } wait_ms(100); return checkHumidity(humidx10); } //****************************************************************************** int Si7006::getHumidity(float *humid){ if (i2c_->write(I2C_ADDR, cmd_meas_humid, sizeof(cmd_meas_humid)) != 0) { //return -1; } wait_ms(100); return checkHumidity(humid); } //****************************************************************************** int Si7006::startHumidity(void){ if (i2c_->write(I2C_ADDR, cmd_meas_humid_no_hold, sizeof(cmd_meas_humid_no_hold)) != 0) { return -1; } return 0; } //****************************************************************************** int Si7006::checkHumidity(int16_t *humidx10){ char data[3]; uint16_t code; if (i2c_->read(I2C_ADDR, data, 3) != 0) { return -1; } // Get 16-bit value from bytes read code = ((uint16_t)data[0] << 8) + data[1]; // Calculate the humidity using the formula from the datasheet // Scaled by 10 *humidx10 = ((((int)1250 * code) + 0x8000) >> 16) - 60; // Check the crc char crc = crc8(data[0], 0x00); crc = crc8(data[1], crc); if(crc != data[2]) { return -1; } return 0; } //****************************************************************************** int Si7006::checkHumidity(float *humid){ char data[3]; uint16_t code; if (i2c_->read(I2C_ADDR, data, 3) != 0) { return -1; } // Get 16-bit value from bytes read code = ((uint16_t)data[0] << 8) + data[1]; // Calculate the humidity using the formula from the datasheet *humid = (((float)125 * code) / 65536) - 6; return 0; } //****************************************************************************** int Si7006::getPrevTemperature(float *tempC) { if (i2c_->write(I2C_ADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) { return -1; } if(checkTemperature(tempC) != 0) { return -1; } return 0; } //****************************************************************************** int Si7006::getPrevTemperature(int16_t *tempCx10) { if (i2c_->write(I2C_ADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) { return -1; } if(checkTemperature(tempCx10) != 0) { return -1; } return 0; } //****************************************************************************** int Si7006::getRev(char *rev){ if (i2c_->write(I2C_ADDR, cmd_fw_ver, sizeof(cmd_fw_ver)) != 0) { return -1; } if (i2c_->read(I2C_ADDR, rev, 1) != 0) { return -1; } return 0; } //****************************************************************************** int Si7006::heater(bool enable) { char data[2]; if (i2c_->write(I2C_ADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) { return -1; } if (i2c_->read(I2C_ADDR, &data[1], 1) != 0) { return -1; } if (enable) { data[1] |= 0x04; } else { data[1] &= ~0x04; } data[0] = cmd_write_user1[0]; if (i2c_->write(I2C_ADDR, data, 2) != 0) { return -1; } return 0; } //****************************************************************************** char Si7006::crc8(char value, char seed) { int i; for (i = 0; i < 8; i++) { if ((value ^ seed) & 0x80) { seed <<= 1; seed ^= POLYVAL; } else { seed <<= 1; } value <<= 1; } return seed; }