Port of the SHT2x example code from Sensirion
Dependents: sht21_test ENVLogger
Revision 2:2464fed17980, committed 2011-04-23
- Comitter:
- ssozonoff
- Date:
- Sat Apr 23 19:07:52 2011 +0000
- Parent:
- 1:d0f691423bf1
- Commit message:
- Code cleanup
Changed in this revision
SHT2x.cpp | Show annotated file Show diff for this revision Revisions of this file |
SHT2x.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r d0f691423bf1 -r 2464fed17980 SHT2x.cpp --- a/SHT2x.cpp Sat Apr 23 18:10:56 2011 +0000 +++ b/SHT2x.cpp Sat Apr 23 19:07:52 2011 +0000 @@ -1,18 +1,32 @@ -// -// S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland -//==============================================================================// -// Project : SHT2x Sample Code (V1.2) -// File : SHT2x.c -// Author : MST -// Controller: NEC V850/SG3 (uPD70F3740) -// Compiler : IAR compiler for V850 (3.50A) -// Brief : Sensor layer. Functions for sensor access -//==============================================================================// -//---------- Includes ---------------------------------------------------------- +/* + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +//============================================================================= +// Port of the SHT21 example code from Sensirion +// port by Serge Sozonoff +//============================================================================= + #include "SHT2x.h" #include "mbed.h" -SHT2x::SHT2x (PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl), out(USBTX, USBRX) { +SHT2x::SHT2x (PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl) { } //==============================================================================// @@ -32,6 +46,15 @@ if (crc != checksum) return CHECKSUM_ERROR; else return 0; } + +//===========================================================================// +int SHT2x::i2cWrite(int data) +//===========================================================================// +{ + if (i2c.write(data) < 1) return ACK_ERROR; + else return 0; +} + //===========================================================================// int SHT2x::SHT2x_ReadUserRegister(int *pRegisterValue) //===========================================================================// @@ -40,11 +63,11 @@ int error=0; //variable for error code i2c.start(); - i2c.write(I2C_ADR_W); - i2c.write(USER_REG_R); + error |= i2cWrite(I2C_ADR_W); + error |= i2cWrite(USER_REG_R); i2c.start(); - i2c.write(I2C_ADR_R); + error |= i2cWrite(I2C_ADR_R); *pRegisterValue = i2c.read(ACK); @@ -63,9 +86,9 @@ int error=0; //variable for error code i2c.start(); - i2c.write(I2C_ADR_W); - i2c.write(USER_REG_W); - i2c.write(*pRegisterValue); + error |= i2cWrite(I2C_ADR_W); + error |= i2cWrite(USER_REG_W); + error |= i2cWrite(*pRegisterValue); i2c.stop(); return error; @@ -82,21 +105,24 @@ //-- write I2C sensor address and command -- i2c.start(); - i2c.write(I2C_ADR_W); // I2C Adr + error |= i2cWrite(I2C_ADR_W); // I2C Adr switch (eSHT2xMeasureType) { case HUMIDITY: - i2c.write(TRIG_RH_MEASUREMENT_HM); + error |= i2cWrite(TRIG_RH_MEASUREMENT_HM); break; case TEMP: - i2c.write(TRIG_T_MEASUREMENT_HM); + error |= i2cWrite(TRIG_T_MEASUREMENT_HM); break; default: break; } - + //-- wait until hold master is released -- i2c.start(); - i2c.write(I2C_ADR_R); + + __disable_irq(); // Disable Interrupts + + error |= i2cWrite(I2C_ADR_R); //SCL=HIGH; // set SCL I/O port as input i2c.sclAsInput(); @@ -119,6 +145,9 @@ *pMeasurand |= data[1]; checksum = i2c.read(NoACK); + + __enable_irq(); // Enable Interrupts + //-- verify checksum -- error |= SHT2x_CheckCrc (data, 2, checksum); @@ -137,13 +166,13 @@ //-- write I2C sensor address and command -- i2c.start(); - i2c.write(I2C_ADR_W); + error |= i2cWrite(I2C_ADR_W); switch (eSHT2xMeasureType) { case HUMIDITY: - i2c.write(TRIG_RH_MEASUREMENT_POLL); + error |= i2cWrite(TRIG_RH_MEASUREMENT_POLL); break; case TEMP: - i2c.write(TRIG_T_MEASUREMENT_POLL); + error |= i2cWrite(TRIG_T_MEASUREMENT_POLL); break; default: break; @@ -177,8 +206,8 @@ { int error=0; //error variable i2c.start(); - error |= i2c.write(I2C_ADR_W); // I2C Adr - error |= i2c.write(SOFT_RESET); // Command + error |= i2cWrite(I2C_ADR_W); // I2C Adr + error |= i2cWrite(SOFT_RESET); // Command i2c.stop(); wait_ms(15); return error; @@ -199,10 +228,21 @@ { float temperatureC; // variable for result u16sT &= ~0x0003; // clear bits [1..0] (status bits) - //-- calculate temperature [�C] -- + //-- calculate temperature [in degrees C] -- temperatureC= -46.85 + 175.72/65536 *(float)u16sT; //T= -46.85 + 175.72 * ST/2^16 return temperatureC; } + +//==============================================================================// +float SHT2x::SHT2x_GetDewpoint(float h, float t) +//==============================================================================// +{ + float logEx, dew_point; + logEx = 0.66077 + 7.5 * t / (237.3 + t) + (log10(h) - 2); + dew_point = (logEx - 0.66077) * 237.3 / (0.66077 + 7.5 - logEx); + return dew_point; +} + //==============================================================================// int SHT2x::SHT2x_GetSerialNumber(int u8SerialNumber[]) //==============================================================================// @@ -210,11 +250,11 @@ int error=0; //error variable //Read from memory location 1 i2c.start(); - error |= i2c.write(I2C_ADR_W); - error |= i2c.write(0xFA); //Command for readout on-chip memory - error |= i2c.write(0x0F); //on-chip memory address + error |= i2cWrite(I2C_ADR_W); + error |= i2cWrite(0xFA); //Command for readout on-chip memory + error |= i2cWrite(0x0F); //on-chip memory address i2c.start(); - error |= i2c.write(I2C_ADR_R); //I2C address + error |= i2cWrite(I2C_ADR_R); //I2C address u8SerialNumber[5] = i2c.read(ACK); //Read SNB_3 i2c.read(ACK); //Read CRC SNB_3 (CRC is not analyzed) u8SerialNumber[4] = i2c.read(ACK); //Read SNB_2 @@ -226,11 +266,11 @@ i2c.stop(); //Read from memory location 2 i2c.start(); - error |= i2c.write(I2C_ADR_W); //I2C address - error |= i2c.write(0xFC); //Command for readout on-chip memory - error |= i2c.write(0xC9); //on-chip memory address + error |= i2cWrite(I2C_ADR_W); //I2C address + error |= i2cWrite(0xFC); //Command for readout on-chip memory + error |= i2cWrite(0xC9); //on-chip memory address i2c.start(); - error |= i2c.write(I2C_ADR_R); //I2C address + error |= i2cWrite(I2C_ADR_R); //I2C address u8SerialNumber[1] = i2c.read(ACK); //Read SNC_1 u8SerialNumber[0] = i2c.read(ACK); //Read SNC_0 i2c.read(ACK);
diff -r d0f691423bf1 -r 2464fed17980 SHT2x.h --- a/SHT2x.h Sat Apr 23 18:10:56 2011 +0000 +++ b/SHT2x.h Sat Apr 23 19:07:52 2011 +0000 @@ -1,20 +1,33 @@ +/* + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +//============================================================================= +// Port of the SHT21 example code from Sensirion +// port by Serge Sozonoff +//============================================================================= + #include "mbed.h" #include "SHT21_I2C.h" #ifndef SHT2x_H #define SHT2x_H -//============================================================================== -// S E N S I R I O N AG, Laubisruetistr. 50, CH-8712 Staefa, Switzerland -//============================================================================== -// Project : SHT2x Sample Code (V1.2) -// File : SHT2x.h -// Author : MST -// Controller: NEC V850/SG3 (uPD70F3740) -// Compiler : IAR compiler for V850 (3.50A) -// Brief : Sensor layer. Definitions of commands and registers, -// functions for sensor access -//============================================================================== -// CRC const int POLYNOMIAL = 0x131; //P(x)=x^8+x^5+x^4+1 = 100110001 const int ACK = 1; @@ -82,10 +95,11 @@ float SHT2x_CalcRH(int u16sRH); float SHT2x_CalcTemperatureC(int u16sT); int SHT2x_GetSerialNumber(int u8SerialNumber[]); + float SHT2x_GetDewpoint(float h, float t); protected: - SHT_I2C i2c; - Serial out; + SHT_I2C i2c; + int i2cWrite(int data); private: