From 22c9803382fcba28845a3eeb2e668e7c6cf62ba3 Mon Sep 17 00:00:00 2001 From: Paul Ring Date: Sat, 18 Dec 2010 13:47:05 +0100 Subject: [PATCH] Fixed negative temperature bug In read_temp() the temperature was not converted according to DS1620 specification. --- README | 30 +++++++++++++++--------------- ds1620.cpp | 21 +++++++++++++++++---- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/README b/README index 7e2a9ed..81ca6e0 100755 --- a/README +++ b/README @@ -1,15 +1,15 @@ -Arduino DS1620 Library 0.1 -Copyright (C) 2010 John P. Mulligan. All rights reserved. - -This is an Arduino library for use with the Dallas Semiconductor DS1620 Digital -Thermometer and Thermostat. Uses 3-wire serial communication. - -Latest version of library: - - http://github.com/thinkhole/Arduino-DS1620 - -Project wiki: - - http://wiki.thinkhole.org/ds1620 - -See INSTALL file for installation instructions. +Arduino DS1620 Library 0.2 +Copyright (C) 2010 John P. Mulligan. All rights reserved. + +This is an Arduino library for use with the Dallas Semiconductor DS1620 Digital +Thermometer and Thermostat. Uses 3-wire serial communication. + +Latest version of library: + + http://github.com/thinkhole/Arduino-DS1620 + +Project wiki: + + http://wiki.thinkhole.org/ds1620 + +See INSTALL file for installation instructions. diff --git a/ds1620.cpp b/ds1620.cpp index 2383ca6..d5f4285 100755 --- a/ds1620.cpp +++ b/ds1620.cpp @@ -66,14 +66,27 @@ DS1620::DS1620(int DQ, int CLK, int RST) int DS1620::read_temp() { - int t; - + short t; + rst_start(); send_command(READ_TEMP); // Next 9 clock cycles, last temp conv result - t = receive_data()/2; + t = (short)receive_data(); rst_stop(); + + // Check sign bit from Temp + if (t & 0x0100) + { + // Temperature is negative + // According to specification: [256 (0x0100) - Temp] + t &= 0x00FF; // Remove the sign bit + t = 0x0100 - t; + t *= (short)-1; + } + + t /= 2; + return(t); -} +} void DS1620::write_th(int high_temp) {