Added extra features of the SHT25 sensor

This commit is contained in:
Antonio Lignan 2016-01-10 21:10:40 +01:00
parent 97a7e98ac5
commit d53a62187d
3 changed files with 162 additions and 51 deletions

View File

@ -60,13 +60,29 @@ PROCESS_THREAD(remote_sht25_process, ev, data)
PROCESS_BEGIN();
SENSORS_ACTIVATE(sht25);
/* Check if the sensor voltage operation is over 2.25V */
if(sht25.value(SHT25_VOLTAGE_ALARM)) {
printf("Voltage is lower than recommended for the sensor operation\n");
PROCESS_EXIT();
}
/* Configure the sensor for maximum resolution (14-bit temperature, 12-bit
* relative humidity), this will require up to 85ms for the temperature
* integration, and 29ms for the relative humidity (this is the default
* setting at power on). To achieve a faster integration time at the cost
* of a lower resolution, change the value below accordingly, see sht25.h.
*/
sht25.configure(SHT25_RESOLUTION, SHT2X_RES_14T_12RH);
/* Let it spin and read sensor data */
while(1) {
etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
temperature = sht25.value(SHT25_VAL_TEMP);
printf("Temperature %d.%d ºC\n", temperature / 100, temperature % 100);
printf("Temperature %02d.%02d ºC, ", temperature / 100, temperature % 100);
humidity = sht25.value(SHT25_VAL_HUM);
printf("Humidity %d.%d RH\n", humidity / 100, humidity % 100);
printf("Humidity %02d.%02d RH\n", humidity / 100, humidity % 100);
}
PROCESS_END();
}

View File

