*copyright, Peter H. Anderson, Baltimore, MD, July,
'98*

(A copy of this, including the figures may be obtained from the author for
$1.00. There is no
postage cost in the United States.)

**Introduction.**

This discussion focuses on interfacing the Basic Stamp 2 with an Analog
Devices TMP03 or
TMP04 Temperature Sensor.

Towanda Malone is currently attempting to extend the range of the
distant sensor to up to 4000
feet using a pair of Analog Devices ADM485 RS-485 Transceivers. She is
also working on
routines to interface the TMP04 with a PIC.

Data sheets for the TMP03 and 04 and ADM485 are available from
http://www.analog.com. The
devices are available from Newark at http://www.newark.com. We also sell
them at about the
same price.

**Theory of Operation.**

The TMP03/04 temperature sensor outputs a pulse train of nominally 35
pulses per second at 25
degrees C where the temperature is a ratio of the high time to the low
time;

T_C = 235 - (400 * T1 / T2)

or T_F = 455 - (720 * T1 / T2)

where T1 is the high time and T2 is the low time. It is important to
note that the temperature is
the ratio of the two quantities.

For 0.1 degree resolution the above may be expressed as;

T_C_10 = 2350 - (4000 * T1)/T2 or T_F_10 = 4550 - (7200 * T1) / T2

The TMP03/04 has a lot of advantages over the DS1620 in that only one
Stamp I/O pin is
required, it is simple and it is inexpensive. Using an inexpensive ADM485
as a transmitter at the
remotely located and a receiver at the Stamp, precise measurements at
ranges of up to 4000 feet
may be achieved.

T1 is relatively constant for a specific device and the specified
maximum is 12 ms. T2 increases
with temperature. Note that at 125 degrees C, the maximum value of T2
would be 43 ms. These
values dovetail nicely with the Stamps PULSIN command where values in the
range of 0 to 131
ms may be accommodated with a resolution of 0.002 ms.

**Program TMP04_1.BS2.**

Program TMP04_1.BS2 illustrates the simplicity of interfacing with the
device. T1 and T2 are
measured using the PULSIN command.

Note that the Stamp's inherent arithmetic capabilities are limited to
16 bits. However, it is quite
simple to write routines to perform 16 by 16 bit multiplication and 32-bit
divided by 16-bits.

A routine to multiply two 16-bit numbers, in this case 4000 and T1, is
presented which simply
adds T1 to a 32-bit quantity 4000 times. A routine to perform a 32-bit
divided by a 16-bit
number is also presented. This is simply a matter of counting the number
of times T2 can be
subtracted from the 32-bit quantity.

' TMP04_1.BS2 ' ' Illustrates how to make a measurement using an Analog Devices ' TMP03 or TMP04. ' ' Measures high (T1) and low (T2) times and calculates temperature. ' ' T_C_10 = 2350-(4000*T1)/T2 ' T_F_10 = 4550-(7200*T1)/T2 ' ' TMP04 is connected to Stamp I/O terminal P0. ' ' Where T_C_10 is temperature in degrees C times ten and T_F_10 is ' temperature in degrees F times 10. ' ' copyright, Towanda Malone, Baltimore, MD, July, '98 T1 VAR WORD 'high time T2 VAR WORD 'low time MULTR_H VAR WORD 'result of 16*16 multiplication MULTR_L VAR WORD M1 VAR WORD 'for M1 = 1 to 4000 DIV_R VAR WORD '32 bit divided by 16 bit TEMP VAR WORD 'temporary variable used for calculations T_C_10 VAR WORD T_F_10 VAR WORD MAIN: PULSIN 0, 1, T1 'get the high time T1 PULSIN 0, 0, T2 'get the low time T2 'T1 = 6000 'used for debugging arithmetic routines 'T2 = 11000 'without the TMP04 connected. IF ((T1 = $FFFF) OR (T2 = $FFFF)) THEN INVALID MULTR_H = 0 'initialize to zero MULTR_L = 0 GOSUB TC_CALC 'calculate using 2350 - (4000 * T1) / T2 DEBUG CR GOSUB TF_CALC 'calculate using 4550 - (7000 * T1) / T2 DEBUG CR GOTO MAIN INVALID: DEBUG "No Reading", CR GOTO MAIN TC_CALC: ' 2350 - (4000 * T1)/T2 TC_MULT: ' 4000 * T1 FOR M1 = 1 TO 4000 'for degrees C TEMP = MULTR_L + T1 IF (TEMP > MULTR_L) THEN TC_MULT_NO_CARRY MULTR_H = MULTR_H + 1 TC_MULT_NO_CARRY: MULTR_L = TEMP NEXT TC_DIVIDE: 'now (MULTR_H AND MULT_L)/T2 DIV_R=0 TC_DIV_CONT: TEMP = MULTR_L - T2 IF (TEMP < MULTR_L) THEN TC_DIV_NO_BORROW MULTR_H = MULTR_H -1 IF (MULTR_H = $FFFF) THEN TC_DIV_DONE TC_DIV_NO_BORROW: MULTR_L = TEMP DIV_R = DIV_R + 1 GOTO TC_DIV_CONT TC_DIV_DONE: 'Now 2350 - DIVR for degrees C T_C_10 = 2350 - DIV_R DEBUG "HEX = ",HEX T_C_10, TAB ,"DEC = ",DEC T_C_10, CR TC_DISPLAY: DEBUG "T_C = ", DEC T_C_10 / 10 ,".", DEC T_C_10// 10,CR PAUSE 5000 'five second pause RETURN TF_CALC: '4550 - (7000 * T1) / T2 'Perform 7000*T1 multiplication TF_MULT: FOR M1 = 1 TO 7000 'for degrees F TEMP = MULTR_L + T1 IF (TEMP > MULTR_L) THEN TF_MULT_NO_CARRY MULTR_H = MULTR_H + 1 TF_MULT_NO_CARRY: MULTR_L = TEMP NEXT TF_DIVIDE: 'now (MULTR_H AND MULT_L/T2) DIV_R=0 TF_DIV_CONT: TEMP = MULTR_L - T2 IF (TEMP < MULTR_L) THEN TF_DIV_NO_BORROW MULTR_H = MULTR_H -1 IF (MULTR_H = $FFFF) THEN TF_DIV_DONE TF_DIV_NO_BORROW: MULTR_L = TEMP DIV_R = DIV_R + 1 GOTO TF_DIV_CONT TF_DIV_DONE: 'Now 4550 - DIVR for degrees F T_F_10 = 4550 - DIV_R DEBUG "HEX = ", HEX T_F_10, TAB,"DEC = ", DEC T_F_10, CR TF_DISPLAY: DEBUG "T_F = ", DEC T_F_10 / 10 ,".", DEC T_F_10// 10, CR PAUSE 5000 'five second pause RETURN