//============================================================================== int main(void) //============================================================================== { // variables u8t error = 0; //variable for error code. For codes see system.h // u8t userRegister; //variable for user register // bt endOfBattery; //variable for end of battery nt16 sRH; //variable for raw humidity ticks ft humidityRH; //variable for relative humidity[%RH] as float nt16 sT; //variable for raw temperature ticks ft temperatureC; //variable for temperature[°C] as float //u8t SerialNumber_SHT2x[8]; //64bit serial number Grace_init(); //Init_HW(); //initializes Hardware (osc, watchdog,...) I2c_Init(); //initializes uC-ports for I2C DelayMicroSeconds(15000); //wait for sensor initialization t_powerUp (15ms) //note: The following code segments show how to use the different functions // of SHT2x. The loop does not show a typical sequence in an application while(1) { error = 0; // reset error status /* // --- Reset sensor by command --- error |= SHT2x_SoftReset(); // --- Read the sensors serial number (64bit) --- error |= SHT2x_GetSerialNumber(SerialNumber_SHT2x); // --- Set Resolution e.g. RH 10bit, Temp 13bit --- error |= SHT2x_ReadUserRegister(&userRegister); //get actual user reg userRegister = (userRegister & ~SHT2x_RES_MASK) | SHT2x_RES_10_13BIT; error |= SHT2x_WriteUserRegister(&userRegister); //write changed user reg */ // --- measure humidity with "Polling Mode" (no hold master) --- error |= SHT2x_MeasurePoll(HUMIDITY, &sRH); // --- measure temperature with "Polling Mode" (no hold master) --- error |= SHT2x_MeasurePoll(TEMP, &sT); //-- calculate humidity and temperature -- temperatureC = SHT2x_CalcTemperatureC(sT.u16); humidityRH = SHT2x_CalcRH(sRH.u16); /* // --- check end of battery status (eob)--- // note: a RH / Temp. measurement must be executed to update the status of eob error |= SHT2x_ReadUserRegister(&userRegister); //get actual user reg if( (userRegister & SHT2x_EOB_MASK) == SHT2x_EOB_ON ) endOfBattery = true; else endOfBattery = false; */ } }
//=========================================================================== u8t SHT2x_SoftReset() //=========================================================================== { u8t error=0; //error variable I2c_StartCondition(); error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr error |= I2c_WriteByte (SOFT_RESET); // Command I2c_StopCondition(); DelayMicroSeconds(15000); // wait till sensor has restarted 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; }
//============================================================================== int main() { //============================================================================== uint8_t error = 0; uint8_t userRegister; uint16_t sRH; float humidityRH; char humitityOutStr[21]; uint16_t sT; float temperatureC; char temperatureOutStr[21]; uint8_t SerialNumber_sht21[8]; I2c_Init(); { error = 0; error |= sht21_SoftReset(); //============================================================================== error |= sht21_GetSerialNumber(SerialNumber_sht21); //============================================================================== error |= sht21_ReadUserRegister(&userRegister); userRegister = (userRegister & ~sht21_RES_MASK) | sht21_RES_10_13BIT; error |= sht21_WriteUserRegister(&userRegister); //============================================================================== error |= sht21_MeasureHM(HUMIDITY, &sRH); //============================================================================== error |= sht21_MeasurePoll(TEMP, &sT); //============================================================================== temperatureC = sht21_CalcTemperatureC(sT.u16); humidityRH = sht21_CalcRH(sRH.u16); printf(humitityOutStr, "Humidity RH:%6.2f %% ",humidityRH); printf(temperatureOutStr, "Temperature:%6.2f€C",temperatureC); DelayMicroSeconds(300000); } }