Port of the SHT2x example code from Sensirion
Dependents: sht21_test ENVLogger
Revision 1:d0f691423bf1, committed 2011-04-23
- Comitter:
- ssozonoff
- Date:
- Sat Apr 23 18:10:56 2011 +0000
- Parent:
- 0:74df6ab91c79
- Child:
- 2:2464fed17980
- Commit message:
- Got Master Hold mode working, Thanks Andy!
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SHT21_I2C.h Sat Apr 23 18:10:56 2011 +0000 @@ -0,0 +1,78 @@ +/* + Copyright (c) 2010 Andy Kirkham + + 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. +*/ + +#include "mbed.h" + +#ifndef SHT21_I2C_H +#define SHT21_I2C_H + +class SHT_I2C : public I2C { + +protected: + PinName _scl; + PinName _sda; + +public: + + SHT_I2C(PinName sda, PinName scl, const char *name = NULL) : I2C(sda, scl, name) { + _sda = sda; + _scl = scl; + } + + void sclAsInput(void) { + switch(_scl) { + case p10: + LPC_PINCON->PINSEL0 &= ~(3UL << 2); // p10, P0.1 as GPIO + LPC_GPIO0->FIODIR &= ~(1UL << 1); // p10, P0.1 as Input + break; + case p27: + LPC_PINCON->PINSEL0&=~(3UL << 22); // p27, P0.11 as GPIO + LPC_GPIO0->FIODIR &= ~(1UL << 11); // p27, P0.11 as input + break; + } + } + + int sclRead(void) { + switch(_scl) { + case p10: return (LPC_GPIO0->FIOPIN & (1UL << 1)) ? 1 : 0; + case p27: return (LPC_GPIO0->FIOPIN & (1UL << 11)) ? 1 : 0; + default: + return 0; + } + } + + void sclNormal(void) { + switch(_scl) { + case p10: + LPC_PINCON->PINSEL0 &= ~(3UL << 2); // p10, P0.1 as I2C SCL1 + LPC_PINCON->PINSEL0 |= (3UL << 2); + break; + case p27: + LPC_PINCON->PINSEL0 &= ~(3UL << 22); // p27, P0.11 as I2C SCL2 + LPC_PINCON->PINSEL0 |= (2UL << 22); + break; + } + } + +}; + +#endif \ No newline at end of file
--- 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
--- a/SHT2x.h Sat Apr 23 14:08:11 2011 +0000 +++ b/SHT2x.h Sat Apr 23 18:10:56 2011 +0000 @@ -1,4 +1,5 @@ #include "mbed.h" +#include "SHT21_I2C.h" #ifndef SHT2x_H #define SHT2x_H @@ -16,6 +17,9 @@ // CRC const int POLYNOMIAL = 0x131; //P(x)=x^8+x^5+x^4+1 = 100110001 +const int ACK = 1; +const int NoACK = 0; + // sensor command enum etSHT2xCommand { TRIG_T_MEASUREMENT_HM = 0xE3, // command trig. temp meas. hold master @@ -68,7 +72,6 @@ class SHT2x : public Base { public: SHT2x (PinName p_sda, PinName p_scl); - SHT2x (I2C& p_i2c); int SHT2x_CheckCrc(int data[], int nbrOfBytes, int checksum); int SHT2x_ReadUserRegister(int *pRegisterValue); @@ -81,7 +84,8 @@ int SHT2x_GetSerialNumber(int u8SerialNumber[]); protected: - I2C i2c; + SHT_I2C i2c; + Serial out; private: