BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Data Structures | Macros | Typedefs | Enumerations | Functions
sht21.h File Reference

Interface to the Sensirion SHT21 Digital Humidity Sensor or the Measurement Specialties HTU21D clone. More...

#include <bsp430/serial.h>
#include <bsp430/periph/timer.h>

Go to the source code of this file.

Data Structures

struct  sBSP430sensorsSHT21sample
 

Macros

#define BSP430_SENSORS_SHT21_I2C_ADDRESS   0x40
 
#define BSP430_SENSORS_SHT21_RESET_DELAY_MS   15
 
#define BSP430_SENSORS_SHT21_CONFIG_H12T14   0x00
 
#define BSP430_SENSORS_SHT21_CONFIG_H8T12   0x01
 
#define BSP430_SENSORS_SHT21_CONFIG_H10T13   0x80
 
#define BSP430_SENSORS_SHT21_CONFIG_H11T11   0x81
 
#define BSP430_SENSORS_SHT21_CONFIG_RESOLUTION_MASK   0x81
 
#define BSP430_SENSORS_SHT21_CONFIG_EOB_MASK   0x40
 
#define BSP430_SENSORS_SHT21_CONFIG_HEATER_MASK   0x04
 
#define BSP430_SENSORS_SHT21_EIC_LENGTH   8
 
#define BSP430_SENSORS_SHT21_EIC_IS_SHT21(eic_)   ((0x00 == (eic_)[0]) && (0x80 == (eic_)[1]))
 
#define BSP430_SENSORS_SHT21_EIC_IS_HTU21D(eic_)   ((0x48 == (eic_)[0]) && (0x54 == (eic_)[1]))
 
#define BSP430_SENSORS_SHT21_HUMIDITY_RAW_TO_ppth(raw_)   (unsigned int)(((1250UL * (raw_)) >> 16) - 60)
 
#define BSP430_SENSORS_SHT21_TEMPERATURE_RAW_TO_cK(raw_)   (22630U + (unsigned int)((17572UL * (raw_)) >> 16))
 
#define BSP430_SENSORS_SHT21_TEMPERATURE_RAW_TO_dK(raw_)   ((unsigned int)((5 + BSP430_SENSORS_SHT21_TEMPERATURE_RAW_TO_cK(raw_)) / 10))
 
#define BSP430_SENSORS_SHT21_IS_HTU21D   1
 

Typedefs

typedef struct sBSP430sensorsSHT21sample sBSP430sensorsSHT21sample
 
typedef sBSP430sensorsSHT21samplehBSP430sensorsSHT21sample
 
typedef enum eBSP430sensorsSHT21measurement eBSP430sensorsSHT21measurement
 

Enumerations

enum  eBSP430sensorsSHT21measurement { eBSP430sensorsSHT21measurement_HUMIDITY, eBSP430sensorsSHT21measurement_TEMPERATURE }
 

Functions

int iBSP430sensorsSHT21crc (const uint8_t *data, int len)
 
int iBSP430sensorsSHT21eic (hBSP430halSERIAL i2c, uint8_t *eic)
 
int iBSP430sensorsSHT21configuration (hBSP430halSERIAL i2c, int resolution, int heater)
 
int iBSP430sensorsSHT21reset (hBSP430halSERIAL i2c)
 
int iBSP430sensorsSHT21initiateMeasurement (hBSP430halSERIAL i2c, int hold_master, eBSP430sensorsSHT21measurement type)
 
int iBSP430sensorsSHT21getMeasurement (hBSP430halSERIAL i2c, int hold_master, uint16_t *rawp)
 
int iBSP430sensorsSHT21getSample (hBSP430halSERIAL i2c, hBSP430sensorsSHT21sample sample)
 

Detailed Description

Interface to the Sensirion SHT21 Digital Humidity Sensor or the Measurement Specialties HTU21D clone.

This device measures relative humidity and temperature.

To use this with BSP430 you will need an I2C device. See examples/sensors/bmp085 for an example of how to use it.

Note
For all operations that involve I2C, the caller must hold the i2c resource. For most functions the following conditions apply at the time the function is invoked:
  • i2c may be in reset mode on entry, in which case it will be put back into reset mode before returning. i2c must not be in hold mode .
  • The slave address of the i2c resource will be reconfigured as a side effect of calling the function.
When hold_master is used in iBSP430sensorsSHT21initiateMeasurement() the I2C bus is not restored to reset mode on return. The caller must do so, after successful completion of iBSP430sensorsSHT21getMeasurement(), if desired.
Homepage
http://github.com/pabigot/bsp430

