Example #1
0
/*---------------------------------------------------------------------------*/
#if 1 /* But ok! */
unsigned
sht11_sreg(void)
{
  unsigned sreg, rcrc;

  sstart();			/* Start transmission */
  if(!swrite(STATUS_REG_R)) {
    goto fail;
  }

  sreg = sread(1);
  rcrc = sread(0);

#ifdef CRC_CHECK
  {
    unsigned crc;
    crc = crc8_add(0x0, STATUS_REG_R);
    crc = crc8_add(crc, sreg);
    if (crc != rev8bits(rcrc))
      goto fail;
  }
#endif

  return sreg;

 fail:
  sreset();
  return -1;
}
Example #2
0
/**
 * Reads the current raw temperature value
 */
uint16_t sensirion::readTemperatureRaw()
{
  uint16_t _val;

  // Command to send to the SHT1x to request Temperature
  const uint8_t _gTempCmd  = 0b00000011;

  if (millis() - _lastTempMillis < SHT_CACHE_MILLIS ){
	  return _lastTempRaw;
  } else {
	  if ( sendCommandSHT(_gTempCmd, _dataPin, _clockPin) == SHT_SUCCESS ){
		  waitForResultSHT(_dataPin);
		  _val = getData16SHT(_dataPin, _clockPin);
		  uint8_t rxcrc = rev8bits( readCRC(_dataPin, _clockPin ) );
		  uint8_t mycrc = crc8add( 0, _gTempCmd );
		  mycrc = crc8add( mycrc, _val );
		  if (mycrc != rxcrc){
			Serial.println("SHT: crc error");
			Serial.print(" Got:      0x"); Serial.println(rxcrc, HEX);
			Serial.print(" Expected: 0x"); Serial.println(mycrc, HEX);
			return 0;
		  }
		  // cache
		  _lastTempMillis = millis();
		  _lastTempRaw = _val;
		  return (_val);
	  }
  }
  return 0; // error
}
Example #3
0
/*
 * Only commands MEASURE_HUMI or MEASURE_TEMP!
 */
static unsigned int
scmd(unsigned cmd)
{
  unsigned int n;

  if(cmd != MEASURE_HUMI && cmd != MEASURE_TEMP) {
    PRINTF("Illegal command: %d\n", cmd);
    return -1;
  }

  sstart();			/* Start transmission */
  if(!swrite(cmd)) {
    PRINTF("SHT11: scmd - swrite failed\n");
    goto fail;
  }

  for(n = 0; n < 20000; n++) {
    if(!SDA_IS_1) {
      unsigned t0, t1, rcrc;
      t0 = sread(1);
      t1 = sread(1);
      rcrc = sread(0);
      PRINTF("SHT11: scmd - read %d, %d\n", t0, t1);
#ifdef CRC_CHECK
      {
	unsigned crc;
	crc = crc8_add(0x0, cmd);
	crc = crc8_add(crc, t0);
	crc = crc8_add(crc, t1);
	if(crc != rev8bits(rcrc)) {
	  PRINTF("SHT11: scmd - crc check failed %d vs %d\n",
		 crc, rev8bits(rcrc));
	  goto fail;
	}
      }
#endif
      return (t0 << 8) | t1;
    }
    /* short wait before next loop */
    clock_wait(1);
  }
 fail:
  sreset();
  return -1;
}
Example #4
0
/**
 * Reads current temperature-corrected relative humidity
 */
