## misterhouse-users

 [misterhouse-users] two's complement notation and DS2438 From: Jason I. Comander - 2004-12-24 15:08:37 ```I bought a one-wire humidity sensor/thermometer from AAG electronica and I noticed that the temperature data was reading incorrectly below 0 degrees C. The bug was in Get_Temp_2438{} in device.pm line 1009. The data is stored in 2's complement notation, according to the datasheet http://pdfserv.maxim-ic.com/en/ds/DS2438.pdf After digging out my old electronics textbook I think I figured out the right conversion- seems to work (below). Is there a more elegant way to convert two's complement to decimal? MSB of \$data[4] is sign, rest of \$data[4] is whole degrees (2^6..2^0), top five MSBs of \$data[3] contains fractions of degrees (2^-1...2^-5). Maybe perl has a conversion built in somehow? Thanks, Jason my @data = unpack("C*",\$result); my \$temp; if (\$data[4]>=128) { \$temp = sprintf ("%3.2f",((((256-\$data[4]) * 256) + (256-\$data[3])) * -0.03125 / 8)+1); } else { \$temp = sprintf ("%3.2f",(((\$data[4] * 256) + \$data[3]) * 0.03125 / 8)); } return \$temp; ```
 Re: [misterhouse-users] two's complement notation and DS2438 From: Scott Johnson - 2004-12-27 22:15:45 ```I ran into this last year with my DS18B20's. In Device.PM checkout sub "read_temperature_18B20". As I recall, I only changed the package for the DS1822. ----- Original Message ----- From: "Jason I. Comander" To: Sent: Friday, December 24, 2004 9:08 AM Subject: [misterhouse-users] two's complement notation and DS2438 > > I bought a one-wire humidity sensor/thermometer from AAG electronica and I > noticed that the temperature data was reading incorrectly below 0 degrees > C. The bug was in Get_Temp_2438{} in device.pm line 1009. The data is > stored in 2's complement notation, according to the datasheet > http://pdfserv.maxim-ic.com/en/ds/DS2438.pdf > > After digging out my old electronics textbook I think I figured out the > right conversion- seems to work (below). Is there a more elegant way to > convert two's complement to decimal? MSB of \$data[4] is sign, rest of > \$data[4] is whole degrees (2^6..2^0), top five MSBs of \$data[3] contains > fractions of degrees (2^-1...2^-5). Maybe perl has a conversion built in > somehow? > > Thanks, > Jason > > my @data = unpack("C*",\$result); > my \$temp; > if (\$data[4]>=128) { > \$temp = sprintf ("%3.2f",((((256-\$data[4]) * 256) + (256-\$data[3])) > * -0.03125 / 8)+1); > } else { > \$temp = sprintf ("%3.2f",(((\$data[4] * 256) + \$data[3]) * 0.03125 / 8)); > } > return \$temp; > > > ------------------------------------------------------- > SF email is sponsored by - The IT Product Guide > Read honest & candid reviews on hundreds of IT Products from real users. > Discover which products truly live up to the hype. Start reading now. > http://productguide.itmanagersjournal.com/ > ________________________________________________________ > To unsubscribe from this list, go to: > http://sourceforge.net/mail/?group_id=1365 > ```
 RE: [misterhouse-users] two's complement notation and DS2438 From: Bruce Winter - 2004-12-28 00:24:05 ```> I ran into this last year with my DS18B20's. > > In Device.PM checkout sub "read_temperature_18B20". As I recall, I only > changed the package for the DS1822. > ... > > I bought a one-wire humidity sensor/thermometer from AAG > electronica and I > > noticed that the temperature data was reading incorrectly below > 0 degrees > > C. The bug was in Get_Temp_2438{} in device.pm line 1009. The data is > > stored in 2's complement notation, according to the datasheet > > http://pdfserv.maxim-ic.com/en/ds/DS2438.pdf > > > > After digging out my old electronics textbook I think I figured out the > > right conversion- seems to work (below). Is there a more elegant way to > > convert two's complement to decimal? I'm sure there is a more elegant way, but I don't know it offhand. Let me know if you think we should include your fix, or if Scott's suggestion works and is better. Bruce ```