Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API TMP_nrf51 mbed nRF51822
Fork of VTT_NODEV3_BMP180_Si7021 by
Si7021.cpp@3:101b0f081435, 2016-05-27 (annotated)
- Committer:
- JarkkoS
- Date:
- Fri May 27 07:38:42 2016 +0000
- Revision:
- 3:101b0f081435
- Parent:
- 2:b221ba23b37f
VTT TinyNodeV2, slightly modified code from example (https://developer.mbed.org/users/jejuho/code/VTT_NODEV3_BMP180_Si7021)
Who changed what in which revision?
| User | Revision | Line number | New 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 | } |
