Ok

Dependencies:   mbed_rtos_types Mutex mbed_rtos_storage mbed Semaphore

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Si7021.h Source File

Si7021.h

00001 #ifndef SI7021_H
00002 #define SI7021_H
00003 
00004 #include "mbed.h"
00005 
00006 /** Si7012 Read Temperature Command */
00007 #define READ_TEMP        0xE0 /* Read previous T data from RH measurement command*/
00008 /** Si7012 Read RH Command */
00009 #define READ_RH          0xE5 /* Perform RH (and T) measurement. */
00010 
00011 /** Si7012 Read ID */
00012 #define READ_ID1_1       0xFA
00013 #define READ_ID1_2       0x0F
00014 #define READ_ID2_1       0xFC
00015 #define READ_ID2_2       0xC9
00016 
00017 /** Si7012 Read Firmware Revision */
00018 #define READ_FWREV_1     0x84
00019 #define READ_FWREV_2     0xB8
00020 
00021 /** I2C device address for Si7021 */
00022 #define ADDR    0x80
00023 
00024 /** I2C device frequency for Si7021 */
00025 #define FREQ    100000
00026 
00027 /** Device ID value for Si7021 */
00028 #define DEVICE_ID 0x15
00029 
00030 class Si7021
00031 {
00032 public:
00033     Si7021(PinName sda, PinName scl);
00034     ~Si7021();
00035     
00036     /*
00037      * Get last measured temperature data
00038      * return: int32_t = temperature in millidegrees centigrade
00039      */
00040     int32_t get_temperature();
00041 
00042     /*
00043      * Get last measured relative humidity data
00044      * return: uint32_t = relative humidity value in milli-percent
00045      */
00046     uint32_t get_humidity();
00047     
00048     /*
00049      * Perform measurement.
00050      * Asynchronous callback can be provided (type void (*)(void)).
00051      * return: 0 if successful, else one of the defined error codes.
00052      */
00053     bool measure();
00054     
00055     /*
00056      * Check if the sensor is active and responding. This will update the get_active value.
00057      * Asynchronous callback can be provided (type void (*)(void)).
00058      * return: 0 if successful, else one of the defined error codes.
00059      */
00060     bool check();
00061     
00062 private:
00063     I2C i2c;
00064     
00065     uint8_t  rx_buff[8];
00066     uint8_t  tx_buff[2];
00067 
00068     uint32_t rhData;
00069     int32_t  tData;
00070 };
00071 
00072 #endif
00073 Si7021::Si7021(PinName sda, PinName scl):i2c(sda, scl)
00074 {
00075     i2c.frequency(FREQ);
00076 }
00077 
00078 Si7021::~Si7021()
00079 {
00080     
00081 }
00082 
00083 int32_t Si7021::get_temperature()
00084 {
00085     return tData;
00086 }
00087 
00088 uint32_t Si7021::get_humidity()
00089 {
00090     return rhData;
00091 }
00092 
00093 bool Si7021::measure()
00094 {
00095     tx_buff[0] = READ_RH;
00096     if(i2c.write(ADDR, (char*)tx_buff, 1) != 0) return 0;
00097     if(i2c.read(ADDR, (char*)rx_buff, 2) != 0) return 0;
00098     
00099     rhData = ((uint32_t)rx_buff[0] << 8) + (rx_buff[1] & 0xFC);
00100     rhData = (((rhData) * 15625L) >> 13) - 6000;
00101     
00102     tx_buff[0] = READ_TEMP;
00103     if(i2c.write(ADDR, (char*)tx_buff, 1) != 0) return 0;
00104     if(i2c.read(ADDR, (char*)rx_buff, 2) != 0) return 0;
00105     
00106     tData = ((uint32_t)rx_buff[0] << 8) + (rx_buff[1] & 0xFC);
00107     tData = (((tData) * 21965L) >> 13) - 46850;
00108     
00109     return 1;
00110 }
00111 
00112 bool Si7021::check()
00113 {
00114     tx_buff[0] = READ_ID2_1;
00115     tx_buff[1] = READ_ID2_2;
00116     if(i2c.write(ADDR, (char*)tx_buff, 2) != 0) return 0;
00117     if(i2c.read(ADDR, (char*)rx_buff, 8) != 0) return 0;
00118     
00119     if(rx_buff[0] == DEVICE_ID)
00120         return true;
00121     else return 0;
00122 }