//---------------------------------------------------------------------------------- char sht_do_measure(unsigned char Sensor){ //---------------------------------------------------------------------------------- // sample program that shows how to use SHT11 functions // 1. connection reset // 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit) // 3. calculate humidity [%RH] and temperature [°C] // 4. calculate dew point [°C] // 5. print temperature, humidity, dew point unsigned int humi_val_i; unsigned int temp_val_i; Values humi_val; Values temp_val; unsigned char error,checksum; unsigned int i; float humi_val_f; float temp_val_f; error=sht_SetSensor(Sensor); sht_connectionreset(); SHTdevice_table[CurrentSensorIndex].valid = 0; error+=sht_measure(HUMI); //measure humidity humi_val_i = CurrentValue; error+=sht_measure(TEMP); //measure temperature temp_val_i = CurrentValue; if(error!=0) { sht_connectionreset(); } //in case of an error: connection reset else { humi_val_f = (float)humi_val_i; //converts integer to float temp_val_f = (float)temp_val_i; //converts integer to float calc_sth11(&humi_val_f,&temp_val_f); SHTdevice_table[CurrentSensorIndex].humidity = (unsigned char)humi_val_f; SHTdevice_table[CurrentSensorIndex].temperature = (int)(temp_val_f*100); SHTdevice_table[CurrentSensorIndex].valid = 1; } return error; }
//---------------------------------------------------------------------------------- // measure and check crc (with waiting) //---------------------------------------------------------------------------------- unsigned char sht_measure_check(unsigned int *value, unsigned char mode) { unsigned char checksum; unsigned int val; unsigned char data[3]; if (sht_measure((unsigned char*)&val,&checksum,mode)!=0) return 1; switch (mode) { case TEMP: data[0]=MEASURE_TEMP; break; case HUMI: data[0]=MEASURE_HUMI; break; default: return 1; } data[1]=(unsigned int)val>>8; data[2]=val; if (checksum!=sht_crc(data,3)) return 1; *value=val; return 0; }