Macro Definition Documentation

#define BSP430_SENSORS_SHT21_CONFIG_EOB_MASK   0x40

Bits that represent end-of-battery state in the value returned by iBSP430sensorsSHT21configuration().

#define BSP430_SENSORS_SHT21_CONFIG_H10T13   0x80

resolution parameter to iBSP430sensorsSHT21configuration() to configure for 10-bit humidity and 13-bit temperature measurements.

#define BSP430_SENSORS_SHT21_CONFIG_H11T11   0x81

resolution parameter to iBSP430sensorsSHT21configuration() to configure for 11-bit humidity and 11-bit temperature measurements.

#define BSP430_SENSORS_SHT21_CONFIG_H12T14   0x00

resolution parameter to iBSP430sensorsSHT21configuration() to configure for 12-bit humidity and 14-bit temperature measurements.

#define BSP430_SENSORS_SHT21_CONFIG_H8T12   0x01

resolution parameter to iBSP430sensorsSHT21configuration() to configure for 8-bit humidity and 12-bit temperature measurements.

#define BSP430_SENSORS_SHT21_CONFIG_HEATER_MASK   0x04

Bit that represents on-chip heater enabled state in the value returned by iBSP430sensorsSHT21configuration().

#define BSP430_SENSORS_SHT21_CONFIG_RESOLUTION_MASK   0x81

Bits that represent measurement resolution in the value returned by iBSP430sensorsSHT21configuration().

#define BSP430_SENSORS_SHT21_EIC_IS_HTU21D (   eic_)    ((0x48 == (eic_)[0]) && (0x54 == (eic_)[1]))

Assess whether an EIC represents a true HTU21D device.

#define BSP430_SENSORS_SHT21_EIC_IS_SHT21 (   eic_)    ((0x00 == (eic_)[0]) && (0x80 == (eic_)[1]))

Assess whether an EIC represents a true SHT21 device.

#define BSP430_SENSORS_SHT21_EIC_LENGTH   8

Number of octets in the SHT21 Electronic Identification Code

#define BSP430_SENSORS_SHT21_HUMIDITY_RAW_TO_ppth (   raw_)    (unsigned int)(((1250UL * (raw_)) >> 16) - 60)

Convert a raw humidity value to parts-per-thousand as an unsigned int

#define BSP430_SENSORS_SHT21_I2C_ADDRESS   0x40

The 7-bit I2C slave address for the device. This is not configurable.

#define BSP430_SENSORS_SHT21_IS_HTU21D   1

Define to true value to indicate that device is HTU21D as opposed to a real SHT21.

The HTU21D has a significantly less stable I2C interface: in particular, bus errors occur in if a measurement read request arrives too soon after the measurement initialization.

When enabled, hard-coded delays are introduced at certain points, decreasing but not eliminating the likelihood of bus failures.

#define BSP430_SENSORS_SHT21_RESET_DELAY_MS   15

The maximum time required by the sensor to stabilize after iBSP430sensorsSHT21reset(), in milliseconds.

#define BSP430_SENSORS_SHT21_TEMPERATURE_RAW_TO_cK (   raw_)    (22630U + (unsigned int)((17572UL * (raw_)) >> 16))

Convert a raw temperature value to centi-degrees Kelvin as an unsigned int

#define BSP430_SENSORS_SHT21_TEMPERATURE_RAW_TO_dK (   raw_)    ((unsigned int)((5 + BSP430_SENSORS_SHT21_TEMPERATURE_RAW_TO_cK(raw_)) / 10))

Convert a raw temperature value to deci-degrees Kelvin as an unsigned int.

Typedef Documentation

Silly two-valued enumeration to avoid using literal integers (e.g. 0 and 1) to select between the available measurement types.

A handle for a SHT21 sample

Data defining a SHT21 sample.

Enumeration Type Documentation

Silly two-valued enumeration to avoid using literal integers (e.g. 0 and 1) to select between the available measurement types.

Enumerator
eBSP430sensorsSHT21measurement_HUMIDITY 

Measurement of relative humidity

eBSP430sensorsSHT21measurement_TEMPERATURE 

Measurement of temperature

Function Documentation

int iBSP430sensorsSHT21configuration ( hBSP430halSERIAL  i2c,
int  resolution,
int  heater 
)

Read and update the user register.

The user register controls the measurement resolution and whether the on-chip heater is enabled. This function reads the register, calculates a new setting as requested by resolution and heater, and if the setting has changed writes the new setting. The value that was read originally is returned. Bits unrelated to measurement resolution and on-chip heater are left unchanged.

