//---------------------------------------------------------------------------------- char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned char error=0; unsigned int i; s_transstart(); //transmission start //发送测量命令 switch(mode){ //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } //读数 for (i=0;i<60000;i++) if(SDA_READ()==0) break; //wait until sensor has finished the measurement if(SDA_READ()) error+=1; // or timeout (~2 sec.) is reached *(p_value+1) =s_read_byte(ACK); //read the first byte (MSB) // printf("p_value is %x",*p_value); *(p_value)=s_read_byte(ACK); // printf("p_value+1 is %x",*(p_value+1)); //read the second byte (LSB) *p_checksum =s_read_byte(noACK); //read checksum return error; // } }
//---------------------------------------------------------------------------------- u8 s_measure(u16 *p_value, u16 *p_checksum, u8 mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned short error=0; // unsigned int i; u16 sht11_msb, sht11_lsb; s_transstart(); //transmission start switch(mode){ //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } if(error != 0){return error;} /* Enable External_Interrupt1 of SHT11. */ MAKE_I2CDATA_INPUT(); //SHT11_INTERRUPT_ENABLE(); while(1){ sht11_delay(500); if(READ_I2CDATA_PIN() == 0) break; } MAKE_I2CDATA_INPUT(); sht11_msb = s_read_byte(ACK); //read the first byte (MSB) sht11_lsb = s_read_byte(ACK); //read the second byte (LSB) *p_value = (sht11_msb * 256) + sht11_lsb; *p_checksum =s_read_byte(noACK); //read checksum acq_complete = false; return error; }
/* ********************************************************************************************************* * MAKE MEASUREMENT ON HUMIDITY AND TEMPERATURE IN 12BITS ADN 14BITS * * Description : Makes a measurement (humidity/temperature) with checksum * Arguments : * * Returns : * Notes : It takes approximately 11/55/210 ms for a 8/12/14bit measurement. * Measurement data is stored until readout. * Two bytes of measurement data and one byte of CRC checksum will then be transmitted. * The uC must acknowledge each byte by pulling the DATA line low. ********************************************************************************************************* */ char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) { unsigned error=0; unsigned int i; s_transstart(); //transmission start switch(mode) { //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } (mode==HUMI)?DelayMs(55):DelayMs(210); for (i=0; i<65535; i++) if(DATA_RD==0) break; //wait until sensor has finished the measurement if(DATA_RD) error+=1; //or timeout (~2 sec.) is reached *(p_value+1) = s_read_byte(ACK); //read the first byte (MSB) *(p_value) = s_read_byte(ACK); //read the second byte (LSB) *p_checksum = s_read_byte(noACK); //read checksum return error; }
//---------------------------------------------------------------------------------- char s_measure(int *p_value, unsigned char *p_checksum, unsigned char mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { //unsigned char error=0; // int MSB, LSB, i; error_=0; s_transstart(); //transmission start switch(mode){ //send command to sensor case TEMP : error_+=s_write_byte(MEASURE_TEMP); break; case HUMI : error_+=s_write_byte(MEASURE_HUMI); break; default : break; } P2DIR &= ~SDA; //change direction; while (1) { if(! (SDA & P2IN)) break; } if(P2IN & SDA) error_+=1; // or timeout (~2 sec.) is reached MSB = s_read_byte(ACK); //read the first byte (MSB) LSB = s_read_byte(ACK); //read the second byte (LSB) *p_checksum =s_read_byte(NACK); //read checksum *p_value = 256 * MSB + LSB; return error_; }
//---------------------------------------------------------------------------------- u8 s_write_statusreg(u8 *p_value) //---------------------------------------------------------------------------------- // writes the status register with checksum (8-bit) { u8 error=0; s_transstart(); //transmission start error+=s_write_byte(STATUS_REG_W);//send command to sensor error+=s_write_byte(*p_value); //send value of status register return error; //error>=1 in case of no response form the sensor }
uint8_t s_start_measure(uint8_t mode) { // makes a measurement (humidity/temperature) with checksum uint8_t error=0; s_transstart(); //transmission start switch(mode){ //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } return error; }
/* ********************************************************************************************************* * SOFT RESET THE SENSOR * * Description : Soft reset, resets the interface, clears the status register to default values * Wait minimum 11 ms before next command * Arguments : none * * Returns : 1 if no response from the sensor * Notes : ********************************************************************************************************* */ char s_softreset(void) { unsigned char error=0; s_connectionreset(); //reset communication error+=s_write_byte(RESET); //send RESET-command to sensor return error; //error=1 in case of no response from the sensor }
//---------------------------------------------------------------------------------- u8 s_softreset(void) //---------------------------------------------------------------------------------- // resets the sensor by a softreset { u8 error=0; s_connectionreset(); //reset communication error+=s_write_byte(RESET); //send RESET-command to sensor return error; //error=1 in case of no response form the sensor }
uint8_t humid_sht_reset( void ) { // resets the sensor by a softreset uint8_t error=0; s_connectionreset(); //reset communication error+=s_write_byte(RESET); //send RESET-command to sensor return error; //error=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- u8 s_read_statusreg(u8 *p_value, u8 *p_checksum) //---------------------------------------------------------------------------------- // reads the status register with checksum (8-bit) { u8 error=0; s_transstart(); //transmission start error=s_write_byte(STATUS_REG_R); //send command to sensor *p_value=s_read_byte(ACK); //read status register (8-bit) *p_checksum=s_read_byte(noACK); //read checksum (8-bit) return error; //error=1 in case of no response form the sensor }
uint8_t s_measure(uint16_t *p_value, uint8_t *p_checksum, uint8_t mode) { // makes a measurement (humidity/temperature) with checksum uint8_t error=0; uint32_t i; s_transstart(); //transmission start switch(mode){ //send command to sensor case TEMP : error+=s_write_byte(MEASURE_TEMP); break; case HUMI : error+=s_write_byte(MEASURE_HUMI); break; default : break; } for (i=0;i<6665535;i++) if(DATA_IN==0) break; //wait until sensor has finished the measurement if(DATA_IN) error+=1; // or timeout (~2 sec.) is reached *(p_value) = s_read_byte(ACK) << 8; //read the first byte (MSB) *(p_value) |= s_read_byte(ACK); //read the second byte (LSB) *p_checksum = s_read_byte(noACK); //read checksum return error; }
//---------------------------------------------------------------------------------- char s_softreset(void) //---------------------------------------------------------------------------------- // resets the sensor by a softreset { //unsigned char error=0; error_=0; s_connectionreset(); //reset communication error_+=s_write_byte(RESET); //send RESET-command to sensor return error_; //error=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- void ReadHMS(void) //---------------------------------------------------------------------------------- { if(timeout) timeout--; if(hmioerr) { if(++hmerrcnt>3) // больше трех ошибок подряд? { hmerrcnt=0; // сбросить счетчик ошибок hms_errflg = -2; // неисправность сенсора влажности // ets_printf("*"); }; hmioerr = 0; // сбросить флаг ошибки hmfuncs = 0; // далее сбросить датчик SetTimeOut(50); // зададим таймаут в 50 ms }; switch(hmfuncs) { default: if(!TestTimeOut()) break; // ожидание паузы s_connectionreset(); // 26t // s_transstart(); // 8t transmission start if(hmioerr) break; s_write_byte(HTD_WS);// 18t Status Register Write [0x06] if(hmioerr) break; s_write_byte(0x00); // 18t if(hmioerr) break; hmioerr=0; // сбросить флаг ошибки (см. InitHMS()) hmfuncs=1; // далее на запрос температуры датчика break; // 26+8+16+18 = 68t -> 68*1.25=85 us case 2: // чтение данных температуры с датчика и запрос данных о влажности case 5: // чтение данных температуры с датчика и запрос данных о влажности if(i2c_test_sda()) { if(TestTimeOut()) hmioerr++; break; } reg_tmp = s_read_byte() << 8; // 19t reg_tmp |= s_read_byte(); // 19t if (s_read_crc()) // 19t { hmioerr++; break; }; s_transstart(); // 8t transmission start s_write_byte(HTD_MH); // 18t Read Measure Humidity [0x05] ... 0.06133 s hmfuncs++; // след. функция SetTimeOut(120); // зададим таймаут в 120 ms break; // 19*3+8+18=83t 83*1.25=103.75us=0.00010375 sec case 3: // чтение данных о влажности case 6: // чтение данных о влажности if(i2c_test_sda()) { if(TestTimeOut()) hmioerr++; break; } reg_rh = s_read_byte()<<8; // 19t reg_rh |= s_read_byte(); if (s_read_crc()) // 19t // ~75us { hmioerr++; break; }; case 1: // запрос температуры датчика (цикл опроса 0.2744s) s_transstart(); // 8t transmission start s_write_byte(HTD_MT); // 18t Read Measure Temperature [0x03] ... 0.2128 s hmfuncs++; // далее на чтение данных температуры с датчика SetTimeOut(350); // зададим таймаут в 350 ms break; case 4: // сумма, проход 1 T.ul = reg_tmp; RH.ul = reg_rh; hmfuncs++; break; case 7: // сумма, проход 2 T.ul += reg_tmp; // mouts.rt = T.ui[0]; RH.ul += reg_rh; // mouts.rh = RH.ui[0]; hmfuncs++; break; case 8: // расчет, часть 1 #if HDT14BIT T.d=((float)(T.ul))*0.005 - PTATD1; //calc. Temperature from ticks to [C] 36.92 0.3 #else T.d=((float)(T.ul))*0.002 - PTATD1; //calc. Temperature from ticks to [C] #endif RH.d=(float)(RH.ul)*0.5; #if HDT14BIT RH.d=(T.d-25.0)*(0.01+0.00008*RH.d)-0.0000028*RH.d*RH.d+0.0405*RH.d-4.0; #else RH.d=(T.d-25.0)*(0.01+0.00128*RH.d)-0.00072*RH.d*RH.d+0.648*RH.d-4.0; #endif if(RH.d>100.0) RH.d=100.0; else if(RH.d<0.1) RH.d=0.1; /* hmfuncs++; break; case 9: // расчет, часть 2 Dp.d = (log10(RH.d)-2)/0.4343 + (17.62*T.d)/(243.12+T.d); Dp.d = 243.12*Dp.d/(17.62-Dp.d); ets_printf("T=%d, RH=%d, DP=%d\n", (int)(T.d*100.0), (int)(RH.d*100.0), (int)(Dp.d*100.0) ); */ // перевод float в int.01 hms_tmp = (int)(T.d*100.0); hms_rh = (int)(RH.d*100.0); hms_count++; hms_errflg = 0; // сбросить неисправность сенсора влажности // ets_printf("T=%d, RH=%d\n", hms_tmp, hms_rh); hmerrcnt=0; // сбросить счетчик ошибок hmfuncs=2; // на начало опроса break; } }