@ -46,7 +46,7 @@
#include "dev/sht25.h"
#include "lib/sensors.h"
/*---------------------------------------------------------------------------*/
#define DEBUG 0
#define DEBUG 1
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
@ -54,23 +54,7 @@
#endif
/*---------------------------------------------------------------------------*/
static uint8_t enabled;
/*---------------------------------------------------------------------------*/
static int
configure(int type, int value)
{
if(type != SENSORS_ACTIVE) {
PRINTF("SHT25: option not supported\n");
return SHT25_ERROR;
}
if(value) {
i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
I2C_SCL_NORMAL_BUS_SPEED);
}
enabled = value;
return SHT25_SUCCESS;
}
static uint8_t user_reg;
/*---------------------------------------------------------------------------*/
static int
status(int type)
@ -84,11 +68,15 @@ status(int type)
}
/*---------------------------------------------------------------------------*/
static uint16_t
sht25_read_reg(uint8_t reg, uint8_t *buf, uint8_t regNum)
sht25_read_reg(uint8_t reg, uint8_t *buf, uint8_t num)
{
if((buf == NULL) || (num <= 0)) {
return SHT25_ERROR;
}
i2c_master_enable();
if(i2c_single_send(SHT25_ADDR, reg) == I2C_MASTER_ERR_NONE) {
if(i2c_burst_receive(SHT25_ADDR, buf, regNum) == I2C_MASTER_ERR_NONE) {
if(i2c_burst_receive(SHT25_ADDR, buf, num) == I2C_MASTER_ERR_NONE) {
return SHT25_SUCCESS;
}
}
@ -100,7 +88,10 @@ sht25_convert(uint8_t variable, uint16_t value)
{
int16_t rd;
uint32_t buff;
buff = (uint32_t)value;
/* Clear the status bits */
buff = (uint32_t)(value & ~SHT25_STATUS_BITS_MASK);
if(variable == SHT25_VAL_TEMP) {
buff *= 17572;
buff = buff >> 16;
@ -114,7 +105,7 @@ sht25_convert(uint8_t variable, uint16_t value)
return rd;
}
/*---------------------------------------------------------------------------*/
static int16_t
static int
sht25_read(uint8_t variable, uint16_t *rd)
{
uint8_t buf[2];
@ -136,6 +127,32 @@ sht25_read(uint8_t variable, uint16_t *rd)
}
/*---------------------------------------------------------------------------*/
static int
sht25_write_reg(uint8_t *buf, uint8_t num)
{
if((buf == NULL) || (num <= 0)) {
PRINTF("SHT25: invalid write values\n");
return SHT25_ERROR;
}
i2c_master_enable();
if(i2c_burst_send(SHT25_ADDR, buf, num) == I2C_MASTER_ERR_NONE) {
return SHT25_SUCCESS;
}
return SHT25_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
sht25_read_user_register(void)
{
if(sht25_read_reg(SHT2X_UREG_READ, &user_reg, 1) == SHT25_SUCCESS) {
PRINTF("SHT25: user register 0x%02X\n", user_reg);
return SHT25_SUCCESS;
}
PRINTF("SHT25: failed to read user register\n");
return SHT25_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
value(int type)
{
uint16_t val;
@ -145,9 +162,78 @@ value(int type)
return SHT25_ERROR;
}
if(sht25_read(type, &val) == SHT25_SUCCESS) {
return val;
if((type != SHT25_VAL_TEMP) && (type != SHT25_VAL_HUM) &&
(type != SHT25_VOLTAGE_ALARM)) {
PRINTF("SHT25: invalid value requested\n");
return SHT25_ERROR;
}
if(type == SHT25_VOLTAGE_ALARM) {
if(sht25_read_user_register() == SHT25_SUCCESS) {
return ((user_reg & SHT2x_LOW_VOLTAGE_MASK) >> SHT2x_LOW_VOLTAGE_SHIFT);
}
} else {
if(sht25_read(type, &val) == SHT25_SUCCESS) {
return val;
}
}
return SHT25_ERROR;
}
/*---------------------------------------------------------------------------*/
static int
configure(int type, int value)
{
uint8_t buf[2];
if((type != SHT25_ACTIVE) && (type != SHT25_SOFT_RESET) &&
(type != SHT25_RESOLUTION)) {
PRINTF("SHT25: option not supported\n");
return SHT25_ERROR;
}
switch(type) {
case SHT25_ACTIVE:
if(value) {
i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
I2C_SCL_NORMAL_BUS_SPEED);
/* Read the user config register */
if(sht25_read_user_register() == SHT25_SUCCESS) {
enabled = value;
return SHT25_SUCCESS;
}
}
case SHT25_SOFT_RESET:
buf[0] = SHT2X_SOFT_RESET;
if(sht25_write_reg(&buf[0], 1) != SHT25_SUCCESS) {
PRINTF("SHT25: failed to reset the sensor\n");
return SHT25_ERROR;
}
clock_delay_usec(SHT25_RESET_DELAY);
return SHT25_SUCCESS;
case SHT25_RESOLUTION:
if((value != SHT2X_RES_14T_12RH) && (value != SHT2X_RES_12T_08RH) &&
(value != SHT2X_RES_13T_10RH) && (value != SHT2X_RES_11T_11RH)) {
PRINTF("SHT25: invalid resolution value\n");
return SHT25_ERROR;
}
user_reg &= ~SHT2X_RES_11T_11RH;
user_reg |= value;
buf[0] = SHT2X_UREG_WRITE;
buf[1] = user_reg;
if(sht25_write_reg(buf, 2) == SHT25_SUCCESS) {
PRINTF("SHT25: new user register value 0x%02X\n", user_reg);
return SHT25_SUCCESS;
}
default:
return SHT25_ERROR;
}
return SHT25_ERROR;
}
/*---------------------------------------------------------------------------*/

View File

@ -44,34 +44,43 @@
*/
/*---------------------------------------------------------------------------*/
#include "lib/sensors.h"
/* -------------------------------------------------------------------------- */
#ifndef SHT25_H_
#define SHT25_H_
/* -------------------------------------------------------------------------- */
#define SHT25_ADDR 0x40
#define SHT25_TEMP_HOLD 0xE3
#define SHT25_HUM_HOLD 0xE5
#define SHT25_TEMP_NO_HOLD 0xF3
#define SHT25_HUM_NO_HOLD 0xF5
#define SHT2X_UREG_WRITE 0xE6
#define SHT2X_UREG_READ 0xE7
#define SHT2X_SOFT_RESET 0XFE
#define SHT2X_NULL 0x00
/* -------------------------------------------------------------------------- */
#define SHT2X_RES_14T_12RH 0x00
#define SHT2X_RES_12T_08RH 0x01
#define SHT2X_RES_13T_10RH 0x80
#define SHT2X_RES_11T_11RH 0x81
#define SHT2X_HEATER_ON 0x04
#define SHT2X_HEATER_OFF 0x00
#define SHT2X_OTP_RELOAD_EN 0x00
#define SHT2X_OTP_RELOAD_DIS 0x02
#define SHT2x_LOW_VOLTAGE_MASK 0x40
#define SHT2x_LOW_VOLTAGE_SHIFT 0x06
/* -------------------------------------------------------------------------- */
#define SHT25_ACTIVE SENSORS_ACTIVE
#define SHT25_SOFT_RESET 0x01
#define SHT25_RESOLUTION 0x02
/* -------------------------------------------------------------------------- */
#define SHT25_ADDR 0x40
#define SHT25_TEMP_HOLD 0xE3
#define SHT25_HUM_HOLD 0xE5
#define SHT25_TEMP_NO_HOLD 0xF3
#define SHT25_HUM_NO_HOLD 0xF5
#define SHT2X_UREG_WRITE 0xE6
#define SHT2X_UREG_READ 0xE7
#define SHT2X_SOFT_RESET 0XFE
#define SHT2X_NULL 0x00
/* -------------------------------------------------------------------------- */
#define SHT2X_RES_14T_12RH 0x00
#define SHT2X_RES_12T_08RH 0x01
#define SHT2X_RES_13T_10RH 0x80
#define SHT2X_RES_11T_11RH 0x81
#define SHT2X_HEATER_ON 0x04
#define SHT2X_HEATER_OFF 0x00
#define SHT2X_OTP_RELOAD_EN 0x00
#define SHT2X_OTP_RELOAD_DIS 0x02
/* -------------------------------------------------------------------------- */
#define SHT25_VAL_TEMP SHT25_TEMP_HOLD
#define SHT25_VAL_HUM SHT25_HUM_HOLD
#define SHT25_ERROR -1
#define SHT25_SUCCESS 0x00
#define SHT25_VAL_TEMP SHT25_TEMP_HOLD
#define SHT25_VAL_HUM SHT25_HUM_HOLD
#define SHT25_VOLTAGE_ALARM 0x01
#define SHT25_ERROR (-1)
#define SHT25_SUCCESS 0x00
#define SHT25_RESET_DELAY 15000
#define SHT25_STATUS_BITS_MASK 0x0003
/* -------------------------------------------------------------------------- */
#define SHT25_SENSOR "SHT25 Sensor"
/* -------------------------------------------------------------------------- */