The resolution of the device is configured by passing resolution with one of the values in the following table:

resolution RH Res (bits) Temp Res (bits)
BSP430_SENSORS_SHT21_CONFIG_H8T12 8 12
BSP430_SENSORS_SHT21_CONFIG_H10T13 10 13
BSP430_SENSORS_SHT21_CONFIG_H11T11 11 11
BSP430_SENSORS_SHT21_CONFIG_H12T14 12 14
Parameters
i2cthe I2C bus on which the SHT21 device can be contacted
resolutionControl the resolution of temperature and humidity measurements. Values in the table above (which are all non-negative) are accepted. Negative values will result in no change to the configuration; unrecognized non-negative values produce an error.
heaterA positive value to turn the heater on, zero to turn the heater off, a negative value to leave the heater at its current setting.
Returns
The non-negative eight-bit register value, or a negative error code.
int iBSP430sensorsSHT21crc ( const uint8_t *  data,
int  len 
)

Calculate the Cyclic Redundancy Checksum over a range of data.

This uses the Sensirion polynomial as described in the CRC Calculation Humidity Sensor SHT2x application note to calculate an 8-bit checksum over the provided data.

Parameters
datathe sequence of octets to be checked
lenthe number of octets in the sequence
Returns
the 8-bit checksum, which is zero if the data terminates with the expected checksum of the preceding octets.
int iBSP430sensorsSHT21eic ( hBSP430halSERIAL  i2c,
uint8_t *  eic 
)

Read the 64-bit electronic identification code from the chip.

This is a unique identifier comprising 16-bit, 32-bit, and 16-bit components in MSB sequence. No information is provided about the internal structure of the components. See the SHT2x Electronic Identification Code application node.

Parameters
i2cthe I2C bus on which the SHT21 device can be contacted
eicpointer to an BSP430_SENSORS_SHT21_EIC_LENGTH-octet buffer into which the retrieved identifier may be written.
Returns
0 on success, or a negative error code.
int iBSP430sensorsSHT21getMeasurement ( hBSP430halSERIAL  i2c,
int  hold_master,
uint16_t *  rawp 
)

Read a measurement from the SHT21.

The type of the measurement is not provided by this API: the caller must be aware of whether the last initiated sample was for temperature or for humidity.

Parameters
i2cthe I2C bus on which the SHT21 device can be contacted
hold_masternonzero if the measurement initiation used hold-master mode; zero if it did not. If nonzero the i2c bus must not have been used since the measurement was initiated. If zero, this function will return -1 if the measurement is not yet available.
rawppointer to where the 16-bit raw measurement should be stored.
Returns
A negative error code on I2C failure, otherwise the result of running iBSP430sensorsSHT21crc() on the data read from the sensor.
int iBSP430sensorsSHT21getSample ( hBSP430halSERIAL  i2c,
hBSP430sensorsSHT21sample  sample 
)

Read both temperature and humidity from the SHT21.

Parameters
i2cthe i2c bus on which the SHT21 device can be contacted.
samplea pointer to where the sample data should be stored. Calibrated values are calculated from the raw values if the measurement was successful. On input sBSP430sensorsSHT21sample::config should accurately reflect the current configuration of the device.
Returns
0 if the sample was successfully retrieved, otherwise a negative error code.
int iBSP430sensorsSHT21initiateMeasurement ( hBSP430halSERIAL  i2c,
int  hold_master,
eBSP430sensorsSHT21measurement  type 
)

Initiate a measurement.

Parameters
i2cthe I2C bus on which the SHT21 device can be contacted
hold_masternonzero if the SHT21 should hold the I2C bus until the measurement is complete; zero if it should release it for other use. If nonzero the i2c bus is not restored to its original state on return; it must remain untouched until the corresponding iBSP430sensorsSHT21getMeasurement() succeeds, and must be manually placed back in reset mode if so desired.
typethe type of measurement requested.
Returns
0 if successful, otherwise a negative error code.
int iBSP430sensorsSHT21reset ( hBSP430halSERIAL  i2c)

Execute a soft reset of the SHT21.

This restores all configuration settings except the on-chip heater to their power-up state. The chip will be ready for use BSP430_SENSORS_SHT21_RESET_DELAY_MS milliseconds after this function is invoked; the user is responsible for this delay.

Parameters
i2cthe I2C bus on which the SHT21 device can be contacted
Returns
0 on success, a negative error code on failure.