Shop OBEX P1 Docs P2 Docs Learn Events
DS1620 and Measuring Temp with Tenths Accuracy — Parallax Forums

DS1620 and Measuring Temp with Tenths Accuracy

WavemanWaveman Posts: 20
edited 2007-06-19 08:44 in BASIC Stamp
I modified the existing code for the DS1620 two measure two DS1620 temp sensors.
I was wondering if anyone can suggest some code modifications to measure with tenths
accuracy, such as 23.5 deg. Currently only displays the whole number. DS1620 data sheet
says it can measure in increments of 0.5 deg.



' {$STAMP BS2}
' {$PBASIC 2.5}
' Program: DS1620.BS2 (interface DS1620 digital thermometer to BS2)
' This program interfaces the DS1620 Digital Thermometer to
' the BS2. Input and output routines can be combined to set
' the '1620 for thermometer or thermostat operation, read
' or write nonvolatile temperature setpoints and configuration
' data. In addition to using the BS2's new Shiftin and Shiftout
' instructions to communicate with the 1620, this program uses
' new math and display operators that work with signed integers.
' This makes it relatively easy to convert between degrees C and
' F and to display both positive and negative temperature
' readings. Note that after math operations on negative numbers
' it's necessary to "extend the sign bits." All this means is
' setting all of the bits to the left of actual value to 1s.
' Also note the use of the new */ (pronounced 'star-slash')
' operator. This works like multiplying by an integer (0-255)
' and a fraction (in units of 1/256). For example, to multiply
' 17 by Pi (approx 3.1416) would be written "17 */ $0324."
' The second value is written in hex to emphasize that it's being
' split into bytes: $03 is the integer and $24/$100 is the fraction.
' In the C-to-F conversion below, we multiply the C value by 1.8
' with "*/ $01CC" since $CC/$FF (204/256) = 0.8.
' ===================== Define Pins and Variables ================
' Define for DS1620 Temp Sensor 1 and Temp Sensor 2

DQ1 CON 2··································· ' PIN 2 <=> DQ.
DQ2 CON 4··································· ' PIN 4 <=> DQ.
CLK CON 1··································· ' Pin 1 => CLK.
RST1 CON 0·································· ' Pin 0 => RST (high = active).
RST2 CON 3·································· ' Pin 3 => RST (high = active).
DSdata1 VAR Word···························· ' Word variable to hold 9-bit data.
DSdata2 VAR Word···························· ' Word variable to hold 9-bit data.
Sign1 VAR DSdata1.BIT8······················ ' Sign bit of raw temperature data.
Sign2 VAR DSdata2.BIT8······················ ' Sign bit of raw temperature data.
T_sign1 VAR Bit····························· ' Saved sign bit for converted temperature.
T_sign2 VAR Bit····························· ' Saved sign bit for converted temperature.
' ===================== Define DS1620 Constants ===================
' >>> Constants for configuring the DS1620
Rconfig CON $AC···························· ' Protocol for 'Read Configuration.'
Wconfig CON $0C···························· ' Protocol for 'Write Configuration.'
CPU CON %10································ ' Config bit: serial thermometer mode.
ONeshot CON %01···························· ' Config bit: one conversion per start request.
Cont CON %00······························· ' Config bit: continuous conversions after start.
' >>> Constants for serial thermometer applications.
StartC CON $EE····························· ' Protocol for 'Start Conversion.'
StopC CON $22······························ ' Protocol for 'Stop Conversion.'
Rtemp CON $AA······························ ' Protocol for 'Read Temperature.'
MoveTo········· CON···· 2······················ ' for DEBUG control
ClrRt·········· CON···· 11····················· ' clear DEBUG line to right
' ===================== Begin Program ============================
LOW RST1···································· ' Deactivate '1620#1 for now.
HIGH CLK
PAUSE 100
HIGH RST1
SHIFTOUT DQ1,CLK,LSBFIRST,[noparse][[/noparse]Wconfig,CPU+Cont] ' ..temp conversions.
LOW RST1···································· ' Deactivate '1620#2 for now.
PAUSE 50
HIGH RST1
SHIFTOUT DQ1,CLK,LSBFIRST,[noparse][[/noparse]StartC]·········· ' Send #1 start-conversion protocol.
LOW RST1
LOW RST2
HIGH CLK
PAUSE 100
HIGH RST2
SHIFTOUT DQ2,CLK,LSBFIRST,[noparse][[/noparse]Wconfig,CPU+Cont] ' ..temp conversions.
LOW RST2
PAUSE 50
HIGH RST2
SHIFTOUT DQ2,CLK,LSBFIRST,[noparse][[/noparse]StartC]·········· ' Send #2 start-conversion protocol.
LOW RST2

