vuxat / gfb

Automatically exported from code.google.com/p/gfb
0 stars 0 forks source link

Faulty temperature readings in DS2438 library - ds2438::readTempC() looses sign when shifting data from read #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. When the the temperqature drops below zero the sensor gives wrong readings

See: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1289330809/0

What is the expected output? What do you see instead?
expected negative values, see positive

What version of the product are you using? On what operating system?
-

Please provide any additional information below.
In the thread on the forum a solution is proposed by me to change readTempC() 
function to correct the output. This need to be confirmed by someone who has 
such sensor (and a freezer :)
Rob Tillaart

Original issue reported on code.google.com by rob.till...@gmail.com on 10 Nov 2010 at 8:11

GoogleCodeExporter commented 9 years ago
float ds2438::readTempC()
{
  //override for now, plsfixkthx
  _parasite = 1;

  //request temp conversion
  _wire->reset();
  _wire->select(_deviceAddress);
  _wire->write(CONVERTT, _parasite);
  delay(20);

  //copy data from eeprom to scratchpad & read scratchpad
  ScratchPad _scratchPad;
  _readMem(_scratchPad);

  //return tempC (ignore 3 lsb as they are always 0);
  uint8_t x = _scratchPad[TEMP_MSB];
  int rawTemp = (x & 0x80)?(-256+x): x;  // uint8_t => int
  float f = rawTemp + (_scratchPad[TEMP_LSB] >> 3) * 0.03125;

  return f;
}

Original comment by rob.till...@gmail.com on 10 Nov 2010 at 8:16

GoogleCodeExporter commented 9 years ago

simpler patch...

float ds2438::readTempC()
{
  //override for now, plsfixkthx
  _parasite = 1;

  //request temp conversion
  _wire->reset();
  _wire->select(_deviceAddress);
  _wire->write(CONVERTT, _parasite);
  delay(20);

  //copy data from eeprom to scratchpad & read scratchpad
  ScratchPad _scratchPad;
  _readMem(_scratchPad);

  // return tempC (ignore 3 lsb as they are always 0);
  // takes care of signbit when shifting
  int16_t rawTemperature = (((int16_t) _scratchPad[TEMP_MSB]) << 8) | _scratchPad[TEMP_LSB])  >> 3;

return (float)rawTemp * 0.03125;
}

Original comment by rob.till...@gmail.com on 10 Nov 2010 at 11:23

GoogleCodeExporter commented 9 years ago
Thanks Rob, I don't have a spare 2438 to test with right now but I should have 
one next week sometime. I'll test then commit.

Original comment by gfbar...@gmail.com on 12 Nov 2010 at 3:29

GoogleCodeExporter commented 9 years ago
Issue 1 has been merged into this issue.

Original comment by gfbar...@gmail.com on 12 Nov 2010 at 8:00

GoogleCodeExporter commented 9 years ago
"simpler patch" works with this fix:

  int16_t rawTemp = ((((int16_t) _scratchPad[TEMP_MSB]) << 8) | _scratchPad[TEMP_LSB])  >> 3;

Original comment by mart.mai...@gmail.com on 24 Nov 2013 at 10:56