46 #include "lib/sensors.h" 50 #define PRINTF(...) printf(__VA_ARGS__) 58 #define SHT21_ADDRESS (0x40) 65 #define SHT21_USER_REG_READ (0xE7) 66 #define SHT21_USER_REG_WRITE (0xE6) 67 #define SHT21_USER_REG_RESERVED_BITS (0x38) 69 #define SHT21_TEMPERATURE_HM_CMD (0xE3) 70 #define SHT21_HUMIDITY_HM_CMD (0xE5) 71 #define SHT21_TEMPERATURE_NHM_CMD (0xF3) 72 #define SHT21_HUMIDITY_NHM_CMD (0xF5) 73 #define SHT21_RESET_CMD (0xFE) 75 #define SHT21_STATUS_MASK (0xFC) 77 #define SHT21_RESOLUTION_12b_14b ((0 << 7) | (0 << 0)) 78 #define SHT21_RESOLUTION_8b_12b ((0 << 7) | (1 << 0)) 79 #define SHT21_RESOLUTION_10b_13b ((1 << 7) | (0 << 0)) 80 #define SHT21_RESOLUTION_11b_11b ((1 << 7) | (1 << 0)) 81 #define SHT21_BATTERY_ABOVE_2V25 (0 << 6) 82 #define SHT21_BATTERY_BELOW_2V25 (1 << 6) 83 #define SHT21_ONCHIP_HEATER_ENABLE (1 << 2) 84 #define SHT21_ONCHIP_HEATER_DISABLE (0 << 2) 85 #define SHT21_OTP_RELOAD_ENABLE (0 << 1) 86 #define SHT21_OTP_RELOAD_DISABLE (1 << 1) 93 #define SHT21_DEFAULT_CONFIG (SHT21_RESOLUTION_12b_14b | \ 94 SHT21_ONCHIP_HEATER_DISABLE | \ 95 SHT21_BATTERY_ABOVE_2V25 | \ 96 SHT21_OTP_RELOAD_DISABLE) 98 #define SHT21_USER_CONFIG (SHT21_RESOLUTION_12b_14b | \ 99 SHT21_ONCHIP_HEATER_DISABLE | \ 100 SHT21_BATTERY_ABOVE_2V25 | \ 101 SHT21_OTP_RELOAD_DISABLE) 104 static uint8_t enabled;
113 config[0] = SHT21_USER_REG_WRITE;
121 config[1] &= SHT21_USER_REG_RESERVED_BITS;
124 config[1] |= SHT21_USER_CONFIG;
137 sht21_is_present(
void)
145 if(status != I2C_MASTER_ERR_NONE) {
150 is_present &= ~SHT21_USER_REG_RESERVED_BITS;
152 return (is_present == SHT21_USER_CONFIG) || (is_present == SHT21_DEFAULT_CONFIG);
156 sht21_read_temperature(
void)
158 uint8_t sht21_temperature[2];
159 uint16_t temperature;
165 temperature = (sht21_temperature[0] << 8) | ((sht21_temperature[1] & SHT21_STATUS_MASK));
171 sht21_convert_temperature(uint32_t temperature)
175 temperature *= 17572;
176 temperature = temperature >> 16;
177 result = (int16_t)temperature - 4685;
183 sht21_read_humidity(
void)
185 uint8_t sht21_humidity[2];
192 humidity = (sht21_humidity[0] << 8) | ((sht21_humidity[1] & SHT21_STATUS_MASK));
198 sht21_convert_humidity(uint32_t humidity)
203 humidity = humidity >> 16;
204 result = (int16_t)humidity - 600;
205 result = (result > 10000) ? 10000 : result;
227 PRINTF(
"SHT21: sensor not started\n");
231 if(type == SHT21_READ_RAW_TEMP) {
232 return sht21_read_temperature();
233 }
else if(type == SHT21_READ_RAW_RHUM) {
234 return sht21_read_humidity();
235 }
else if(type == SHT21_READ_TEMP) {
236 value = sht21_read_temperature();
237 return sht21_convert_temperature(value);
238 }
else if(type == SHT21_READ_RHUM) {
239 value = sht21_read_humidity();
240 return sht21_convert_humidity(value);
242 PRINTF(
"SHT21: invalid value requested\n");
248 configure(
int type,
int value)
250 if(type == SHT21_ACTIVATE) {
251 if(!sht21_is_present()) {
252 PRINTF(
"SHT21: is not present\n");
257 return SHT21_SUCCESS;
261 if(type == SHT21_RESET && enabled) {
263 return SHT21_SUCCESS;
265 PRINTF(
"SHT21: is not enabled\n");
272 SENSORS_SENSOR(sht21, SHT21_SENSOR, value, configure, status);
uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to send multiple bytes to a slave.
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
Header file for the SHT21 temperature and humidity sensor driver.
uint8_t i2c_single_receive(uint8_t slave_addr, uint8_t *data)
Perform all operations to receive a byte from a slave.
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.