//=========================================================================== u8t SHT2x_ReadUserRegister(u8t *pRegisterValue) //=========================================================================== { u8t checksum; //variable for checksum byte u8t error=0; //variable for error code I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); error |= I2c_WriteByte (USER_REG_R); I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_R); *pRegisterValue = I2c_ReadByte(ACK); checksum=I2c_ReadByte(NO_ACK); error |= SHT2x_CheckCrc (pRegisterValue,1,checksum); I2c_StopCondition(); return error; }
//=========================================================================== u8t SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand) //=========================================================================== { u8t checksum; //checksum u8t data[2]; //data array for checksum verification u8t error=0; //error variable u16t i; //counting variable //-- write I2C sensor address and command -- I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr switch(eSHT2xMeasureType) { case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_HM); break; case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_HM); break; default: assert(0); } //-- wait until hold master is released -- I2c_StartCondition(); error |= I2c_WriteByte (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; } //-- check for timeout -- if(SCL_CONF==0) error |= TIME_OUT_ERROR; //-- read two data bytes and one checksum byte -- pMeasurand->s16.u8H = data[0] = I2c_ReadByte(ACK); pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK); checksum=I2c_ReadByte(NO_ACK); //-- verify checksum -- error |= SHT2x_CheckCrc (data,2,checksum); I2c_StopCondition(); return error; }
//=========================================================================== u8t SHT2x_MeasurePoll(etSHT2xMeasureType eSHT2xMeasureType, nt16 *pMeasurand) //=========================================================================== { u8t checksum; //checksum u8t data[2]; //data array for checksum verification u8t error=0; //error variable u16t i=0; //counting variable //-- write I2C sensor address and command -- I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr switch(eSHT2xMeasureType) { case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_POLL); break; case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_POLL); break; default: assert(0); } //-- poll every 10ms for measurement ready. Timeout after 20 retries (200ms)-- do { I2c_StartCondition(); DelayMicroSeconds(10000); //delay 10ms if(i++ >= 20) break; } while(I2c_WriteByte (I2C_ADR_R) == ACK_ERROR); if (i>=20) error |= TIME_OUT_ERROR; //-- read two data bytes and one checksum byte -- pMeasurand->s16.u8H = data[0] = I2c_ReadByte(ACK); pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK); checksum=I2c_ReadByte(NO_ACK); //-- verify checksum -- error |= SHT2x_CheckCrc (data,2,checksum); I2c_StopCondition(); return error; }
bool ICACHE_FLASH_ATTR sht21_getHumidity(float *humidity){ uint8_t data[3], i; uint16_t tmp; bool status = false; if(!commandOnlyWrite(SHT21_ADDR, 0xF5)){ return false; } for(i = 0, status = false; (i < 85) && !status; i++){ os_delay_us(1000); status = byteReadNoReg(SHT21_ADDR, data, 3); } if(status){ if(SHT2x_CheckCrc(data, 2, data[2])){ tmp = (uint16_t)data[0] << 8 | (data[1] & 0xFC); *humidity = -6.0 + 125.0 * ((float) tmp / 65536); return true; } } return false; }
bool ICACHE_FLASH_ATTR sht21_getTemperature(float *temperature){ uint8_t data[3], i; uint16_t tmp; bool status = false; if(!commandOnlyWrite(SHT21_ADDR, 0xF3)){ return false; } for(i = 0, status = false; (i < 85) && !status; i++){ os_delay_us(1000); status = byteReadNoReg(SHT21_ADDR, data, 3); } if(status){ if(SHT2x_CheckCrc(data, 2, data[2])){ tmp = (uint16_t)data[0] << 8 | (data[1] & 0xFC); *temperature = -46.85 + 175.72 * ((float) tmp / 65536); return true; } } return false; }