Library for Silicon Labs Si7006 digital temperature and humidity chip.
Dependents: acd52832_Humidity_Temp_Example iBeacon acnsensa acnSENSA
Si7006A20.cpp@2:7c3c7db34528, 2017-09-26 (annotated)
- Committer:
- jurica238814
- Date:
- Tue Sep 26 12:04:12 2017 +0000
- Revision:
- 2:7c3c7db34528
- Parent:
- 1:155934570c14
I2C object replaced by I2C reference.
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 | 1:155934570c14 | 15 | ******************************************************************************* |
jurica238814 | 1:155934570c14 | 16 | */ |
jurica238814 | 1:155934570c14 | 17 | |
jurica238814 | 1:155934570c14 | 18 | /* |
jurica238814 | 1:155934570c14 | 19 | * The library is modified to suite Si7006A20 Humidity and Temperature Sensor |
jurica238814 | 1:155934570c14 | 20 | * by Jurica Resetar @ aconno |
jurica238814 | 0:cc2c5d49fe63 | 21 | * More info and contact: aconno.de |
jurica238814 | 1:155934570c14 | 22 | * jurica_resetar@yahoo.com |
jurica238814 | 0:cc2c5d49fe63 | 23 | * |
jurica238814 | 0:cc2c5d49fe63 | 24 | */ |
jurica238814 | 0:cc2c5d49fe63 | 25 | |
jurica238814 | 0:cc2c5d49fe63 | 26 | #include "Si7006A20.h" |
jurica238814 | 1:155934570c14 | 27 | #include "aconno_i2c.h" |
jurica238814 | 0:cc2c5d49fe63 | 28 | |
jurica238814 | 1:155934570c14 | 29 | #define TEMPERATURE_DELAY_MS (10) |
jurica238814 | 1:155934570c14 | 30 | #define HUMIDITY_DELAY_MS (20) |
jurica238814 | 0:cc2c5d49fe63 | 31 | |
jurica238814 | 1:155934570c14 | 32 | char cmd_meas_humid = 0xE5; |
jurica238814 | 1:155934570c14 | 33 | char cmd_meas_humid_no_hold = 0xF5; |
jurica238814 | 1:155934570c14 | 34 | char cmd_meas_temp = 0xE3; |
jurica238814 | 1:155934570c14 | 35 | char cmd_meas_temp_no_hold = 0xF3; |
jurica238814 | 1:155934570c14 | 36 | char cmd_meas_prev_temp = 0xE0; |
jurica238814 | 1:155934570c14 | 37 | char cmd_rst = 0xFE; |
jurica238814 | 1:155934570c14 | 38 | char cmd_write_user1 = 0xE6; |
jurica238814 | 1:155934570c14 | 39 | char cmd_read_user1 = 0xE7; |
jurica238814 | 1:155934570c14 | 40 | char cmd_id_1[] = {0xFA, 0x0F}; |
jurica238814 | 1:155934570c14 | 41 | char cmd_id_2[] = {0xFC, 0xC9}; |
jurica238814 | 1:155934570c14 | 42 | char cmd_fw_ver[] = {0x84, 0xB8}; |
jurica238814 | 0:cc2c5d49fe63 | 43 | |
jurica238814 | 2:7c3c7db34528 | 44 | |
jurica238814 | 2:7c3c7db34528 | 45 | Si7006::Si7006(I2C *i2c) : i2c(i2c, char(I2C_ADDR)){ |
jurica238814 | 2:7c3c7db34528 | 46 | |
jurica238814 | 0:cc2c5d49fe63 | 47 | } |
jurica238814 | 0:cc2c5d49fe63 | 48 | |
jurica238814 | 1:155934570c14 | 49 | uint8_t Si7006::getElectronicId(char* id){ |
jurica238814 | 1:155934570c14 | 50 | uint8_t success; |
jurica238814 | 1:155934570c14 | 51 | |
jurica238814 | 1:155934570c14 | 52 | success = i2c.sendCommand(cmd_id_1, sizeof(cmd_id_1), id, 4); |
jurica238814 | 1:155934570c14 | 53 | success = i2c.sendCommand(cmd_id_2, sizeof(cmd_id_2), (id+4), 4); |
jurica238814 | 1:155934570c14 | 54 | |
jurica238814 | 1:155934570c14 | 55 | return success; |
jurica238814 | 0:cc2c5d49fe63 | 56 | } |
jurica238814 | 0:cc2c5d49fe63 | 57 | |
jurica238814 | 0:cc2c5d49fe63 | 58 | int Si7006::configResolution(Si7006::resolution_t resolution){ |
jurica238814 | 1:155934570c14 | 59 | char data; |
jurica238814 | 1:155934570c14 | 60 | |
jurica238814 | 1:155934570c14 | 61 | // Read User1 Register |
jurica238814 | 1:155934570c14 | 62 | i2c.readFromReg(cmd_read_user1, &data, 1); |
jurica238814 | 0:cc2c5d49fe63 | 63 | switch (resolution) { |
jurica238814 | 0:cc2c5d49fe63 | 64 | case RH_12b_TEMP_14b: |
jurica238814 | 1:155934570c14 | 65 | data &= ~0x81; |
jurica238814 | 0:cc2c5d49fe63 | 66 | break; |
jurica238814 | 0:cc2c5d49fe63 | 67 | case RH_8b_TEMP_12b: |
jurica238814 | 1:155934570c14 | 68 | data = (data & ~0x80) | 0x01; |
jurica238814 | 0:cc2c5d49fe63 | 69 | break; |
jurica238814 | 0:cc2c5d49fe63 | 70 | case RH_10b_TEMP_13b: |
jurica238814 | 1:155934570c14 | 71 | data = (data & ~0x01) | 0x80; |
jurica238814 | 0:cc2c5d49fe63 | 72 | break; |
jurica238814 | 0:cc2c5d49fe63 | 73 | case RH_11b_TEMP_11b: |
jurica238814 | 1:155934570c14 | 74 | data |= 0x81; |
jurica238814 | 0:cc2c5d49fe63 | 75 | break; |
jurica238814 | 0:cc2c5d49fe63 | 76 | default: |
jurica238814 | 0:cc2c5d49fe63 | 77 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 78 | } |
jurica238814 | 0:cc2c5d49fe63 | 79 | |
jurica238814 | 1:155934570c14 | 80 | return i2c.writeToReg(cmd_write_user1, &data, 1); |
jurica238814 | 0:cc2c5d49fe63 | 81 | } |
jurica238814 | 0:cc2c5d49fe63 | 82 | |
jurica238814 | 1:155934570c14 | 83 | float Si7006::getTemperature(){ |
jurica238814 | 1:155934570c14 | 84 | char temper[2]; |
jurica238814 | 1:155934570c14 | 85 | |
jurica238814 | 1:155934570c14 | 86 | i2c.sendCommand(&cmd_meas_temp_no_hold, sizeof(cmd_meas_temp_no_hold)); |
jurica238814 | 1:155934570c14 | 87 | wait_ms(TEMPERATURE_DELAY_MS); |
jurica238814 | 1:155934570c14 | 88 | i2c.readBus(temper, 2); |
jurica238814 | 0:cc2c5d49fe63 | 89 | |
jurica238814 | 1:155934570c14 | 90 | return calculateTemperature(temper); |
jurica238814 | 0:cc2c5d49fe63 | 91 | } |
jurica238814 | 0:cc2c5d49fe63 | 92 | |
jurica238814 | 1:155934570c14 | 93 | float Si7006::calculateTemperature(char *rawTemp){ |
jurica238814 | 1:155934570c14 | 94 | float temperature; |
jurica238814 | 1:155934570c14 | 95 | int16_t code; |
jurica238814 | 0:cc2c5d49fe63 | 96 | |
jurica238814 | 1:155934570c14 | 97 | code = (uint16_t)((*(rawTemp+0)<<8) + (*(rawTemp+1)<<0)); |
jurica238814 | 1:155934570c14 | 98 | temperature = (((float)175.72 * code) / 65536.0f) - 46.85; |
jurica238814 | 1:155934570c14 | 99 | |
jurica238814 | 1:155934570c14 | 100 | //return temperature; |
jurica238814 | 1:155934570c14 | 101 | return temperature; |
jurica238814 | 0:cc2c5d49fe63 | 102 | } |
jurica238814 | 0:cc2c5d49fe63 | 103 | |
jurica238814 | 1:155934570c14 | 104 | float Si7006::getHumidity(){ |
jurica238814 | 1:155934570c14 | 105 | char humid[2]; |
jurica238814 | 1:155934570c14 | 106 | |
jurica238814 | 1:155934570c14 | 107 | i2c.sendCommand(&cmd_meas_humid, sizeof(cmd_meas_humid)); |
jurica238814 | 1:155934570c14 | 108 | wait_ms(HUMIDITY_DELAY_MS); |
jurica238814 | 1:155934570c14 | 109 | i2c.readBus(humid, 2); |
jurica238814 | 1:155934570c14 | 110 | |
jurica238814 | 0:cc2c5d49fe63 | 111 | return checkHumidity(humid); |
jurica238814 | 0:cc2c5d49fe63 | 112 | } |
jurica238814 | 0:cc2c5d49fe63 | 113 | |
jurica238814 | 1:155934570c14 | 114 | float Si7006::checkHumidity(char *rawHumidity){ |
jurica238814 | 1:155934570c14 | 115 | float humidity; |
jurica238814 | 0:cc2c5d49fe63 | 116 | uint16_t code; |
jurica238814 | 0:cc2c5d49fe63 | 117 | |
jurica238814 | 0:cc2c5d49fe63 | 118 | // Get 16-bit value from bytes read |
jurica238814 | 1:155934570c14 | 119 | code = (uint16_t)((*(rawHumidity+0)<<8) + (*(rawHumidity+1)<<0)); |
jurica238814 | 0:cc2c5d49fe63 | 120 | // Calculate the humidity using the formula from the datasheet |
jurica238814 | 1:155934570c14 | 121 | humidity = ((((float)125 * code)) / 65536.0f) - 6; |
jurica238814 | 0:cc2c5d49fe63 | 122 | |
jurica238814 | 1:155934570c14 | 123 | return humidity; |
jurica238814 | 0:cc2c5d49fe63 | 124 | } |
jurica238814 | 0:cc2c5d49fe63 | 125 | |
jurica238814 | 0:cc2c5d49fe63 | 126 | int Si7006::getPrevTemperature(float *tempC) { |
jurica238814 | 1:155934570c14 | 127 | /* |
jurica238814 | 1:155934570c14 | 128 | if (i2c.write(i2cADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 129 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 130 | } |
jurica238814 | 0:cc2c5d49fe63 | 131 | if(checkTemperature(tempC) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 132 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 133 | } |
jurica238814 | 0:cc2c5d49fe63 | 134 | return 0; |
jurica238814 | 1:155934570c14 | 135 | */ |
jurica238814 | 0:cc2c5d49fe63 | 136 | } |
jurica238814 | 0:cc2c5d49fe63 | 137 | |
jurica238814 | 0:cc2c5d49fe63 | 138 | int Si7006::getPrevTemperature(int16_t *tempCx10) { |
jurica238814 | 1:155934570c14 | 139 | /* |
jurica238814 | 1:155934570c14 | 140 | if (i2c.write(i2cADDR, cmd_meas_prev_temp, sizeof(cmd_meas_prev_temp)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 141 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 142 | } |
jurica238814 | 0:cc2c5d49fe63 | 143 | if(checkTemperature(tempCx10) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 144 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 145 | } |
jurica238814 | 0:cc2c5d49fe63 | 146 | return 0; |
jurica238814 | 1:155934570c14 | 147 | */ |
jurica238814 | 0:cc2c5d49fe63 | 148 | } |
jurica238814 | 0:cc2c5d49fe63 | 149 | |
jurica238814 | 0:cc2c5d49fe63 | 150 | int Si7006::getRev(char *rev){ |
jurica238814 | 1:155934570c14 | 151 | /* |
jurica238814 | 1:155934570c14 | 152 | if (i2c.write(i2cADDR, cmd_fw_ver, sizeof(cmd_fw_ver)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 153 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 154 | } |
jurica238814 | 1:155934570c14 | 155 | if (i2c.read(i2cADDR, rev, 1) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 156 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 157 | } |
jurica238814 | 0:cc2c5d49fe63 | 158 | |
jurica238814 | 0:cc2c5d49fe63 | 159 | return 0; |
jurica238814 | 1:155934570c14 | 160 | */ |
jurica238814 | 0:cc2c5d49fe63 | 161 | } |
jurica238814 | 0:cc2c5d49fe63 | 162 | |
jurica238814 | 1:155934570c14 | 163 | int Si7006::heater(bool enable){ |
jurica238814 | 0:cc2c5d49fe63 | 164 | char data[2]; |
jurica238814 | 1:155934570c14 | 165 | /* |
jurica238814 | 1:155934570c14 | 166 | if (i2c.write(i2cADDR, cmd_read_user1, sizeof(cmd_read_user1)) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 167 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 168 | } |
jurica238814 | 0:cc2c5d49fe63 | 169 | |
jurica238814 | 1:155934570c14 | 170 | if (i2c.read(i2cADDR, &data[1], 1) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 171 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 172 | } |
jurica238814 | 0:cc2c5d49fe63 | 173 | |
jurica238814 | 0:cc2c5d49fe63 | 174 | if (enable) { |
jurica238814 | 0:cc2c5d49fe63 | 175 | data[1] |= 0x04; |
jurica238814 | 0:cc2c5d49fe63 | 176 | } else { |
jurica238814 | 0:cc2c5d49fe63 | 177 | data[1] &= ~0x04; |
jurica238814 | 0:cc2c5d49fe63 | 178 | } |
jurica238814 | 0:cc2c5d49fe63 | 179 | |
jurica238814 | 0:cc2c5d49fe63 | 180 | data[0] = cmd_write_user1[0]; |
jurica238814 | 0:cc2c5d49fe63 | 181 | |
jurica238814 | 1:155934570c14 | 182 | if (i2c.write(i2cADDR, data, 2) != 0) { |
jurica238814 | 0:cc2c5d49fe63 | 183 | return -1; |
jurica238814 | 0:cc2c5d49fe63 | 184 | } |
jurica238814 | 0:cc2c5d49fe63 | 185 | |
jurica238814 | 0:cc2c5d49fe63 | 186 | return 0; |
jurica238814 | 1:155934570c14 | 187 | */ |
jurica238814 | 0:cc2c5d49fe63 | 188 | } |
jurica238814 | 0:cc2c5d49fe63 | 189 | |
jurica238814 | 1:155934570c14 | 190 | char Si7006::crc8(char value, char seed){ |
jurica238814 | 0:cc2c5d49fe63 | 191 | int i; |
jurica238814 | 0:cc2c5d49fe63 | 192 | |
jurica238814 | 0:cc2c5d49fe63 | 193 | for (i = 0; i < 8; i++) { |
jurica238814 | 0:cc2c5d49fe63 | 194 | |
jurica238814 | 0:cc2c5d49fe63 | 195 | if ((value ^ seed) & 0x80) { |
jurica238814 | 0:cc2c5d49fe63 | 196 | seed <<= 1; |
jurica238814 | 0:cc2c5d49fe63 | 197 | seed ^= POLYVAL; |
jurica238814 | 0:cc2c5d49fe63 | 198 | } else { |
jurica238814 | 0:cc2c5d49fe63 | 199 | seed <<= 1; |
jurica238814 | 0:cc2c5d49fe63 | 200 | } |
jurica238814 | 0:cc2c5d49fe63 | 201 | value <<= 1; |
jurica238814 | 0:cc2c5d49fe63 | 202 | } |
jurica238814 | 0:cc2c5d49fe63 | 203 | return seed; |
jurica238814 | 0:cc2c5d49fe63 | 204 | } |