//---------------------------------------------------------------------------------- unsigned char sht_measure(sht_value *p_sht_value, unsigned char *p_checksum, unsigned char mode, unsigned char pin) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned char error=0; unsigned char i; sht_value this_sht_value; /* sht_transstart(pin); //transmission start */ sht_connectionreset(pin); switch(mode) { //send command to sensor case TEMP : error+=sht_write_byte(MEASURE_TEMP, pin); /*usart_write("TEMP: ");*/ break; case HUMI : error+=sht_write_byte(MEASURE_HUMI, pin); /*usart_write("HUMI: ");*/ break; default : break; } for (i=0;i<500;i++) { if(SHT_DATA(pin)==0) break; //wait until sensor has finished the measurement _delay_ms(1); // neccesary since the AVR is so much faster than the old 8051 // and the 210ms for 14bit measuremnt are only a rough estimate } if(SHT_DATA(pin)) error+=1; // or timeout is reached this_sht_value.i = 256*sht_read_byte(ACK, pin); //read the first byte (MSB) this_sht_value.i +=sht_read_byte(ACK, pin); //read the second byte (LSB) *p_checksum =sht_read_byte(noACK, pin); //read checksum *(p_sht_value)= this_sht_value; return error; }
//---------------------------------------------------------------------------------- // writes the status register with checksum (8-bit) //---------------------------------------------------------------------------------- char sht_write_statusreg(unsigned char *p_value) { unsigned char error=0; sht_transstart(); //transmission start error+=sht_write_byte(STATUS_REG_W);//send command to sensor error+=sht_write_byte(*p_value); //send value of status register return error; //error>=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- // start measurement (humidity/temperature) //---------------------------------------------------------------------------------- char sht_measure_start(unsigned char mode) { sht_transstart(); // transmission start switch(mode) { // send command to sensor case TEMP: return(sht_write_byte(MEASURE_TEMP)); //break; case HUMI: return(sht_write_byte(MEASURE_HUMI)); //break; default: break; } return 1; // error }
//---------------------------------------------------------------------------------- unsigned char sht_write_statusreg(unsigned char *p_sht_value, unsigned char pin) //---------------------------------------------------------------------------------- // writes the status register with checksum (8-bit) { unsigned char error=0; sht_transstart(pin); //transmission start error+=sht_write_byte(STATUS_REG_W, pin);//send command to sensor error+=sht_write_byte(*p_sht_value, pin); //send value of status register return error; //error>=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- char sht_measure(unsigned char mode) //---------------------------------------------------------------------------------- // makes a measurement (humidity/temperature) with checksum { unsigned error=0; unsigned int i; unsigned char LSB; unsigned char MSB; unsigned char tmp = 0; unsigned char bits = 8; sht_crc_init(); sht_transstart(); //transmission start switch(mode){ //send command to sensor case TEMP : error+=sht_write_byte(MEASURE_TEMP); sht_crc_shuffle_byte(MEASURE_TEMP);break; case HUMI : error+=sht_write_byte(MEASURE_HUMI); sht_crc_shuffle_byte(MEASURE_HUMI);break; default : break; } for (i=0;i<40000;i++) { if(DATA_IN==0) { break; //wait until sensor has finished the measurement } Delay100TCYx(4); //50 us } if(DATA_IN) { error+=1; // or timeout (~2 sec.) is reached } MSB = sht_read_byte(ACK); //read the first byte (MSB) sht_crc_shuffle_byte(MSB); LSB = sht_read_byte(ACK); //read the second byte (LSB) sht_crc_shuffle_byte(LSB); CurrentValue = MSB * 256 + LSB; CheckSum = sht_read_byte(noACK); //read checksum while( bits--) { tmp >>=1; if( sht_crc & 0b10000000) tmp |= 0b10000000; sht_crc <<=1; } if (tmp != CheckSum) { SHTdevice_table[CurrentSensorIndex].crc++; return 1; } return error; }
//---------------------------------------------------------------------------------- char sht_read_statusreg(void) //---------------------------------------------------------------------------------- // reads the status register with checksum (8-bit) { unsigned char error=0; unsigned char tmp = 0; unsigned char bits = 8; sht_crc_init(); sht_transstart(); //transmission start error=sht_write_byte(STATUS_REG_R); //send command to sensor sht_crc_shuffle_byte(STATUS_REG_R); StatusReg =sht_read_byte(ACK); //read status register (8-bit) sht_crc_shuffle_byte(StatusReg); CheckSum =sht_read_byte(noACK); //read checksum (8-bit) while( bits--) { tmp >>=1; if( sht_crc & 0b10000000) tmp |= 0b10000000; sht_crc <<=1; } if (tmp != CheckSum) { SHTdevice_table[CurrentSensorIndex].crc++; error++; } return error; //error=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- // resets the sensor by a softreset //---------------------------------------------------------------------------------- char sht_softreset(void) { unsigned char error=0; sht_connectionreset(); //reset communication error+=sht_write_byte(RESET); //send RESET-command to sensor return error; //error=1 in case of no response form the sensor }
//---------------------------------------------------------------------------------- // reads the status register with checksum (8-bit) //---------------------------------------------------------------------------------- char sht_read_statusreg(unsigned char *p_value, unsigned char *p_checksum) { unsigned char error=0; sht_transstart(); //transmission start error=sht_write_byte(STATUS_REG_R); //send command to sensor *p_value=sht_read_byte(ACK); //read status register (8-bit) *p_checksum=sht_read_byte(noACK); //read checksum (8-bit) return error; //error=1 in case of no response form the sensor }
char sht_measure( unsigned char *p_value, unsigned char *p_checksum, unsigned char mode ) { unsigned error=0; unsigned long i; sht_transstart(); // transmission start switch( mode ) { // send command to sensor case SHT_MODE_TEMP: error += sht_write_byte( MEASURE_TEMP ); break; case SHT_MODE_HUMI: error += sht_write_byte( MEASURE_HUMI ); break; default: break; } // wait until sensor has finished the measurement sht_measure_timer = 0; while ( sht_measure_timer < SHT_READ_TIMEOUT ) { if ( 0 == ( PORTCbits.RC5 ) ) { break; } } if ( PORTCbits.RC5 ) { // or timeout (~2 sec.) is reached error += 1; } *(p_value) = sht_read_byte( ACK ); // read the first byte (MSB) *(p_value+1)= sht_read_byte( ACK ); // read the second byte (LSB) *p_checksum = sht_read_byte( noACK ); // read checksum return error; }
//---------------------------------------------------------------------------------- unsigned char sht_softreset(unsigned char pin) //---------------------------------------------------------------------------------- // resets the sensor by a softreset { // rockclimber MAKE_SHT_SCK_PIN_OUTPUT; asm volatile ("nop"::); unsigned char error=0; sht_connectionreset(pin); //reset communication error+=sht_write_byte(RESET_SHT, pin); //send RESET_SHT-command to sensor return error; //error=1 in case of no response form the sensor }