Пример #1
0
int DHT::readData() 
{

	
   int err = ERROR_NONE;
   Timer tmr;

     DigitalInOut data_pin(_pin);
		// We make sure we are the only ones
		// reading data right now.
	   _mutex.lock();
	
    // BUFFER TO RECEIVE
    uint8_t cnt = 7;
    uint8_t idx = 0;
    
    tmr.stop();
    tmr.reset();
 
    // EMPTY BUFFER
    for(int i=0; i< 5; i++) DHT_data[i] = 0;
 
    // REQUEST SAMPLE
    data_pin.output();
    data_pin.write(0);
    wait_ms(18);
    data_pin.write(1);
    wait_us(40);
    data_pin.input();
 
    // ACKNOWLEDGE or TIMEOUT
    unsigned int loopCnt = 10000;
    
    while(!data_pin.read())if(!loopCnt--)
		{
			_mutex.unlock();
			return ERROR_DATA_TIMEOUT;
		}
 
    loopCnt = 10000;
    
    while(data_pin.read())if(!loopCnt--)
		{
			_mutex.unlock();
			return ERROR_DATA_TIMEOUT;
		}
 
    // READ OUTPUT - 40 BITS => 5 BYTES or TIMEOUT
    for(int i=0; i<40; i++){
        
        loopCnt = 10000;
        
        while(!data_pin.read())if(loopCnt-- == 0)
				{
					_mutex.unlock();
					return ERROR_DATA_TIMEOUT;
				}
 
        //unsigned long t = micros();
        tmr.start();
 
        loopCnt = 10000;
        
        while(data_pin.read())if(!loopCnt--)
				{
					_mutex.unlock();
					return ERROR_DATA_TIMEOUT;
				}
 
        if(tmr.read_us() > 40) DHT_data[idx] |= (1 << cnt);
        
        tmr.stop();
        tmr.reset();
        
        if(cnt == 0){   // next byte?
        
            cnt = 7;    // restart at MSB
            idx++;      // next byte!
            
        }else cnt--;
        
    }
 
    // WRITE TO RIGHT VARS
    uint8_t sum = (DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF;  
 
    if(DHT_data[4] != sum)
		{ 
			_mutex.unlock();
			return ERROR_CHECKSUM;
		}
        
    _lastTemperature= CalcTemperature();
    _lastHumidity= CalcHumidity();
    
		_mutex.unlock();
    return err;
}
Пример #2
0
eError DHT::readData()
{
  uint8_t i = 0, j = 0, b = 0, data_valid = 0;
  uint32_t bit_value[DHT_DATA_BIT_COUNT] = {0};

  eError err = ERROR_NONE;
  //time_t currentTime = time(NULL);

  DigitalInOut DHT_io(_pin);

  // IO must be in hi state to start
  if (ERROR_NONE != stall(DHT_io, 0, 250))
  {
    return BUS_BUSY;
  }

  // start the transfer
  DHT_io.output();
  DHT_io = 0;
  // only 500uS for DHT22 but 18ms for DHT11
  (_DHTtype == 22) ? wait_ms(18) : wait(1);
  DHT_io = 1;
  wait_us(30);
  DHT_io.input();
  // wait till the sensor grabs the bus
  if (ERROR_NONE != stall(DHT_io, 1, 40))
  {
    return ERROR_NOT_PRESENT;
  }
  // sensor should signal low 80us and then hi 80us
  if (ERROR_NONE != stall(DHT_io, 0, 100))
  {
    return ERROR_SYNC_TIMEOUT;
  }
  if (ERROR_NONE != stall(DHT_io, 1, 100))
  {
    return ERROR_NO_PATIENCE;
  }
  // capture the data
  for (i = 0; i < 5; i++)
  {
    for (j = 0; j < 8; j++)
    {
      if (ERROR_NONE != stall(DHT_io, 0, 75))
      {
        return ERROR_DATA_TIMEOUT;
      }
      // logic 0 is 28us max, 1 is 70us
      wait_us(40);
      bit_value[i * 8 + j] = DHT_io;
      if (ERROR_NONE != stall(DHT_io, 1, 50))
      {
        return ERROR_DATA_TIMEOUT;
      }
    }
  }
  // store the data
  for (i = 0; i < 5; i++)
  {
    b = 0;
    for (j = 0; j < 8; j++)
    {
      if (bit_value[i * 8 + j] == 1)
      {
        b |= (1 << (7 - j));
      }
    }
    DHT_data[i] = b;
  }

  // uncomment to see the checksum error if it exists
  //printf(" 0x%02x + 0x%02x + 0x%02x + 0x%02x = 0x%02x \n", DHT_data[0], DHT_data[1], DHT_data[2], DHT_data[3], DHT_data[4]);
  data_valid = DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3];
  if (DHT_data[4] == data_valid)
  {
    //_lastReadTime = currentTime;
    _lastTemperature = CalcTemperature();
    _lastHumidity = CalcHumidity();

  }
  else
  {
    err = ERROR_CHECKSUM;
  }

  return err;

}