' The loop below continuously reads the latest temperature from
' the DS1620.
again:
PAUSE 500···································· ' Wait 0.5 second between readings.
HIGH RST1···································· ' Activate the '1620 #1.
SHIFTOUT DQ1,CLK,LSBFIRST,[noparse][[/noparse]Rtemp]············ ' Request to read #1 temperature.
SHIFTIN DQ1,CLK,LSBPRE,[noparse][[/noparse]DSdata1\9]··········· ' Get the temperature reading.
LOW RST1
T_sign1 = Sign1······························ ' Save the sign bit of the reading #1.
DSdata1 = DSdata1/2·························· ' Scale reading to whole degrees C of#1.
HIGH RST2···································· ' Activate the '1620 #2.
SHIFTOUT DQ2,CLK,LSBFIRST,[noparse][[/noparse]Rtemp]············ ' Request to read #2 temperature.
SHIFTIN DQ2,CLK,LSBPRE,[noparse][[/noparse]DSdata2\9]··········· ' Get the temperature reading.
LOW RST2
T_sign2 = Sign2······························ ' Save the sign bit of the reading #2.
DSdata2 = DSdata2/2·························· ' Scale reading to whole degrees C of#2.
DEBUG CLS
'DEBUG MoveTo, 0, 3
DEBUG "Temp Sensor #1", CR
DEBUG "
", CR
DEBUG SDEC DSdata1," Sensor 1 degrees C",CR·· ' Show signed temperature in C.
DSdata1 = (DSdata1 */ $01CC)················· ' Multiply by 1.8.
DSdata1 = DSdata1 + 32······················· ' Complete the conversion.
DEBUG SDEC DSdata1," Sensor 1 degrees F",CR·· ' Show signed temperature in F.
DEBUG "············· ", CR
DEBUG "Temp Sensor #2", CR
DEBUG "
", CR
DEBUG SDEC DSdata2," Sensor 2 degrees C",CR·· ' Show signed temperature in C.
DSdata2 = (DSdata2 */ $01CC)················· ' Multiply by 1.8.
DSdata2 = DSdata2 + 32······················· ' Complete the conversion.
DEBUG SDEC DSdata2," Sensor 2 degrees F",CR·· ' Show signed temperature in F.
GOTO again··································· ' Repeat forever.

Comments

  • Tracy AllenTracy Allen Posts: 6,664
    edited 2007-06-17 00:52
    The program needs to hang on to the resolution and not divide by two. Things have to be done a little differently in the calculation and in the display, for example:

    HIGH RST1                                     ' Activate the '1620 #1.
    SHIFTOUT DQ1,CLK,LSBFIRST,[noparse][[/noparse]Rtemp]             ' Request to read #1 temperature.
    SHIFTIN DQ1,CLK,LSBPRE,[noparse][[/noparse]DSdata1\9]            ' Get the temperature reading.in units of 0.5 degree
    LOW RST1
    '  T_sign1 = Sign1                               ' not necessary to Save the sign bit of the reading #1.
    '  DSdata1 = DSdata1/2                           ' NO! this throws away resolution
    DSdata1.byte1 = -DSdata1.bit8    ' extend sign to 16 bits
    DSdata1=DSdata1 * 5   ' units of 0.1 degree with 0.5 degree resolution
    DEBUG REP "-"\DSdata.bit15, DEC ABS DSdata1/10, ".", DEC1 ABS DSdata1  ' display degC with sign and decimal
    DSdata1 = DSdata1 + 1000 * 9 / 5 + 320 - 1800  ' to Fahrenheit with care for negative, units of 0.1 degF
    DEBUG REP "-"\DSdata.bit15, DEC ABS DSdata1/10, ".", DEC1 ABS DSdata1  ' display degF with sign and decimal
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • Tcpip_v6Tcpip_v6 Posts: 15
    edited 2007-06-19 08:44
    See page 161 of the Stampworks manual. http://www.parallax.com/dl/docs/books/sw/Web-SW-v2.1.pdf
Sign In or Register to comment.