Interfacing a Basic Stamp 2 with the Serial Temperature Measurement System

This, together with the referenced figures, assembly instructions and other examples, is included in the documentation which is shipped with the serial temperature kit.

Introduction.

A suggested Basic Stamp 2 interface is illustrated in Figure #11.

Note that Stamp output P1 is connected to /MEAS. Output P1 is normally high and the serial temperature system is idle. (Note that MEAS is grounded).

When a measurement is desired, Stamp output P1 is brought low which initiates a measurement sequence.

The serial data is then sent to the Stamp via the MAX232. Note that the serial temperature measurement system sends 2400 baud non inverted. That is, the idle condition is a TTL logic one. The START bit is a TTL logic zero, the eight data bits are sent, followed by the STOP bit which is a TTL logic one. However, the MAX232 converts a TTL logic one and zero to less than -3V and greater than +3V respectively.

On the Stamp side, there is no similar conversion from EIA levels to TTL levels. Thus, a logic one (less than -3V) is read by the Stamp as a TTL logic zero, and a logic zero (greater than +3V) is read as a TTL logic one. This inversion is acceptable if one configures the Stamp to receive 2400 Inverted.

Note that a 22K series limiting resistor is provided to protect the Stamp from the +10V and -10V signals seen at the output of the MAX232.

It is suggested that when using the Stamp, the temperature measurement unit be configured for the multi line format mode. This is done by grounding the 1_LINE option input to the temperature processor.

The reasoning is that the use of the multi line mode requires only three variables; channel, T_C which is the integer value of the temperature and FRACTION which is the fractional value.

Program T_MEAS.BS2.

This simply initiates a measurement sequence and then reads the results of each of the eight measurements. The characters are converted to decimal and then displayed using the DEBUG command.

The intent is to simply demonstrate the operation. Once you have read the temperatures, you might desire to compare one or more against either a high or low threshold or both and use the Stamp to turn on fans, air conditioning, heat or simply to sound an alarm. You might desire to log the T_C and fraction to a serial EEPROM for later uploading to a PC. The number of possibilities are endless.

A measurement sequence is initiated by bringing output P1 low for 100 msecs.

The first measurement is then read using the SERIN command;

     SERIN 0, BAUDMODE, 5000, TIMEOUT, [DEC1 CHANNEL, SKIP 1, 
              SDEC T_C, SKIP 1, DEC2 FRACTION, SKIP 2]

Note that this must all be on one line. I found it necessary to split the line for this discussion.

Recall that the format of the data is;

     0 -12.7

Followed by a return (ASCII 0DH) and linefeed (ASCII 0AH).

A literal translation of this is, "receive the serial bit stream (2400 inverted) on pin P0. If more than 5000 msecs elapses, jump to location TIMEOUT. Strip off the first received character, convert it to decimal and save in variable CHANNEL. Skip the next character (a space). Convert the next to a signed decimal and save in T_C. Skip the decimal point. Convert the next two digits to a decimal value and save in variable FRACTION. Finally, skip the return and linefeed characters.

The serial temperature system will now provide a one second delay before sending the next value. This provides the user with a period of time to display the data, save it to EEPROM or compare it with alarm thresholds. However, it is important to get back to read the next value within the second or portions of the serial string will be lost.

Note that T_C is defined to be a signed byte and if a quantity is negative, the Stamp saves the quantity in two's complement form and is thus capable of storing values in the range of 0 to 127 and -1 to -128 degrees. In the example, 12 is;

     0 0 0 0  1 1 0 0 

The two's complement is;

     1 1 1 1  0 0 1 1 + 0 0 0 0 0 0 0 1 

or      1 1 1 1  0 1 0 0

or 244 unsigned decimal.

A simple test as to whether a quantity is negative is to test the most significant bit as to whether it is a one, or if the quantity is 128 or greater. In this routine, if the quantity is >= 128, a minus sign is displayed followed by the two's complement of the value. For example;

     1 1 1 1  0 1 0 0

     0 0 0 0  1 0 1 1 + 0 0 0 0  0 0 0 1

or   0 0 0 0  1 1 0

or 12 decimal.

Don't worry too much if you are confused. My students are always confused with two's complement and my best advice is to take it on faith. It works. Keep tinkering with it, and one beauty of the Stamp is that it permits you to tinker, and over time, you will grasp it.

There is an annoying aspect of the Stamp when using the SLEEP or NAP commands in that the Stamp periodically "wakes up" every 2.1 seconds and causes a momentary high impedance state. This will not affect this design as the temperature measurement system is idle when P1 is at a logic one. During the brief high impedance state, the temperature measurement system continues to see the logic level as a logic one.

'T_MEAS.BS2
'
' This is a simple routine to initiate a measurement sequence and then
' read each of eight measurments from the serial temperature system.
'
' The result of each measurement is displayed using the debug command.
'
' Continual loop with 10 second delay after receipt of the last
' measurement.
'
' Copyright, Peter H. Anderson, August 18, '97


BAUDMODE  CON         396 + $4000      '2400 Inverted

CHANNEL   VAR      BYTE
T_C       VAR      BYTE
FRACTION  VAR      BYTE
N         VAR      BYTE

 
DIR0 = 0  ' serial input
DIR1 = 1  ' output.  A logic zero initiates initiates a 
          ' measurement sequence.


TOP:
     OUT1=0         ' bring low to start measurement sequence
     PAUSE 100
     OUT1=1
        FOR N=1 TO 8
        SERIN 0, BAUDMODE, 5000, TIMEOUT, [DEC1 CHANNEL, SKIP 1, 
                    SDEC T_C, SKIP 1, DEC2 FRACTION, SKIP 2]
        DEBUG DEC CHANNEL
        DEBUG " "
        IF T_C<128 THEN AROUND 	' if its negative
           T_C = T_C^$FF +1	' take 2's complement
           DEBUG "-"
AROUND:
        DEBUG DEC T_C
        DEBUG "."
        DEBUG DEC FRACTION
        DEBUG 13		' return
     NEXT 
     SLEEP 10
     GOTO TOP

TIMEOUT:
     DEBUG "TIMEOUT"
     DEBUG 13
     GOTO TOP