Library for Silicon Labs Si7006 digital temperature and humidity chip.
Dependents: acd52832_Humidity_Temp_Example iBeacon acnsensa acnSENSA
Si7006A20.cpp@0:cc2c5d49fe63, 2017-07-10 (annotated)
- Committer:
- jurica238814
- Date:
- Mon Jul 10 17:14:44 2017 +0000
- Revision:
- 0:cc2c5d49fe63
- Child:
- 1:155934570c14
Humidity works. Temperature todo (maybe just check).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:cc2c5d49fe63 | 1 | /******************************************************************************* |
jurica238814 | 0:cc2c5d49fe63 | 2 | * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. |
jurica238814 | 0:cc2c5d49fe63 | 3 | * |
jurica238814 | 0:cc2c5d49fe63 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
jurica238814 | 0:cc2c5d49fe63 | 5 | * copy of this software and associated documentation files (the "Software"), |
jurica238814 | 0:cc2c5d49fe63 | 6 | * to deal in the Software without restriction, including without limitation |
jurica238814 | 0:cc2c5d49fe63 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
jurica238814 | 0:cc2c5d49fe63 | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
jurica238814 | 0:cc2c5d49fe63 | 9 | * Software is furnished to do so, subject to the following conditions: |
jurica238814 | 0:cc2c5d49fe63 | 10 | * |
jurica238814 | 0:cc2c5d49fe63 | 11 | * The above copyright notice and this permission notice shall be included |
jurica238814 | 0:cc2c5d49fe63 | 12 | * in all copies or substantial portions of the Software. |
jurica238814 | 0:cc2c5d49fe63 | 13 | * |
jurica238814 | 0:cc2c5d49fe63 | 14 | * |
jurica238814 | 0:cc2c5d49fe63 | 15 | * The library is modified to suite Si7006A20 Humidity and Temperature Sesor |
jurica238814 | 0:cc2c5d49fe63 | 16 | * Made by Jurica Resetar @ aconno |
jurica238814 | 0:cc2c5d49fe63 | 17 | * More info and contact: aconno.de |
jurica238814 | 0:cc2c5d49fe63 | 18 | * |
jurica238814 | 0:cc2c5d49fe63 | 19 | ******************************************************************************* |
jurica238814 | 0:cc2c5d49fe63 | 20 | */ |
jurica238814 | 0:cc2c5d49fe63 | 21 | |
jurica238814 | 0:cc2c5d49fe63 | 22 | #include "Si7006A20.h" |
jurica238814 | 0:cc2c5d49fe63 | 23 | |
jurica238814 | 0:cc2c5d49fe63 | 24 | /***** Definitions *****/ |
jurica238814 | 0:cc2c5d49fe63 | 25 | #define I2C_ADDR (0x80) // 1000000x |
jurica238814 | 0:cc2c5d49fe63 | 26 | #define POLYVAL (0x131) |
jurica238814 | 0:cc2c5d49fe63 | 27 | |
jurica238814 | 0:cc2c5d49fe63 | 28 | /***** File Scope Data *****/ |
jurica238814 | 0:cc2c5d49fe63 | 29 | static const char cmd_meas_humid[] = { 0xE5 }; |
jurica238814 | 0:cc2c5d49fe63 | 30 | static const char cmd_meas_humid_no_hold[] = { 0xF5 }; |
jurica238814 | 0:cc2c5d49fe63 | 31 | static const char cmd_meas_temp[] = { 0xE3 }; |
jurica238814 | 0:cc2c5d49fe63 | 32 | static const char cmd_meas_temp_no_hold[] = { 0xF3 }; |
jurica238814 | 0:cc2c5d49fe63 | 33 | static const char cmd_meas_prev_temp[] = { 0xE0 }; |
jurica238814 | 0:cc2c5d49fe63 | 34 | static const char cmd_rst[] = { 0xFE }; |
jurica238814 | 0:cc2c5d49fe63 | 35 | static const char cmd_write_user1[] = { 0xE6 }; |
jurica238814 | 0:cc2c5d49fe63 | 36 | static const char cmd_read_user1[] = { 0xE7 }; |
jurica238814 | 0:cc2c5d49fe63 | 37 | static const char cmd_id_1[] = { 0xFA, 0x0F }; |
jurica238814 | 0:cc2c5d49fe63 | 38 | static const char cmd_id_2[] = { 0xFC, 0xC9 }; |
jurica238814 | 0:cc2c5d49fe63 | 39 | static const char cmd_fw_ver[] = { 0x84, 0xB8 }; |
jurica238814 | 0:cc2c5d49fe63 | 40 | |
jurica238814 | 0:cc2c5d49fe63 | 41 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 42 | Si7006::Si7006(PinName sda, PinName scl){ |
jurica238814 | 0:cc2c5d49fe63 | 43 | i2c_ = new I2C(sda, scl); |
jurica238814 | 0:cc2c5d49fe63 | 44 | i2c_owner = true; |
jurica238814 | 0:cc2c5d49fe63 | 45 | |
jurica238814 | 0:cc2c5d49fe63 | 46 | // 400KHz, as specified by the datasheet. |
jurica238814 | 0:cc2c5d49fe63 | 47 | i2c_->frequency(400000); |
jurica238814 | 0:cc2c5d49fe63 | 48 | } |
jurica238814 | 0:cc2c5d49fe63 | 49 | |
jurica238814 | 0:cc2c5d49fe63 | 50 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 51 | Si7006::Si7006(I2C *i2c) : i2c_(i2c){ |
jurica238814 | 0:cc2c5d49fe63 | 52 | i2c_owner = false; |
jurica238814 | 0:cc2c5d49fe63 | 53 | } |
jurica238814 | 0:cc2c5d49fe63 | 54 | |
jurica238814 | 0:cc2c5d49fe63 | 55 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 56 | Si7006::~Si7006(){ |
jurica238814 | 0:cc2c5d49fe63 | 57 | if(i2c_owner) { |
jurica238814 | 0:cc2c5d49fe63 | 58 | delete i2c_; |
jurica238814 | 0:cc2c5d49fe63 | 59 | } |
jurica238814 | 0:cc2c5d49fe63 | 60 | } |
jurica238814 | 0:cc2c5d49fe63 | 61 | |
jurica238814 | 0:cc2c5d49fe63 | 62 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 63 | int Si7006::reset(void){ |
jurica238814 | 0:cc2c5d49fe63 | 64 | if (i2c_->write(I2C_ADDR, cmd_rst, sizeof(cmd_rst)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 65 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 66 | } |
jurica238814 | 0:cc2c5d49fe63 | 67 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 68 | } |
jurica238814 | 0:cc2c5d49fe63 | 69 | |
jurica238814 | 0:cc2c5d49fe63 | 70 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 71 | int Si7006::getElectronicId(char* id){ |
jurica238814 | 0:cc2c5d49fe63 | 72 | // Send cmd with repeated start |
jurica238814 | 0:cc2c5d49fe63 | 73 | if (i2c_->write(I2C_ADDR, cmd_id_1, sizeof(cmd_id_1), true) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 74 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 75 | } |
jurica238814 | 0:cc2c5d49fe63 | 76 | |
jurica238814 | 0:cc2c5d49fe63 | 77 | // Read first portion of ID |
jurica238814 | 0:cc2c5d49fe63 | 78 | char temp[8]; |
jurica238814 | 0:cc2c5d49fe63 | 79 | if (i2c_->read(I2C_ADDR, temp, 8, false) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 80 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 81 | } |
jurica238814 | 0:cc2c5d49fe63 | 82 | |
jurica238814 | 0:cc2c5d49fe63 | 83 | // Check the CRC |
jurica238814 | 0:cc2c5d49fe63 | 84 | char crc = 0; |
jurica238814 | 0:cc2c5d49fe63 | 85 | int i; |
jurica238814 | 0:cc2c5d49fe63 | 86 | for(i = 0; i < 4; i++) { |
jurica238814 | 0:cc2c5d49fe63 | 87 | crc = crc8(temp[2*i], crc); |
jurica238814 | 0:cc2c5d49fe63 | 88 | if(crc != temp[2*i +1]) { |
jurica238814 | 0:cc2c5d49fe63 | 89 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 90 | } |
jurica238814 | 0:cc2c5d49fe63 | 91 | } |
jurica238814 | 0:cc2c5d49fe63 | 92 | |
jurica238814 | 0:cc2c5d49fe63 | 93 | // Save top portion of ID |
jurica238814 | 0:cc2c5d49fe63 | 94 | id[7] = temp [0]; |
jurica238814 | 0:cc2c5d49fe63 | 95 | id[6] = temp [2]; |
jurica238814 | 0:cc2c5d49fe63 | 96 | id[5] = temp [4]; |
jurica238814 | 0:cc2c5d49fe63 | 97 | id[4] = temp [6]; |
jurica238814 | 0:cc2c5d49fe63 | 98 | |
jurica238814 | 0:cc2c5d49fe63 | 99 | // Send cmd with repeated start |
jurica238814 | 0:cc2c5d49fe63 | 100 | if (i2c_->write(I2C_ADDR, cmd_id_2, sizeof(cmd_id_2), true) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 101 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 102 | } |
jurica238814 | 0:cc2c5d49fe63 | 103 | |
jurica238814 | 0:cc2c5d49fe63 | 104 | // Read rest of ID |
jurica238814 | 0:cc2c5d49fe63 | 105 | if (i2c_->read(I2C_ADDR, temp, 6, false) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 106 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 107 | } |
jurica238814 | 0:cc2c5d49fe63 | 108 | |
jurica238814 | 0:cc2c5d49fe63 | 109 | // Check the CRC |
jurica238814 | 0:cc2c5d49fe63 | 110 | crc = 0; |
jurica238814 | 0:cc2c5d49fe63 | 111 | for(i = 0; i < 2; i++) { |
jurica238814 | 0:cc2c5d49fe63 | 112 | crc = crc8(temp[3*i], crc); |
jurica238814 | 0:cc2c5d49fe63 | 113 | crc = crc8(temp[3*i + 1], crc); |
jurica238814 | 0:cc2c5d49fe63 | 114 | if(crc != temp[3*i + 2]) { |
jurica238814 | 0:cc2c5d49fe63 | 115 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 116 | } |
jurica238814 | 0:cc2c5d49fe63 | 117 | } |
jurica238814 | 0:cc2c5d49fe63 | 118 | |
jurica238814 | 0:cc2c5d49fe63 | 119 | // Save bottom portion of ID |
jurica238814 | 0:cc2c5d49fe63 | 120 | id[3] = temp [0]; |
jurica238814 | 0:cc2c5d49fe63 | 121 | id[2] = temp [1]; |
jurica238814 | 0:cc2c5d49fe63 | 122 | id[1] = temp [3]; |
jurica238814 | 0:cc2c5d49fe63 | 123 | id[0] = temp [4]; |
jurica238814 | 0:cc2c5d49fe63 | 124 | |
jurica238814 | 0:cc2c5d49fe63 | 125 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 126 | } |
jurica238814 | 0:cc2c5d49fe63 | 127 | |
jurica238814 | 0:cc2c5d49fe63 | 128 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 129 | int Si7006::configResolution(Si7006::resolution_t resolution){ |
jurica238814 | 0:cc2c5d49fe63 | 130 | char data[2]; |
jurica238814 | 0:cc2c5d49fe63 | 131 | |
jurica238814 | 0:cc2c5d49fe63 | 132 | if (i2c_->write(I2C_ADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 133 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 134 | } |
jurica238814 | 0:cc2c5d49fe63 | 135 | |
jurica238814 | 0:cc2c5d49fe63 | 136 | if (i2c_->read(I2C_ADDR, &data[1], 1) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 137 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 138 | } |
jurica238814 | 0:cc2c5d49fe63 | 139 | |
jurica238814 | 0:cc2c5d49fe63 | 140 | switch (resolution) { |
jurica238814 | 0:cc2c5d49fe63 | 141 | case RH_12b_TEMP_14b: |
jurica238814 | 0:cc2c5d49fe63 | 142 | data[1] &= ~0x81; |
jurica238814 | 0:cc2c5d49fe63 | 143 | break; |
jurica238814 | 0:cc2c5d49fe63 | 144 | case RH_8b_TEMP_12b: |
jurica238814 | 0:cc2c5d49fe63 | 145 | data[1] = (data[1] & ~0x80) | 0x01; |
jurica238814 | 0:cc2c5d49fe63 | 146 | break; |
jurica238814 | 0:cc2c5d49fe63 | 147 | case RH_10b_TEMP_13b: |
jurica238814 | 0:cc2c5d49fe63 | 148 | data[1] = (data[1] & ~0x01) | 0x80; |
jurica238814 | 0:cc2c5d49fe63 | 149 | break; |
jurica238814 | 0:cc2c5d49fe63 | 150 | case RH_11b_TEMP_11b: |
jurica238814 | 0:cc2c5d49fe63 | 151 | data[1] |= 0x81; |
jurica238814 | 0:cc2c5d49fe63 | 152 | break; |
jurica238814 | 0:cc2c5d49fe63 | 153 | default: |
jurica238814 | 0:cc2c5d49fe63 | 154 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 155 | } |
jurica238814 | 0:cc2c5d49fe63 | 156 | |
jurica238814 | 0:cc2c5d49fe63 | 157 | data[0] = cmd_write_user1[0]; |
jurica238814 | 0:cc2c5d49fe63 | 158 | |
jurica238814 | 0:cc2c5d49fe63 | 159 | if (i2c_->write(I2C_ADDR, data, 2) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 160 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 161 | } |
jurica238814 | 0:cc2c5d49fe63 | 162 | |
jurica238814 | 0:cc2c5d49fe63 | 163 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 164 | } |
jurica238814 | 0:cc2c5d49fe63 | 165 | |
jurica238814 | 0:cc2c5d49fe63 | 166 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 167 | int Si7006::getTemperature(int16_t *tempCx10){ |
jurica238814 | 0:cc2c5d49fe63 | 168 | if (i2c_->write(I2C_ADDR, cmd_meas_temp, sizeof(cmd_meas_temp)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 169 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 170 | } |
jurica238814 | 0:cc2c5d49fe63 | 171 | |
jurica238814 | 0:cc2c5d49fe63 | 172 | return checkTemperature(tempCx10); |
jurica238814 | 0:cc2c5d49fe63 | 173 | } |
jurica238814 | 0:cc2c5d49fe63 | 174 | |
jurica238814 | 0:cc2c5d49fe63 | 175 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 176 | int Si7006::getTemperature(float *tempC){ |
jurica238814 | 0:cc2c5d49fe63 | 177 | if (i2c_->write(I2C_ADDR, cmd_meas_temp, sizeof(cmd_meas_temp)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 178 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 179 | } |
jurica238814 | 0:cc2c5d49fe63 | 180 | |
jurica238814 | 0:cc2c5d49fe63 | 181 | return checkTemperature(tempC); |
jurica238814 | 0:cc2c5d49fe63 | 182 | } |
jurica238814 | 0:cc2c5d49fe63 | 183 | |
jurica238814 | 0:cc2c5d49fe63 | 184 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 185 | int Si7006::startTemperature(void){ |
jurica238814 | 0:cc2c5d49fe63 | 186 | if (i2c_->write(I2C_ADDR, cmd_meas_temp_no_hold, sizeof(cmd_meas_temp_no_hold)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 187 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 188 | } |
jurica238814 | 0:cc2c5d49fe63 | 189 | |
jurica238814 | 0:cc2c5d49fe63 | 190 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 191 | } |
jurica238814 | 0:cc2c5d49fe63 | 192 | |
jurica238814 | 0:cc2c5d49fe63 | 193 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 194 | int Si7006::checkTemperature(int16_t *tempCx10){ |
jurica238814 | 0:cc2c5d49fe63 | 195 | char data[3]; |
jurica238814 | 0:cc2c5d49fe63 | 196 | uint16_t code; |
jurica238814 | 0:cc2c5d49fe63 | 197 | int temp; |
jurica238814 | 0:cc2c5d49fe63 | 198 | |
jurica238814 | 0:cc2c5d49fe63 | 199 | if (i2c_->read(I2C_ADDR, data, 3) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 200 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 201 | } |
jurica238814 | 0:cc2c5d49fe63 | 202 | |
jurica238814 | 0:cc2c5d49fe63 | 203 | // Get 16-bit value from bytes read |
jurica238814 | 0:cc2c5d49fe63 | 204 | code = ((uint16_t)data[0] << 8) + data[1]; |
jurica238814 | 0:cc2c5d49fe63 | 205 | |
jurica238814 | 0:cc2c5d49fe63 | 206 | // Calculate the temperature using the formula from the datasheet |
jurica238814 | 0:cc2c5d49fe63 | 207 | // Scaled by 100 |
jurica238814 | 0:cc2c5d49fe63 | 208 | temp = ((((int)17572 * code) + 0x8000) >> 16) - 4685; |
jurica238814 | 0:cc2c5d49fe63 | 209 | |
jurica238814 | 0:cc2c5d49fe63 | 210 | // Return value is to be scaled by 10 |
jurica238814 | 0:cc2c5d49fe63 | 211 | *tempCx10 = (temp + 5) / 10; |
jurica238814 | 0:cc2c5d49fe63 | 212 | |
jurica238814 | 0:cc2c5d49fe63 | 213 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 214 | } |
jurica238814 | 0:cc2c5d49fe63 | 215 | |
jurica238814 | 0:cc2c5d49fe63 | 216 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 217 | int Si7006::checkTemperature(float *tempC){ |
jurica238814 | 0:cc2c5d49fe63 | 218 | char data[3]; |
jurica238814 | 0:cc2c5d49fe63 | 219 | uint16_t code; |
jurica238814 | 0:cc2c5d49fe63 | 220 | |
jurica238814 | 0:cc2c5d49fe63 | 221 | if (i2c_->read(I2C_ADDR, data, 3) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 222 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 223 | } |
jurica238814 | 0:cc2c5d49fe63 | 224 | |
jurica238814 | 0:cc2c5d49fe63 | 225 | // Get 16-bit value from bytes read |
jurica238814 | 0:cc2c5d49fe63 | 226 | code = ((uint16_t)data[0] << 8) + data[1]; |
jurica238814 | 0:cc2c5d49fe63 | 227 | |
jurica238814 | 0:cc2c5d49fe63 | 228 | // Calculate the temperature using the formula from the datasheet |
jurica238814 | 0:cc2c5d49fe63 | 229 | // Scaled by 100 |
jurica238814 | 0:cc2c5d49fe63 | 230 | *tempC = (((float)175.72 * code) / 65536) - 46.85; |
jurica238814 | 0:cc2c5d49fe63 | 231 | |
jurica238814 | 0:cc2c5d49fe63 | 232 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 233 | } |
jurica238814 | 0:cc2c5d49fe63 | 234 | |
jurica238814 | 0:cc2c5d49fe63 | 235 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 236 | int Si7006::getHumidity(int16_t *humidx10){ |
jurica238814 | 0:cc2c5d49fe63 | 237 | if (i2c_->write(I2C_ADDR, cmd_meas_humid, sizeof(cmd_meas_humid), true) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 238 | //return -1; |
jurica238814 | 0:cc2c5d49fe63 | 239 | } |
jurica238814 | 0:cc2c5d49fe63 | 240 | wait_ms(100); |
jurica238814 | 0:cc2c5d49fe63 | 241 | return checkHumidity(humidx10); |
jurica238814 | 0:cc2c5d49fe63 | 242 | } |
jurica238814 | 0:cc2c5d49fe63 | 243 | |
jurica238814 | 0:cc2c5d49fe63 | 244 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 245 | int Si7006::getHumidity(float *humid){ |
jurica238814 | 0:cc2c5d49fe63 | 246 | if (i2c_->write(I2C_ADDR, cmd_meas_humid, sizeof(cmd_meas_humid)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 247 | //return -1; |
jurica238814 | 0:cc2c5d49fe63 | 248 | } |
jurica238814 | 0:cc2c5d49fe63 | 249 | wait_ms(100); |
jurica238814 | 0:cc2c5d49fe63 | 250 | return checkHumidity(humid); |
jurica238814 | 0:cc2c5d49fe63 | 251 | } |
jurica238814 | 0:cc2c5d49fe63 | 252 | |
jurica238814 | 0:cc2c5d49fe63 | 253 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 254 | int Si7006::startHumidity(void){ |
jurica238814 | 0:cc2c5d49fe63 | 255 | if (i2c_->write(I2C_ADDR, cmd_meas_humid_no_hold, sizeof(cmd_meas_humid_no_hold)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 256 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 257 | } |
jurica238814 | 0:cc2c5d49fe63 | 258 | |
jurica238814 | 0:cc2c5d49fe63 | 259 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 260 | } |
jurica238814 | 0:cc2c5d49fe63 | 261 | |
jurica238814 | 0:cc2c5d49fe63 | 262 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 263 | int Si7006::checkHumidity(int16_t *humidx10){ |
jurica238814 | 0:cc2c5d49fe63 | 264 | char data[3]; |
jurica238814 | 0:cc2c5d49fe63 | 265 | uint16_t code; |
jurica238814 | 0:cc2c5d49fe63 | 266 | |
jurica238814 | 0:cc2c5d49fe63 | 267 | if (i2c_->read(I2C_ADDR, data, 3) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 268 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 269 | } |
jurica238814 | 0:cc2c5d49fe63 | 270 | |
jurica238814 | 0:cc2c5d49fe63 | 271 | // Get 16-bit value from bytes read |
jurica238814 | 0:cc2c5d49fe63 | 272 | code = ((uint16_t)data[0] << 8) + data[1]; |
jurica238814 | 0:cc2c5d49fe63 | 273 | |
jurica238814 | 0:cc2c5d49fe63 | 274 | // Calculate the humidity using the formula from the datasheet |
jurica238814 | 0:cc2c5d49fe63 | 275 | // Scaled by 10 |
jurica238814 | 0:cc2c5d49fe63 | 276 | *humidx10 = ((((int)1250 * code) + 0x8000) >> 16) - 60; |
jurica238814 | 0:cc2c5d49fe63 | 277 | |
jurica238814 | 0:cc2c5d49fe63 | 278 | // Check the crc |
jurica238814 | 0:cc2c5d49fe63 | 279 | char crc = crc8(data[0], 0x00); |
jurica238814 | 0:cc2c5d49fe63 | 280 | crc = crc8(data[1], crc); |
jurica238814 | 0:cc2c5d49fe63 | 281 | if(crc != data[2]) { |
jurica238814 | 0:cc2c5d49fe63 | 282 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 283 | } |
jurica238814 | 0:cc2c5d49fe63 | 284 | |
jurica238814 | 0:cc2c5d49fe63 | 285 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 286 | } |
jurica238814 | 0:cc2c5d49fe63 | 287 | |
jurica238814 | 0:cc2c5d49fe63 | 288 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 289 | int Si7006::checkHumidity(float *humid){ |
jurica238814 | 0:cc2c5d49fe63 | 290 | char data[3]; |
jurica238814 | 0:cc2c5d49fe63 | 291 | uint16_t code; |
jurica238814 | 0:cc2c5d49fe63 | 292 | |
jurica238814 | 0:cc2c5d49fe63 | 293 | if (i2c_->read(I2C_ADDR, data, 3) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 294 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 295 | } |
jurica238814 | 0:cc2c5d49fe63 | 296 | |
jurica238814 | 0:cc2c5d49fe63 | 297 | // Get 16-bit value from bytes read |
jurica238814 | 0:cc2c5d49fe63 | 298 | code = ((uint16_t)data[0] << 8) + data[1]; |
jurica238814 | 0:cc2c5d49fe63 | 299 | |
jurica238814 | 0:cc2c5d49fe63 | 300 | // Calculate the humidity using the formula from the datasheet |
jurica238814 | 0:cc2c5d49fe63 | 301 | *humid = (((float)125 * code) / 65536) - 6; |
jurica238814 | 0:cc2c5d49fe63 | 302 | |
jurica238814 | 0:cc2c5d49fe63 | 303 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 304 | } |
jurica238814 | 0:cc2c5d49fe63 | 305 | |
jurica238814 | 0:cc2c5d49fe63 | 306 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 307 | int Si7006::getPrevTemperature(float *tempC) { |
jurica238814 | 0:cc2c5d49fe63 | 308 | if (i2c_->write(I2C_ADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 309 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 310 | } |
jurica238814 | 0:cc2c5d49fe63 | 311 | if(checkTemperature(tempC) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 312 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 313 | } |
jurica238814 | 0:cc2c5d49fe63 | 314 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 315 | } |
jurica238814 | 0:cc2c5d49fe63 | 316 | |
jurica238814 | 0:cc2c5d49fe63 | 317 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 318 | int Si7006::getPrevTemperature(int16_t *tempCx10) { |
jurica238814 | 0:cc2c5d49fe63 | 319 | if (i2c_->write(I2C_ADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 320 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 321 | } |
jurica238814 | 0:cc2c5d49fe63 | 322 | if(checkTemperature(tempCx10) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 323 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 324 | } |
jurica238814 | 0:cc2c5d49fe63 | 325 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 326 | } |
jurica238814 | 0:cc2c5d49fe63 | 327 | |
jurica238814 | 0:cc2c5d49fe63 | 328 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 329 | int Si7006::getRev(char *rev){ |
jurica238814 | 0:cc2c5d49fe63 | 330 | if (i2c_->write(I2C_ADDR, cmd_fw_ver, sizeof(cmd_fw_ver)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 331 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 332 | } |
jurica238814 | 0:cc2c5d49fe63 | 333 | if (i2c_->read(I2C_ADDR, rev, 1) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 334 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 335 | } |
jurica238814 | 0:cc2c5d49fe63 | 336 | |
jurica238814 | 0:cc2c5d49fe63 | 337 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 338 | } |
jurica238814 | 0:cc2c5d49fe63 | 339 | |
jurica238814 | 0:cc2c5d49fe63 | 340 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 341 | int Si7006::heater(bool enable) |
jurica238814 | 0:cc2c5d49fe63 | 342 | { |
jurica238814 | 0:cc2c5d49fe63 | 343 | char data[2]; |
jurica238814 | 0:cc2c5d49fe63 | 344 | |
jurica238814 | 0:cc2c5d49fe63 | 345 | if (i2c_->write(I2C_ADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 346 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 347 | } |
jurica238814 | 0:cc2c5d49fe63 | 348 | |
jurica238814 | 0:cc2c5d49fe63 | 349 | if (i2c_->read(I2C_ADDR, &data[1], 1) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 350 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 351 | } |
jurica238814 | 0:cc2c5d49fe63 | 352 | |
jurica238814 | 0:cc2c5d49fe63 | 353 | if (enable) { |
jurica238814 | 0:cc2c5d49fe63 | 354 | data[1] |= 0x04; |
jurica238814 | 0:cc2c5d49fe63 | 355 | } else { |
jurica238814 | 0:cc2c5d49fe63 | 356 | data[1] &= ~0x04; |
jurica238814 | 0:cc2c5d49fe63 | 357 | } |
jurica238814 | 0:cc2c5d49fe63 | 358 | |
jurica238814 | 0:cc2c5d49fe63 | 359 | data[0] = cmd_write_user1[0]; |
jurica238814 | 0:cc2c5d49fe63 | 360 | |
jurica238814 | 0:cc2c5d49fe63 | 361 | if (i2c_->write(I2C_ADDR, data, 2) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 362 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 363 | } |
jurica238814 | 0:cc2c5d49fe63 | 364 | |
jurica238814 | 0:cc2c5d49fe63 | 365 | return 0; |
jurica238814 | 0:cc2c5d49fe63 | 366 | } |
jurica238814 | 0:cc2c5d49fe63 | 367 | |
jurica238814 | 0:cc2c5d49fe63 | 368 | //****************************************************************************** |
jurica238814 | 0:cc2c5d49fe63 | 369 | char Si7006::crc8(char value, char seed) |
jurica238814 | 0:cc2c5d49fe63 | 370 | { |
jurica238814 | 0:cc2c5d49fe63 | 371 | int i; |
jurica238814 | 0:cc2c5d49fe63 | 372 | |
jurica238814 | 0:cc2c5d49fe63 | 373 | for (i = 0; i < 8; i++) { |
jurica238814 | 0:cc2c5d49fe63 | 374 | |
jurica238814 | 0:cc2c5d49fe63 | 375 | if ((value ^ seed) & 0x80) { |
jurica238814 | 0:cc2c5d49fe63 | 376 | seed <<= 1; |
jurica238814 | 0:cc2c5d49fe63 | 377 | seed ^= POLYVAL; |
jurica238814 | 0:cc2c5d49fe63 | 378 | } else { |
jurica238814 | 0:cc2c5d49fe63 | 379 | seed <<= 1; |
jurica238814 | 0:cc2c5d49fe63 | 380 | } |
jurica238814 | 0:cc2c5d49fe63 | 381 | |
jurica238814 | 0:cc2c5d49fe63 | 382 | value <<= 1; |
jurica238814 | 0:cc2c5d49fe63 | 383 | } |
jurica238814 | 0:cc2c5d49fe63 | 384 | |
jurica238814 | 0:cc2c5d49fe63 | 385 | return seed; |
jurica238814 | 0:cc2c5d49fe63 | 386 | } |