Appkit:: Using The Ds1620 Digital Thermometer/Thermostat
Appkit:: Using The Ds1620 Digital Thermometer/Thermostat
AppKit:
Using the DS1620 Digital Thermometer/Thermostat
This AppKit shows how to use the Dallas Semiconductor DS1620 Digital Thermometer/ Thermostat chip with PIC
microcontrollers and the Parallax BASIC Stamp ® single-board computer.
Description
The DS1620 is a complete digital thermometer on a chip, capable of replacing the normal combination of temperature sensor
and analog-to-digital converter in most applications. It can measure temperature in units of 0.5° Centigrade (C) from –55° C
to +125° C. [In Fahrenheit (°F), units of 0.9° F and a range of –67° F to +257° F.] Temperature measurements are
expressed as nine-bit, two’s complement numbers. The DS1620 communicates with a microcontroller such as the PIC or
Stamp through a three-wire serial connection.
The DS1620 can also operate as a standalone thermostat. A temporary connection to a controller establishes the mode of
operation and high/low-temperature setpoints. Thereafter, the chip independently controls three outputs: T(high), which
goes active at temperatures above the high-temperature setpoint; T(low), active at temperatures below the low setpoint; and
T(com), which goes active at temperatures above the high setpoint, and stays active until the temperature drops below the
low setpoint.
Hardware interface
The DS1620 interfaces with controllers through a three-wire connection, consisting of a data input/output line (DQ), a
synchronizing clock line (CLK) and a reset/select line (RST).
The figure shows how to connect the DS1620 to the PIC or Stamp for
the demo programs. Do not omit the bypass capacitor—not even if you +5
1k
feel that your power supply is solid and well-filtered. Locate that cap as 1
close as practical to the supply leads of the DS1620. Although the 1k 3 DQ VDD
resistor is not strictly necessary as long as the firmware is functioning CLK T(hi) 0.1µF
2
correctly, it’s best to leave it in. In the event that both the controller (PIC DS1620
RST
or Stamp) and the DS1620 try to drive the data line at the same time, 1 T(lo)
the resistor limits the amount of current that can flow between them to a GND T(com)
safe value.
PIC Stamp
1 ra.0 pin 0
2 ra.1 pin 1
3 ra.2 pin 2
Software interface
From a software standpoint, using the DS1620 boils down to this:
The program listings and data sheets show these processes in detail.
• The fastest the DS1620 can generate new temperature data is once per second. It does no good to read it at shorter
intervals—you’ll simply read the value of the previous temperature measurement.
• The DS1620’s thermostat outputs can source only 1 mA and sink only 4 mA. You’ll need a Darlington-transistor or
logic-level MOSFET switch to turn on a decent-sized load.
; * Note that the DS1620 can only perform one conversion per second.
; With a 4-MHz clock, this demo routine reads it about five times a
; second. This doesn't do any harm, since the DS1620 provides the
; old reading until a new one is available, but it doesn't do any
; good either! In an actual application, you'll want to read the '1620
; only as often as appropriate.
; Shout shifts out the 8 bits of DSdata and, optionally, the ninth bit
; stored in sign. Eight-bit transfers are required for sending instructions
; (called protocols) to the DS1620. Nine-bit transfers are required for
; storing temperature data to the 1620's thermostat registers. In either
; case, the internal loop executes 9 times. For an eight-bit transfer
; (set up by storing a 0 to clk9) the last clock pulse is skipped. Always
; looping 9 times has the benefit of leaving the value of DSdata unaltered
; after a call to Shout. The bits are rotated back into their original
; positions.
; >> Note: Since all communications with the DS1620 begin with the controller
; sending a protocol, Shout contains the "setb RST" command required to
; activate the DS1620. Since communications can end with either an output
; (Shout) or an input (Shin), Shout does _not_ deactivate the DS1620. Make
; sure to take care of this detail in your code that uses this routine.
Shout
mov !ra,#DQout ; Set to output.
mov bits,#9 ; Set up for 8- or 9-bit transfer.
movb c,sign ; Put bit8 (sign bit) into carry.
:begin setb RST ; Activate the 1620.
:loop rr DSdata ; Rotate bit0 of DSdata into carry.
clrb CLK ; Set up for clock pulse
movb DQ,c ; Move carry bit to input of 1620.
mov w,--bits ; Pulse the clock line for each of
snz ; first 8 bits. On the ninth bit,
snb clk9 ; pulse the clock only if clk9 = 1.
setb CLK ; Finish the pulse if conditions are met.
djnz bits,:loop ; Loop 9 times.
ret
; >> Note: When a program receives input from the DS1620, it is always the
; end of the communication, so Shin incorporates the command "clrb RST"
; to deactivate the DS1620.
Shin
mov !ra,#DQin ; Set DQ to input.
mov bits,#9 ; Nine-bit transfer.
clc ; Clear carry bit.
:loop rr DSdata ; Move carry into bit7, shift bits right.
clrb CLK ; Clock in the bit on falling edge.
movb c,DQ ; Get the bit
setb CLK ; Finish the clock pulse.
djnz bits,:loop ; Loop 9 times.
clrb RST ; Deactivate the 1620.
ret
BASIC Stamp I (BS1-IC) and BASIC Stamp Ver. D Program Listing: Thermometer
' Program: DS1620.BS1
' This program interfaces the DS1620 Digital Thermometer to the
' BASIC Stamp. Input and output subroutines can be combined to
' set the '1620 for thermometer or thermostat operation, read
' or write nonvolatile temperature setpoints and configuration
' data.
' The loop below continuously reads the latest temperature data from
' the DS1620. The '1620 performs one temperature conversion per second.
' If you read it more frequently than that, you'll get the result
' of the most recent conversion. The '1620 data is a 9-bit number
' in units of 0.5 deg. C. See the ConverTemp subroutine below.
Again:
pause 1000 ' Wait 1 second for conversion to finish.
let DSout=Rtemp ' Send the read-temperature opcode.
gosub Shout
gosub Shin ' Get the data.
low RSTn ' Deactivate the DS1620.
gosub ConverTemp ' Convert temperature reading to absolute.
gosub DisplayF ' Display in degrees F.
gosub DisplayC ' Display in degrees C.
goto Again
ConverTemp:
if bit8 = 0 then skip ' If temp > 0 skip "sign extension" procedure.
let w0 = w0 | $FE00 ' Make bits 9 through 15 all 1s to make a
' 16-bit two's complement number.
skip:
let w0 = w0 + 110 ' Add 110 to reading and return.
return
DisplayF:
let w1 = w0*9/10 ' Convert to degrees F relative to -67.
if w1 < 67 then subzF ' Handle negative numbers.
let w1 = w1-67
Debug #w1, " F",cr
return
subzF:
let w1 = 67-w1 ' Calculate degrees below 0.
Debug "-",#w1," F",cr ' Display with minus sign.
return
DisplayC:
let w1 = w0/2 ' Convert to degrees C relative to -55.
if w1 < 55 then subzC ' Handle negative numbers.
let w1 = w1-55
Debug #w1, " C",cr
return
subzC:
let w1 = 55-w1 ' Calculate degrees below 0.
Debug "-",#w1," C",cr ' Display with minus sign.
return
BASIC Stamp I (BS1-IC) and BASIC Stamp Ver. D Program Listing: Thermostat
' Program: DS_STAT.BS1
' This program interfaces the DS1620 Digital Thermometer to the
' BASIC Stamp to configure it for thermostat operation. A PC
' running terminal software, should be connected to the Stamp
' with data out to pin 4 (through a 22k resistor) and data in
' to pin 3:
Err:
Serout comOut,N2400,(" ERR") ' Signal data-entry error.
end
Fail:
Serout comOut,N2400,(13," FAIL") ' Signal 1620 write error.
end
' The loop below continuously reads the latest temperature from
' the DS1620.
again:
pause 1000 ' Wait a second between readings.
high RST ' Activate the '1620.
shiftout DQ,CLK,lsbfirst,[Rtemp] ' Request to read temperature.
shiftin DQ,CLK,lsbpre,[DSdata\9] ' Get the temperature reading.
low RST
T_sign = Sign ' Save the sign bit of the reading.
DSdata = DSdata/2 ' Scale reading to whole degrees C.
if T_sign = 0 then no_neg1
DSdata = DSdata | $FF00 ' Extend sign bits for negative temps.
no_neg1:
debug SDEC DSdata," degrees C",cr ' Show signed temperature in C.
DSdata = (DSdata */ $01CC) ' Multiply by 1.8.
if T_sign = 0 then no_neg2 ' If negative, extend sign bits.
DSdata = DSdata | $FF00
no_neg2:
DSdata = DSdata + 32 ' Complete the conversion.
debug SDEC DSdata," degrees F",cr ' Show signed temperature in F.
goto again ' Repeat forever.