float sensirion::readHumidity()
{
  uint16_t _val;                    // Raw humidity value returned from sensor
  float _linearHumidity;       // Humidity with linear correction applied
  float _correctedHumidity;    // Temperature-corrected humidity
  float _temperature;          // Raw temperature value

  // Conversion coefficients from SHT15 datasheet
  const float C1 = -4.0;       // for 12 Bit
  const float C2 =  0.0405;    // for 12 Bit
  const float C3 = -0.0000028; // for 12 Bit
  const float T1 =  0.01;      // for 14 Bit @ 5V
  const float T2 =  0.00008;   // for 14 Bit @ 5V

  // Command to send to the SHT1x to request humidity
  const uint8_t _gHumidCmd = 0b00000101;

  // check cache
  if ( millis() - _lastHumMillis < SHT_CACHE_MILLIS){
	  return _lastHumVal;

  } else{
	  // Fetch the value from the sensor
	  if ( sendCommandSHT(_gHumidCmd, _dataPin, _clockPin) == SHT_SUCCESS ){
		  waitForResultSHT(_dataPin);
		  _val = getData16SHT(_dataPin, _clockPin);

		  uint8_t rxcrc = rev8bits( readCRC(_dataPin, _clockPin ) );
		  uint8_t mycrc = crc8add( 0, _gHumidCmd );
		  mycrc = crc8add( mycrc, _val );
		  if (mycrc != rxcrc){
			Serial.println("SHT: crc error");
			Serial.print(" Got:      0x"); Serial.println(rxcrc, HEX);
			Serial.print(" Expected: 0x"); Serial.println(mycrc, HEX);
			return NAN;
		  }

		  // Apply linear conversion to raw value
		  _linearHumidity = C1 + C2 * _val + C3 * _val * _val;

			// Get current temperature for humidity correction
		  _temperature = readTemperatureC();

		  // Correct humidity value for current temperature
		  _correctedHumidity = (_temperature - 25.0 ) * (T1 + T2 * _val) + _linearHumidity;

		  // cache
		  _lastHumVal = _correctedHumidity;
		  _lastHumMillis = millis();

		  return (_correctedHumidity);
	  }
  }
  //error
  return NAN;
}
Example #5
0
/*
 * Only commands MEASURE_HUMI or MEASURE_TEMP!
 */
static unsigned int
scmd(unsigned cmd)
{
  unsigned long n;

  if(cmd != MEASURE_HUMI && cmd != MEASURE_TEMP) {
    return -1;
  }
  MCUCR |= (1<<JTD);
  MCUCR |= (1<<JTD);

  sreset();
  /* Start transmission */
  if(!swrite(cmd)) {
	  //printf("-2");
	  goto fail;
  }

  for(n = 0; n < 250000; n++) {
    if(!SDA_IS_1) {
      unsigned t0, t1, rcrc;
      t0 = sread(1);
      t1 = sread(1);
      rcrc = sread(0);
      //printf("t0:%d",t0);
      //printf("t1:%d",t1);
#ifdef CRC_CHECK
      {
	unsigned crc;
	crc = crc8_add(0x0, cmd);
	crc = crc8_add(crc, t0);
	crc = crc8_add(crc, t1);
	if(crc != rev8bits(rcrc)) {
		// printf("-3");
		goto fail;
	}
      }
#endif
      MCUCR &= (0<<JTD);
      MCUCR &= (0<<JTD);

      return (t0 << 8) | t1;
    }
  }

 fail:
  sreset();
  //printf("-1");
  MCUCR &= (0<<JTD);
  MCUCR &= (0<<JTD);

  return -1;
}
Example #6
0
uint8_t sensirion::readStatus()
{
  uint16_t _val;

  // Command to send to the SHT1x to request Status
  const uint8_t _gStatCmd  = 0b00000111;

  sendCommandSHT(_gStatCmd, _dataPin, _clockPin);
  // result is immediate with read status.
  //waitForResultSHT(_dataPin);

  shtDelay(1);
  _val = getData16SHT(_dataPin, _clockPin);

  /*
  // Send the required ack
  digitalWrite(_dataPin, LOW);
  pinMode(_dataPin, OUTPUT);
  shtDelay(1);
  digitalWrite(_clockPin, HIGH);
  shtDelay(1);
  digitalWrite(_clockPin, LOW);
  shtDelay(1);
  pinMode(_dataPin, INPUT);
  */

  // end transmission, turn off gpio's
  pinMode(_dataPin, INPUT);
  pinMode(_clockPin, INPUT);

  uint8_t rx_val = _val >> 8;
  uint8_t rx_crc = _val & 0xff;
  uint8_t local_crc = crc8add( 0, _gStatCmd );
  local_crc = crc8add(local_crc, rx_val);

  if (rx_crc != rev8bits(local_crc)){
	Serial.println("SHT: crc error");
	Serial.print(" Got:      0x"); Serial.println(rx_crc, HEX);
	Serial.print(" Expected: 0x"); Serial.println(local_crc, HEX);
  }

  return rx_val;
}