VTT TinyNode, slightly modified code from example (https://developer.mbed.org/users/jejuho/code/VTT_NODEV3_BMP180_Si7021)

Dependencies:   BLE_API TMP_nrf51 mbed nRF51822

Fork of VTT_NODEV3_BMP180_Si7021 by Juho Eskeli

Committer:
jejuho
Date:
Mon Jan 25 14:36:57 2016 +0000
Revision:
2:b221ba23b37f
Initial version. Values from sensors not verified. Some problems with Si7021.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jejuho 2:b221ba23b37f 1 #include "Si7021.h"
jejuho 2:b221ba23b37f 2
jejuho 2:b221ba23b37f 3 SI7021_I2C::SI7021_I2C(PinName sda, PinName scl) : i2c_(sda, scl)
jejuho 2:b221ba23b37f 4 {
jejuho 2:b221ba23b37f 5 //400kHz, allowing us to use the fastest data rates.
jejuho 2:b221ba23b37f 6 i2c_.frequency(400000);
jejuho 2:b221ba23b37f 7 }
jejuho 2:b221ba23b37f 8
jejuho 2:b221ba23b37f 9 uint8_t SI7021_I2C::readBytes(uint8_t *values, int8_t length, bool waitConversion)
jejuho 2:b221ba23b37f 10 {
jejuho 2:b221ba23b37f 11 uint8_t Status=0;
jejuho 2:b221ba23b37f 12
jejuho 2:b221ba23b37f 13 if(!i2c_.write(SI7021_ADDR, (const char*)values, 1,0))
jejuho 2:b221ba23b37f 14 Status=1;
jejuho 2:b221ba23b37f 15
jejuho 2:b221ba23b37f 16 //Wait the measurement time
jejuho 2:b221ba23b37f 17 if(waitConversion)
jejuho 2:b221ba23b37f 18 wait_ms(23);
jejuho 2:b221ba23b37f 19
jejuho 2:b221ba23b37f 20 if(!i2c_.read(SI7021_ADDR, (char*)values, length,0))
jejuho 2:b221ba23b37f 21 Status=1;
jejuho 2:b221ba23b37f 22 return(Status);
jejuho 2:b221ba23b37f 23 }
jejuho 2:b221ba23b37f 24
jejuho 2:b221ba23b37f 25
jejuho 2:b221ba23b37f 26 float SI7021_I2C::Measure_Temp(void)
jejuho 2:b221ba23b37f 27 {
jejuho 2:b221ba23b37f 28 float Temperature;
jejuho 2:b221ba23b37f 29 uint16_t ConvValue;
jejuho 2:b221ba23b37f 30 uint8_t *Ptr;
jejuho 2:b221ba23b37f 31 uint8_t Data[2];
jejuho 2:b221ba23b37f 32
jejuho 2:b221ba23b37f 33 Data[0]=SI7021_MEASURE_TEMPERATURE_HOLD_MASTER;
jejuho 2:b221ba23b37f 34 if(readBytes(Data,2, 1))
jejuho 2:b221ba23b37f 35 {
jejuho 2:b221ba23b37f 36 Ptr=(uint8_t*)&ConvValue;
jejuho 2:b221ba23b37f 37 Ptr[0]=Data[1];
jejuho 2:b221ba23b37f 38 Ptr[1]=Data[0];
jejuho 2:b221ba23b37f 39
jejuho 2:b221ba23b37f 40 Temperature=(((float)ConvValue)/65536)*175-46.85;
jejuho 2:b221ba23b37f 41 }
jejuho 2:b221ba23b37f 42 else
jejuho 2:b221ba23b37f 43 {
jejuho 2:b221ba23b37f 44 Temperature=1234;
jejuho 2:b221ba23b37f 45 }
jejuho 2:b221ba23b37f 46
jejuho 2:b221ba23b37f 47 return(Temperature);
jejuho 2:b221ba23b37f 48 }
jejuho 2:b221ba23b37f 49
jejuho 2:b221ba23b37f 50 float SI7021_I2C::get_HumidityTemp(void)
jejuho 2:b221ba23b37f 51 {
jejuho 2:b221ba23b37f 52 float Temperature;
jejuho 2:b221ba23b37f 53 uint16_t ConvValue;
jejuho 2:b221ba23b37f 54 uint8_t *Ptr;
jejuho 2:b221ba23b37f 55 uint8_t Data[2];
jejuho 2:b221ba23b37f 56
jejuho 2:b221ba23b37f 57 Data[0]=SI7021_READ_TEMPERATURE;
jejuho 2:b221ba23b37f 58 if(readBytes(Data,2, 0))
jejuho 2:b221ba23b37f 59 {
jejuho 2:b221ba23b37f 60 Ptr=(uint8_t*)&ConvValue;
jejuho 2:b221ba23b37f 61 Ptr[0]=Data[1];
jejuho 2:b221ba23b37f 62 Ptr[1]=Data[0];
jejuho 2:b221ba23b37f 63
jejuho 2:b221ba23b37f 64 Temperature=(((float)ConvValue)/65536)*175-46.85;
jejuho 2:b221ba23b37f 65 }
jejuho 2:b221ba23b37f 66 else
jejuho 2:b221ba23b37f 67 {
jejuho 2:b221ba23b37f 68 Temperature=1234;
jejuho 2:b221ba23b37f 69 }
jejuho 2:b221ba23b37f 70
jejuho 2:b221ba23b37f 71 return(Temperature);
jejuho 2:b221ba23b37f 72 }
jejuho 2:b221ba23b37f 73
jejuho 2:b221ba23b37f 74 void SI7021_I2C::Measure_Humidity_Temp(float *humidity, float *temperature)
jejuho 2:b221ba23b37f 75 {
jejuho 2:b221ba23b37f 76 float Humidity;
jejuho 2:b221ba23b37f 77 uint16_t ConvValueHum;
jejuho 2:b221ba23b37f 78 uint8_t *Ptr;
jejuho 2:b221ba23b37f 79 uint8_t Data[2];
jejuho 2:b221ba23b37f 80
jejuho 2:b221ba23b37f 81 Data[0]=SI7021_MEASURE_RH_NO_HOLD_MASSTER;
jejuho 2:b221ba23b37f 82
jejuho 2:b221ba23b37f 83 if(readBytes(Data,2, 1))
jejuho 2:b221ba23b37f 84 {
jejuho 2:b221ba23b37f 85
jejuho 2:b221ba23b37f 86 Ptr=(uint8_t*)&ConvValueHum;
jejuho 2:b221ba23b37f 87 Ptr[0]=Data[1];
jejuho 2:b221ba23b37f 88 Ptr[1]=Data[0];
jejuho 2:b221ba23b37f 89
jejuho 2:b221ba23b37f 90 Humidity=(((float)ConvValueHum)/65536)*125-6;
jejuho 2:b221ba23b37f 91
jejuho 2:b221ba23b37f 92 if(Humidity<0)
jejuho 2:b221ba23b37f 93 Humidity=0;
jejuho 2:b221ba23b37f 94 if(Humidity>100)
jejuho 2:b221ba23b37f 95 Humidity=100;
jejuho 2:b221ba23b37f 96 }
jejuho 2:b221ba23b37f 97 else
jejuho 2:b221ba23b37f 98 Humidity=1234;
jejuho 2:b221ba23b37f 99
jejuho 2:b221ba23b37f 100 *humidity = Humidity;
jejuho 2:b221ba23b37f 101 *temperature = get_HumidityTemp();
jejuho 2:b221ba23b37f 102
jejuho 2:b221ba23b37f 103 }