Port of the SHT2x example code from Sensirion
Dependents: sht21_test ENVLogger
Diff: SHT2x.cpp
- Revision:
- 1:d0f691423bf1
- Parent:
- 0:74df6ab91c79
- Child:
- 2:2464fed17980
--- a/SHT2x.cpp Sat Apr 23 14:08:11 2011 +0000 +++ b/SHT2x.cpp Sat Apr 23 18:10:56 2011 +0000 @@ -10,11 +10,9 @@ //==============================================================================// //---------- Includes ---------------------------------------------------------- #include "SHT2x.h" +#include "mbed.h" -SHT2x::SHT2x (PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl) { -} - -SHT2x::SHT2x (I2C& p_i2c) : i2c(p_i2c) { +SHT2x::SHT2x (PinName p_sda, PinName p_scl) : i2c(p_sda, p_scl), out(USBTX, USBRX) { } //==============================================================================// @@ -40,19 +38,19 @@ { int checksum; //variable for checksum byte int error=0; //variable for error code - + i2c.start(); i2c.write(I2C_ADR_W); i2c.write(USER_REG_R); - + i2c.start(); i2c.write(I2C_ADR_R); - - - *pRegisterValue = i2c.read(1); - - checksum=i2c.read(0); - + + + *pRegisterValue = i2c.read(ACK); + + checksum=i2c.read(NoACK); + error |= SHT2x_CheckCrc (pRegisterValue,1,checksum); i2c.stop(); @@ -64,7 +62,7 @@ { int error=0; //variable for error code i2c.start(); - + i2c.write(I2C_ADR_W); i2c.write(USER_REG_W); i2c.write(*pRegisterValue); @@ -79,43 +77,48 @@ int checksum; //checksum int data[2]; //data array for checksum verification int error=0; //error variable - - //-- write I2C sensor address and command -- + int i; + + //-- write I2C sensor address and command -- i2c.start(); - + i2c.write(I2C_ADR_W); // I2C Adr switch (eSHT2xMeasureType) { case HUMIDITY: i2c.write(TRIG_RH_MEASUREMENT_HM); break; - case TEMP : + case TEMP: i2c.write(TRIG_T_MEASUREMENT_HM); break; default: break; } + //-- wait until hold master is released -- - i2c.start(); + i2c.start(); i2c.write(I2C_ADR_R); - + //SCL=HIGH; // set SCL I/O port as input - - //for (i=0; i<1000; i++) { // wait until master hold is released or - // DelayMicroSeconds(1000); // a timeout (~1s) is reached - // if (SCL_CONF==1) break; - //} - + i2c.sclAsInput(); + + for (i=0; i<1000; i++) { // wait until master hold is released or + wait_ms(1); // a timeout (~1s) is reached + if (i2c.sclRead() == 1) break; + } + //-- check for timeout -- - //if (SCL_CONF==0) error |= TIME_OUT_ERROR; -//-- read two data bytes and one checksum byte -- + if (i2c.sclRead() == 0) error |= TIME_OUT_ERROR; + //-- read two data bytes and one checksum byte -- - data[0] = i2c.read(1); - data[1] = i2c.read(1); - + i2c.sclNormal(); + + data[0] = i2c.read(ACK); + data[1] = i2c.read(ACK); + *pMeasurand = data[0] << 8; *pMeasurand |= data[1]; - - checksum = i2c.read(0); + + checksum = i2c.read(NoACK); //-- verify checksum -- error |= SHT2x_CheckCrc (data, 2, checksum); @@ -133,7 +136,7 @@ //-- write I2C sensor address and command -- i2c.start(); - + i2c.write(I2C_ADR_W); switch (eSHT2xMeasureType) { case HUMIDITY: @@ -144,25 +147,25 @@ break; default: break; - + } //-- poll every 10ms for measurement ready. Timeout after 20 retries (200ms)-- do { i2c.start(); - wait_ms(10); + wait_ms(10); if (i++ >= 20) break; } while (i2c.write(I2C_ADR_R) == 0); - + if (i >= 20) error |= TIME_OUT_ERROR; - + //-- read two data bytes and one checksum byte -- - data[0] = i2c.read(1); - data[1] = i2c.read(1); - + data[0] = i2c.read(ACK); + data[1] = i2c.read(ACK); + *pMeasurand = data[0] << 8; - *pMeasurand |= data[1]; - - checksum = i2c.read(0); + *pMeasurand |= data[1]; + + checksum = i2c.read(NoACK); //-- verify checksum -- error |= SHT2x_CheckCrc (data,2,checksum); i2c.stop(); @@ -196,7 +199,7 @@ { float temperatureC; // variable for result u16sT &= ~0x0003; // clear bits [1..0] (status bits) - //-- calculate temperature [°C] -- + //-- calculate temperature [�C] -- temperatureC= -46.85 + 175.72/65536 *(float)u16sT; //T= -46.85 + 175.72 * ST/2^16 return temperatureC; } @@ -205,21 +208,21 @@ //==============================================================================// { int error=0; //error variable - //Read from memory location 1 + //Read from memory location 1 i2c.start(); - error |= i2c.write(I2C_ADR_W); + error |= i2c.write(I2C_ADR_W); error |= i2c.write(0xFA); //Command for readout on-chip memory error |= i2c.write(0x0F); //on-chip memory address i2c.start(); error |= i2c.write(I2C_ADR_R); //I2C address - u8SerialNumber[5] = i2c.read(1); //Read SNB_3 - i2c.read(1); //Read CRC SNB_3 (CRC is not analyzed) - u8SerialNumber[4] = i2c.read(1); //Read SNB_2 - i2c.read(1); //Read CRC SNB_2 (CRC is not analyzed) - u8SerialNumber[3] = i2c.read(1); //Read SNB_1Sample Code SHT21 - i2c.read(1); //Read CRC SNB_1 (CRC is not analyzed) - u8SerialNumber[2] = i2c.read(1); //Read SNB_0 - i2c.read(0); //Read CRC SNB_0 (CRC is not analyzed) + 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 + i2c.read(ACK); //Read CRC SNB_2 (CRC is not analyzed) + u8SerialNumber[3] = i2c.read(ACK); //Read SNB_1Sample Code SHT21 + i2c.read(ACK); //Read CRC SNB_1 (CRC is not analyzed) + u8SerialNumber[2] = i2c.read(ACK); //Read SNB_0 + i2c.read(NoACK); //Read CRC SNB_0 (CRC is not analyzed) i2c.stop(); //Read from memory location 2 i2c.start(); @@ -228,12 +231,12 @@ error |= i2c.write(0xC9); //on-chip memory address i2c.start(); error |= i2c.write(I2C_ADR_R); //I2C address - u8SerialNumber[1] = i2c.read(1); //Read SNC_1 - u8SerialNumber[0] = i2c.read(1); //Read SNC_0 - i2c.read(1); - u8SerialNumber[7] = i2c.read(1); //Read SNA_1 - u8SerialNumber[6] = i2c.read(1); //Read SNA_0 - i2c.read(0); //Read CRC SNA0/1 (CRC is not analyzed) + u8SerialNumber[1] = i2c.read(ACK); //Read SNC_1 + u8SerialNumber[0] = i2c.read(ACK); //Read SNC_0 + i2c.read(ACK); + u8SerialNumber[7] = i2c.read(ACK); //Read SNA_1 + u8SerialNumber[6] = i2c.read(ACK); //Read SNA_0 + i2c.read(NoACK); //Read CRC SNA0/1 (CRC is not analyzed) i2c.stop(); return error; } \ No newline at end of file