uint8_t DS18B20_Manager_readScratchpad(DS18B20_Manager_t *a_pMe)
{
	uint8_t retVal = DS18B20_OK;
	OneWireBusStatus_t oneWireBusStatus;
	uint8_t u8crc8 = 0;	// CRC8 initial value
	uint8_t u8Idx;
	
	// Send reset pulse to sensor
	oneWireBusStatus = OneWireMaster_reset(a_pMe->oneWireBusMasterPtr);
	
	if (OneWireBus_DEVICE_PRESENT == oneWireBusStatus)
	{
		
		// Device is present. Issue Skip ROM command to address all devices.
		OneWireBusMaster_writeByte(a_pMe->oneWireBusMasterPtr, SKIP_ROM_18B20);
		
		// Issue read scratchpad function command
		OneWireBusMaster_writeByte(a_pMe->oneWireBusMasterPtr, READ_SCRATCHPAD_18B20);
		
		// Read scratchpad: 9 bytes 
		for (u8Idx = 0; u8Idx < SCRATCHPAD_SIZE_18B20; u8Idx++)
		{
			a_pMe->scratchpad[u8Idx] = OneWireBusMaster_readByte(a_pMe->oneWireBusMasterPtr);
		}

		// Calculate CRC8 for received scratchpad data
		for (u8Idx = 0; u8Idx < (SCRATCHPAD_SIZE_18B20 - 1); u8Idx++)
		{
			u8crc8 = OneWire_crc8(a_pMe->scratchpad[u8Idx], u8crc8);
		}
		
		// Check data integrity
		if (u8crc8 != a_pMe->scratchpad[SCRATCHPAD_SIZE_18B20-1])
		{
			retVal = DS18B20_CRC_ERROR;
		}
		else
		{
			// Nothing to do
		}
	}
	else
	{
		// No device present or short circuit
		retVal = DS18B20_ERROR;
	}
	
	return retVal;
}
void Sensors_Poll(void)
{
  /* This function must be call 100 ms periods */
  
//  // check warning level
//  if (sensors.Gas > __flash_data._thesis._data.Gas || 
//      sensors.Lighting > __flash_data._thesis._data.Lighting || 
//        sensors.TempC > __flash_data._thesis._data.TempC)
//  {
//    // turn on buzzer if enable
//    if (__flash_data._thesis._output.Buzzer)
//    {
//      TurnBuzzerOn();
//    }
//    // turn on speaker if enable
//    if (__flash_data._thesis._output.Speaker)
//    {
//      TurnSpeakerOn();
//    }
//    // turn on relay if enable
//    if (__flash_data._thesis._output.Relay)
//    {
//      TurnRelayOn();
//    }
//  }
//  else
//  {
//    // turn off buzzer if enable
//    if (__flash_data._thesis._output.Buzzer)
//    {
//      TurnBuzzerOff();
//    }
//    // turn off speaker if enable
//    if (__flash_data._thesis._output.Speaker)
//    {
//      TurnSpeakerOff();
//    }
//    // turn off relay if enable
//    if (__flash_data._thesis._output.Relay)
//    {
//      TurnRelayOff();
//    }
//  }
  
  // recalculate sensor values
  sensors.Gas = (ADCConvertedValue[0] + ADCConvertedValue[2] + ADCConvertedValue[4])/3;
  sensors.Lighting = (ADCConvertedValue[1] + ADCConvertedValue[3] + ADCConvertedValue[5])/3;
  
#if SENSORS_DEBUG
  if (SS_Debug_delay == 0)
  {
    USART1_SendStr("\nGas Value: ");
    USART1_SendFloat(sensors.Gas);
    USART1_SendStr(" kppm.\n");
    USART1_SendStr("\nLighting Value: ");
    USART1_SendFloat(sensors.Lighting);
    USART1_SendStr(" Lux.\n");
    SS_Debug_delay = 10;
  }
  else
  {
    SS_Debug_delay--;
  }
#endif
  
  switch (OW_CurrState)
  {
  case OneWire_Idle:
    if ( !OneWire_search(addr)) {
#if SENSORS_DEBUG
			USART1_SendStr("\nNo more addresses.\n");
			USART1_SendStr("\n\n");
#endif
			OneWire_reset_search();
      // delay 300 ms before try again
      OW_CurrState = OneWire_Delay;
      OW_NextState = OneWire_Idle;
      OW_Delay = 3;
		}
    else
    {
      OW_CurrState = OneWire_Delay;
      OW_NextState = OneWire_StartConv;
      OW_Delay = 3;
    }
    break;
  case OneWire_Delay:
    if (OW_Delay == 0)
    {
      // switch state
      OW_CurrState = OW_NextState;
    }
    else
    {
      OW_Delay--;
    }
    break;
  case OneWire_StartConv:
#if SENSORS_DEBUG
    USART1_SendStr("ROM =");
    for( i = 0; i < 8; i++) {
      USART1_SendChar(' ');
      USART1_SendByte(addr[i], HEX);
    }
#endif
    
    if (OneWire_crc8(addr, 7) != addr[7]) {
#if SENSORS_DEBUG
      USART1_SendStr("\nCRC is not valid!\n");
#endif
      // goto delay 300ms before switch to idle to find device again
      OW_CurrState = OneWire_Delay;
      OW_NextState = OneWire_Idle;
      OW_Delay = 3;
    }
    
#if SENSORS_DEBUG
    USART1_SendStr("\n\n");
#endif
    
    // the first ROM byte indicates which chip
    switch (addr[0]) {
    case 0x10:
#if SENSORS_DEBUG
      USART1_SendStr("\n  Chip = DS18S20\n");  // or old DS1820
#endif
      type_s = 1;
      break;
    case 0x28:
#if SENSORS_DEBUG
      USART1_SendStr("\n  Chip = DS18B20\n");
#endif
      type_s = 0;
      break;
    case 0x22:
#if SENSORS_DEBUG
      USART1_SendStr("\n  Chip = DS1822\n");
#endif
      type_s = 0;
      break;
    default:
#if SENSORS_DEBUG
      USART1_SendStr("\nDevice is not a DS18x20 family device.\n");
#endif
      
      // goto delay 300ms before switch to idle to find device again
      OW_CurrState = OneWire_Delay;
      OW_NextState = OneWire_Idle;
      OW_Delay = 3;
      break;
    } 
    
    OneWire_reset();
    OneWire_select(addr);
    OneWire_write(0x44, 1);        // start conversion, with parasite power on at the end
    
    // goto delay 1000ms before read data
    OW_CurrState = OneWire_Delay;
    OW_NextState = OneWire_GetValue;
    OW_Delay = 10;
    break;
  case OneWire_GetValue:
    present = OneWire_reset();
    OneWire_select(addr);    
    OneWire_write(0xBE, 0);         // Read Scratchpad
    
#if SENSORS_DEBUG
    USART1_SendStr("  Data = ");
    USART1_SendByte(present, HEX);
    USART1_SendStr(" ");
#endif
    for ( i = 0; i < 9; i++) {           // we need 9 bytes
      data[i] = OneWire_read();
#if SENSORS_DEBUG
      USART1_SendByte(data[i], HEX);
      USART1_SendStr(" ");
#endif
    }
#if SENSORS_DEBUG
    USART1_SendStr(" CRC=");
    USART1_SendByte(OneWire_crc8(data, 8), HEX);
    USART1_SendStr("\n\n");
#endif
    
    // Convert the data to actual temperature
    // because the result is a 16 bit signed integer, it should
    // be stored to an "int16_t" type, which is always 16 bits
    // even when compiled on a 32 bit processor.
    int16_t raw = (data[1] << 8) | data[0];
    if (type_s) {
      raw = raw << 3; // 9 bit resolution default
      if (data[7] == 0x10) {
        // "count remain" gives full 12 bit resolution
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
    } else {
      byte cfg = (data[4] & 0x60);
      // at lower res, the low bits are undefined, so let's zero them
      if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
      else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
      else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
      //// default is 12 bit resolution, 750 ms conversion time
    }
    celsius = (float)raw / 16.0;
    fahrenheit = celsius * 1.8 + 32.0;
#if SENSORS_DEBUG
    USART1_SendStr("  Temperature = ");
    USART1_SendFloat(celsius);
    USART1_SendStr(" Celsius, ");
    USART1_SendFloat(fahrenheit);
    USART1_SendStr(" Fahrenheit\n");
#endif
    sensors.TempC = celsius;
    
    // goto delay 300ms before next convert
    OW_CurrState = OneWire_Delay;
    OW_NextState = OneWire_StartConv;
    OW_Delay = 3;
    break;                   
  } // switch
  
}