100% found this document useful (1 vote)
66 views26 pages

Description: 1k DQ CLK RST GND VDD T (Hi) 1

The digital thermometer chip can replace a temperature sensor and analog-to-digital converter. It measures temperature from -55°C to 125°C in 0.5°C increments and communicates with a microcontroller via a three-wire serial connection. The chip can operate as a standalone thermostat or as a sensor connected to a microcontroller. The PIC microcontroller code samples show how to initialize the chip, take temperature readings, and interface with the chip using defined protocols.

Uploaded by

lgrome73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
66 views26 pages

Description: 1k DQ CLK RST GND VDD T (Hi) 1

The digital thermometer chip can replace a temperature sensor and analog-to-digital converter. It measures temperature from -55°C to 125°C in 0.5°C increments and communicates with a microcontroller via a three-wire serial connection. The chip can operate as a standalone thermostat or as a sensor connected to a microcontroller. The PIC microcontroller code samples show how to initialize the chip, take temperature readings, and interface with the chip using defined protocols.

Uploaded by

lgrome73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Description

The 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, twos complement numbers. The thermometer communicates with a microcontroller such as the PIC or Stamp through a three-wire serial connection. The thermometer 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 thermometer 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 thermometer to the PIC or Stamp for the demo programs. Do not omit the bypass capacitornot even if you feel that your power supply is solid and well-filtered. Locate that cap as close as practical to the supply leads of the thermometer. Although the 1k 3 resistor is not strictly necessary as long as the firmware is functioning 2 correctly, its best to leave it in. In the event that both the controller (PIC or Stamp) and the thermometer try to drive the data line at the same time, 1 the resistor limits the amount of current that can flow between them to a safe value.

+5

1k

1 DQ CLK RST GND VDD T(hi) T(lo) T(com)

0.1F

DS1620

PIC

Stamp

1 2 3

ra.0 ra.1 ra.2

pin 0 pin 1 pin 2

Software interface
From a software standpoint, using the thermometer boils down to this: (1) Activate RST by taking it high. (2) Send an instruction (protocol) to the thermometer telling it what you want to do. (3) If you are reading data, shift it into the controller (PIC or Stamp). (4) If you are writing data, shift it out to the thermometer (5) Deactivate RST by taking it low. The program listings and data sheets show these processes in detail.

Tips for using the thermometer


Data written to the thermometer configuration or temperature-setpoint registers is stored in EEPROM. It takes as long as 50 milliseconds (ms) to complete the write. Be sure to program a delay of at least this length before sending further commands to the thermometer. The fastest the thermometer can generate new temperature data is once per second. It does no good to read it at shorter intervalsyoull simply read the value of the previous temperature measurement. The thermometers thermostat outputs can source only 1 mA and sink only 4 mA. Youll need a Darlington-transistor or logic-level MOSFET switch to turn on a decent-sized load.

PIC Program Listing


; Program: thermometer ; This program presents PIC assembly language subroutines for communicating ; with the digital thermometer chip. It also provides tables of ; predefined constants called "protocols" that control the chip's various ; operating modes. This demo configures the thermometer as a temperature ; sensor slaved to a controller ("CPU" mode) and requests that it perform ; continuous temperature measurements. The thermometer may also be configured as ; a standalone thermostat with setpoint stored in nonvolatile EEPROM. ; ========================= Define I/O Pins ======================= RST = ra.0 ; Reset-- 0 = inactive, 1 = active. CLK = ra.1 ; Clock pin. DQ = ra.2 ; Data in/out. DQin = 4 ; TRIS ra for DQ = input DQout = 0 ; TRIS ra for DQ = output. ; ======================== Define Variables ======================= org 8 ; RAM above special-function registers. DSdata ds 1 ; Byte for I/O with thermometer bits ds 1 ; Number of clock cycles for serial shifts. flags ds 1 ; Bit flags. temp1 ds 1 ; Counter for delays. temp2 ds 1 ; Counter for delays. sign = flags.0 ; Ninth bit of DSdata for temperature values. clk9 = flags.1 ; bit flag: 1= 9-bit xfer, 0= 8-bit xfer. ; ===================== Define DS1620 Constants =================== ; >>> Constants for configuring the DS1620 Rconfig = 0ACh ; Protocol for 'Read Configuration.' Wconfig = 00Ch ; Protocol for 'Write Configuration.' CPU = 10b ; Config bit: serial thermometer mode. NoCPU = 00b ; Config bit: standalone thermostat mode. OneShot = 01b ; Config bit: one conversion per start request. Cont = 00b ; Config bit: continuous conversions after start. ; >>> Constants for serial thermometer applications. StartC = 0EEh ; Protocol for 'Start Conversion.' StopC = 022h ; Protocol for 'Stop Conversion.' Rtemp = 0AAh ; Protocol for 'Read Temperature.' ; >>> Constants for programming thermostat functions. RhiT = 0A1h ; Protocol for 'Read High-Temperature Setting.' WhiT = 001h ; Protocol for 'Write High-Temperature Setting.' RloT = 0A2h ; Protocol for 'Read Low-Temperature Setting.' WloT = 002h ; Protocol for 'Write Low-Temperature Setting.'

PIC Program Listing (cont.)


; ================= Set Up Device and Start Program =============== device pic16c54,xt_osc,wdt_off,protect_off reset start org 0 ; Main program start. ; To demonstrate communication with the DS1620, we're going to set its ; configuration register to CPU mode and continuous conversion, then ; continuously read the temperature. The results of the temperature readings ; will be written to port RB (eight lsbs) and ra.3 (msb). You can view the readings ; (in binary) by connecting LEDs to these pins through 220-ohm resistors. start mov mov mov mov clrb setb mov call mov OR call clrb call mov call clrb call mov call call movb mov jmp ra,#0 rb,#0 !ra,#DQout !rb,#0 clk9 CLK DSdata,#Wconfig Shout DSdata,#CPU DSdata,#Cont Shout RST delay DSdata,#StartC Shout RST delay DSdata,#Rtemp Shout Shin ra.3,sign rb,DSdata :loop ; Clear ports ; Port ra: all outputs. ; Output for eight lsbs of temp reading. ; Clear the ninth-bit clock flag. ; Set the clock to prepare for comms. ; Put write-config protocol into output byte. ; Send it to the 1620. ; Configure as thermometer... ; ...continuous conversion. ; Send that to the thermometer ; Deactivate the thermometer ; Wait for EEPROM programming cycle. ; Start conversions by sending ; the start protocol to the thermometer ; Deactivate the thermometer ; Short *delay between reads. ; Send read-temperature protocol. ; Get the temperature data. ; Write ninth bit to ra.3. ; Write lower eight bits to port rb. ; Repeat forever.

:loop

; * Note that the thermometer 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 thermometer 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 'thermometer ; only as often as appropriate.

PIC Program Listing (cont.)


; ================== thermometer Communication Routines ================ ; >> Note that callable subroutines should normally come _before_ the ; body of the program (or be placed there in program memory using "org" ; directives) to ensure that call destinations fall in the lower 256 ; addresses of a program-memory page. In example programs like this ; we frequently break this rule since it makes the program more readable. ; 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 thermometer. Nine-bit transfers are required for ; storing temperature data to the thermometer'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 thermometer begin with the controller ; sending a protocol, Shout contains the "setb RST" command required to ; activate the thermometer. Since communications can end with either an output ; (Shout) or an input (Shin), Shout does _not_ deactivate the thermometer. Make ; sure to take care of this detail in your code that uses this routine. Shout

:begin :loop

mov mov movb setb rr clrb movb mov snz snb setb djnz ret

!ra,#DQout bits,#9 c,sign RST DSdata CLK DQ,c w,--bits clk9 CLK bits,:loop

; Set to output. ; Set up for 8- or 9-bit transfer. ; Put bit8 (sign bit) into carry. ; Activate the thermometer. ; Rotate bit0 of DSdata into carry. ; Set up for clock pulse ; Move carry bit to input of thermometer ; Pulse the clock line for each of ; first 8 bits. On the ninth bit, ; pulse the clock only if clk9 = 1. ; Finish the pulse if conditions are met. ; Loop 9 times.

PIC Program Listing (cont.)


; Shin shifts in data from the thermometer. If the data to be received is ; a 9-bit temperature, the lower 8 bits are in DSdata and the 9th (sign) ; bit is in the carry bit. If the data is a configuration byte, ignore the ; extra bit in carry. ; >> Note: When a program receives input from the thermometer, it is always the ; end of the communication, so Shin incorporates the command "clrb RST" ; to deactivate the thermometer. Shin

:loop

mov mov clc rr clrb movb setb djnz clrb ret

!ra,#DQin bits,#9 DSdata CLK c,DQ CLK bits,:loop RST

; Set DQ to input. ; Nine-bit transfer. ; Clear carry bit. ; Move carry into bit7, shift bits right. ; Clock in the bit on falling edge. ; Get the bit ; Finish the clock pulse. ; Loop 9 times. ; Deactivate the thermometer

; General-purpose delay routine (200+ ms at 4 MHz). ; Not required by thermometer rutines per se, but used to wait for EEPROM ; programming cycles to finish (50 ms max) and to wait an interval ; between temperature readings. delay djnz djnz ret temp1,delay temp2,delay

BASIC Stamp I Micro. and Stamp Ver. D Program Listing: Thermometer


' Program: ' This program interfaces the Digital Thermometer to the ' BASIC Stamp. Input and output subroutines can be combined to ' set the 'thermometer for thermometer or thermostat operation, read ' or write nonvolatile temperature setpoints and configuration ' data. ' ===================== Define Pins and Variables ================ SYMBOL DQp = pin2 ' Data I/O pin. SYMBOL DQn = 2 ' Data I/O pin _number_. SYMBOL CLKn = 1 ' Clock pin number. SYMBOL RSTn = 0 ' Reset pin number. SYMBOL DSout = b0 ' Use bit-addressable byte for DS1620 output. SYMBOL DSin = w0 '" " " word " " input. SYMBOL clocks = b2 ' Counter for clock pulses. ' ===================== Define thermometer Constants =================== ' >>> Constants for configuring the thermometer SYMBOL Rconfig = $AC ' Protocol for 'Read Configuration.' SYMBOL Wconfig = $0C ' Protocol for 'Write Configuration.' SYMBOL CPU = %10 ' Config bit: serial thermometer mode. SYMBOL NoCPU = %00 ' Config bit: standalone thermostat mode. SYMBOL OneShot = %01 ' Config bit: one conversion per start request. SYMBOL Cont = %00 ' Config bit: continuous conversions. ' >>> Constants for serial thermometer applications. SYMBOL StartC = $EE ' Protocol for 'Start Conversion.' SYMBOL StopC = $22 ' Protocol for 'Stop Conversion.' SYMBOL Rtemp = $AA ' Protocol for 'Read Temperature.' ' >>> Constants for programming thermostat functions. SYMBOL RhiT = $A1 ' Protocol for 'Read High-Temperature Setting.' SYMBOL WhiT = $01 ' Protocol for 'Write High-Temperature Setting.' SYMBOL RloT = $A2 ' Protocol for 'Read Low-Temperature Setting.' SYMBOL WloT = $02 ' Protocol for 'Write Low-Temperature Setting.' ' ===================== Begin Program ============================ ' Start by setting initial conditions of I/O lines. low RSTn ' Deactivate the thermometer for now. high CLKn ' Initially high as shown in DS specs. pause 100 ' Wait a bit for things to settle down.

BASIC Stamp I and Ver. D Program Listing: Thermometer (cont.)


' Configure the thermometer for thermometer operation. The configuration ' register is nonvolatile EEPROM. You only need to configure ' the thermometer once. It will retain those configuration settings ' until you change them--even with power removed. To conserve ' Stamp program memory, you can preconfigure the thermometer, then ' remove the configuration code from your final program. (You'll ' still have to issue a start-conversion command, though.) let DSout=Wconfig gosub Shout let DSout=CPU+Cont gosub Shout low RSTn pause 50 let DSout=StartC gosub Shout low RSTn ' Put write-config command into output byte. ' And send it to the thermometer. ' Thermometer, continuous conversion. ' Send to thermometer. ' Deactivate '1620. ' Wait 50ms for EEPROM programming cycle. ' Now, start the conversions by ' sending the start protocol to thermometer ' Deactivate 'thermometer

' The loop below continuously reads the latest temperature data from ' the thermometer. The 'thermometer 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 'thermometer data is a 9-bit number ' in units of 0.5 deg. C. See the ConverTemp subroutine below. Again: pause 1000 let DSout=Rtemp gosub Shout gosub Shin low RSTn gosub ConverTemp gosub DisplayF gosub DisplayC goto Again

' Wait 1 second for conversion to finish. ' Send the read-temperature opcode. ' Get the data. ' Deactivate the thermometer ' Convert temperature reading to absolute. ' Display in degrees F. ' Display in degrees C.

BASIC Stamp I and Ver. D Program Listing: Thermometer (cont.)


' ===================== thermometer I/O Subroutines ================== ' Subroutine: Shout ' Shift bits out to the thermometer. Sends the 8 bits stored in DSout ' (b0). Note that Shout activates the thermometer, since all trans' actions begin with the Stamp sending a protocol (command). It does ' not deactivate the thermometer, though, since many transactions either ' send additional data, or receive data after the initial protocol. ' Note that Shout destroys the contents of DSout in the process of ' shifting it. If you need to save this value, copy it to another ' register. Shout: high RSTn ' Activate thermometer. output DQn ' Set to output to send data to thermometer. for clocks = 1 to 8 ' Send 8 data bits. low CLKn ' Data is valid on rising edge of clock. let DQp = bit0 ' Set up the data bit. high CLKn ' Raise clock. let DSout=DSout/2 ' Shift next data bit into position. next ' If less than 8 bits sent, loop. return ' Else return. ' Subroutine: Shin ' Shift bits in from the thermometer. Reads 9 bits into the lsbs of DSin ' (w0). Shin is written to get 9 bits because the thermometer's temperature ' readings are 9 bits long. If you use Shin to read the configuration ' register, just ignore the 9th bit. Note that DSin overlaps with DSout. ' If you need to save the value shifted in, copy it to another register ' before the next Shout. Shin: input DQn ' Get ready for input from DQ. for clocks = 1 to 9 ' Receive 9 data bits. let DSin = DSin/2 ' Shift input right. low CLKn ' DQ is valid after falling edge of clock. let bit8 = DQp ' Get the data bit. high CLKn ' Raise the clock. next ' If less than 9 bits received, loop. return ' Else return.

BASIC Stamp I and Ver. D Program Listing: Thermometer (cont.)


' ================= Data Conversion/Display Subroutines =============== ' Subroutine: ConverTemp ' The DS1620 has a range of -55 to +125 degrees C in increments of 1/2 ' degree. It's awkward to work with negative numbers in the Stamp's ' positive-integer math, so I've made up a temperature scale called ' DSabs (thermometer absolute scale) ranging from 0 (-55C) to 360 (+125C). ' Internally, your program can do its math in DSabs, then convert to ' degrees F or C for display. ConverTemp: if bit8 = 0 then skip let w0 = w0 | $FE00 skip: let w0 = w0 + 110 return

' If temp > 0 skip "sign extension" procedure. ' Make bits 9 through 15 all 1s to make a ' 16-bit two's complement number. ' Add 110 to reading and return.

' Subroutine: DisplayF ' Convert the temperature in DSabs to degrees F and display on the ' PC screen using debug. DisplayF: let w1 = w0*9/10 if w1 < 67 then subzF let w1 = w1-67 Debug #w1, " F",cr return subzF: let w1 = 67-w1 Debug "-",#w1," F",cr return

' Convert to degrees F relative to -67. ' Handle negative numbers.

' Calculate degrees below 0. ' Display with minus sign.

BASIC Stamp I and Ver. D Program Listing: Thermometer (cont.)


' Subroutine: DisplayC ' Convert the temperature in DSabs to degrees C and display on the ' PC screen using debug. DisplayC: let w1 = w0/2 if w1 < 55 then subzC let w1 = w1-55 Debug #w1, " C",cr return subzC: let w1 = 55-w1 Debug "-",#w1," C",cr return

' Convert to degrees C relative to -55. ' Handle negative numbers.

' Calculate degrees below 0. ' Display with minus sign.

BASIC Stamp I (BS1-IC) and BASIC Stamp Ver. D Program Listing: Thermostat
' Program: DS_STAT.BS1 ' This program interfaces the 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: ' ' ' ' Function GND Transmit Receive DB25 Pin DB9 Pin Stamp Pin 7 5 GND 2 3 4 3 2 3

' Hardware handshaking must be disabled in the terminal software. ' Communication format is 2400 baud, no parity, 8 data bits, ' 1 stop bit. The Stamp prompts the user for the high and low ' temperature setpoints, then copies these to the registers of the ' thermometer. The setpoints _must_ be preceded by the appropriate sign ' (+ or -). The program does not assume that "78" means "+78." ' Once the thermometer is programmed, the circuit should be turned ' off, and the thermometer removed for installation in its standalone ' thermostat application. ' ===================== Define Pins and Variables ================ SYMBOL ComIn = 4 ' Serial communication input pin. SYMBOL ComOut = 3 ' Serial communication output pin. SYMBOL DQp = pin2 ' Data I/O pin. SYMBOL DQn = 2 ' Data I/O pin _number_. SYMBOL CLKn = 1 ' Clock pin number. SYMBOL RSTn = 0 ' Reset pin number. SYMBOL DSout = w0 ' Use bit-addressable word for DS1620 output. SYMBOL DSin = w0 '" " " word " " input. SYMBOL clocks = b2 ' Counter for clock pulses. SYMBOL config = b3 ' Copy of the DS1620 configuration bits. SYMBOL setTemp = w2 ' Copy of the temperature setting. SYMBOL comData = b6 ' Serial input data. SYMBOL index = b7 ' Temporary counter used in prompts. ' ===================== Define DS1620 Constants =================== ' >>> Constants for configuring the DS1620 SYMBOL Rconfig = $AC ' Protocol for 'Read Configuration.' SYMBOL Wconfig = $0C ' Protocol for 'Write Configuration.' SYMBOL CPU = %10 ' Config bit: serial thermometer mode. SYMBOL NoCPU = %00 ' Config bit: standalone thermostat mode. SYMBOL OneShot = %01 ' Config bit: one conversion per start request. SYMBOL Cont = %00 ' Config bit: continuous conversions after start.

BASIC Stamp I and Ver. D Program Listing: Thermostat (cont.)


' >>> Constants for serial thermometer applications. SYMBOL StartC = $EE ' Protocol for 'Start Conversion.' SYMBOL StopC = $22 ' Protocol for 'Stop Conversion.' SYMBOL Rtemp = $AA ' Protocol for 'Read Temperature.' ' >>> Constants for programming thermostat functions. SYMBOL RhiT = $A1 ' Protocol for 'Read High-Temperature Setting.' SYMBOL WhiT = $01 ' Protocol for 'Write High-Temperature Setting.' SYMBOL RloT = $A2 ' Protocol for 'Read Low-Temperature Setting.' SYMBOL WloT = $02 ' Protocol for 'Write Low-Temperature Setting.' ' ===================== Begin Program ============================ ' Start by setting initial conditions of I/O lines. Start: low RSTn ' Deactivate the thermometer for now. high CLKn ' Initially high as shown in DS specs. ' Next, send prompts and gather data for configuration. promptUser: Serout comOut,N2400,(13,"Hi:") ' Prompt for the high setpoint. For index = 0 to 1 ' Prepare to get both setpoints. let setTemp = 0 ' Clear the high- and low-temp registers. Serin comIn,N2400,comData ' Get the sign of the temperature (+/-). if comData = "+" then skip1 ' Prepare for positive numbers. if comData <> "-" then Err ' If not negative then what? Error.. let setTemp = 256 ' If negative, set the sign bit. skip1: Serin comIn,N2400,#comData ' Now get the numeric part of the entry. let comData = comData * 2 ' Times 2 for 1620's 0.5-degree scale. if setTemp = 0 then skip2 ' Skip next line if positive. let comData=comData^255+1 ' If negative, take two's complement. skip2: let setTemp = setTemp | comData ' Merge entry value into setTemp. let DSout = WloT ' First entry is hi temp, 2nd is lo temp. if index = 1 then skip3: let DSout = WhiT skip3: gosub Shout ' Send protocol.. let DSout = setTemp ' ..and the temperature setting.. gosub Shout ' ..to the thermometer. (Two Shouts are needed to gosub Shout ' send 9-bit temp data to the thermometer.) low RSTn ' Deactivate 'thermometer. pause 100 ' Let EEPROM self-program. let DSout = RloT ' Read back the value written to '1620.

BASIC Stamp I and Ver. D Program Listing: Thermostat (cont.)


if index = 1 then skip4: let DSout = RhiT skip4: gosub Shout gosub Shin low RSTn if DSin = setTemp then skip5 goto Fail skip5: if index <> 0 then skip6 Serout comOut,N2400,(13,"Lo:") next index skip6: let config = NoCPU+OneShot skip7: let DSout = Wconfig gosub Shout let DSout = config gosub Shout low RSTn pause 100 let DSout = Rconfig gosub Shout gosub Shin low RSTn let DSin = DSin & %11 if DSin = config then skip8 goto Fail skip8: Serout comOut,N2400,(" OK") end Err: Serout comOut,N2400,(" ERR") end Fail: Serout comOut,N2400,(13," FAIL") end

' Send read protocol to the thermometer. ' Get the data. ' Deactivate 'thermometer. ' Compare to the value written. ' If they're different, print "fail" msg. ' Now set up to prompt for lo temp.

' Temps done: configure the 'thermometer.

' Read back the configuration and..

' ..compare it to value sent. ' If they're different, print "fail" msg. ' Else, print "OK". ' Stop until power cycles.

' Signal data-entry error.

' Signal thermometer write error.

BASIC Stamp I and Ver. D Program Listing: Thermostat (cont.)


' ===================== thermometer I/O Subroutines ================== ' Subroutine: Shout ' Shift bits out to the thermometer. Sends the 8 bits stored in DSout ' (b0). Note that Shout activates the thermometer, since all trans' actions begin with the Stamp sending a protocol (command). It does ' not deactivate the thermometer, though, since many transactions either ' send additional data, or receive data after the initial protocol. ' Note that Shout destroys the contents of DSout in the process of ' shifting it. If you need to save this value, copy it to another ' register. Shout: high RSTn ' Activate thermometer. output DQn ' Set to output to send data to thermometer. for clocks = 1 to 8 ' Send 8 data bits. low CLKn ' Data is valid on rising edge of clock. let DQp = bit0 ' Set up the data bit. high CLKn ' Raise clock. let DSout=DSout/2 ' Shift next data bit into position. next ' If less than 8 bits sent, loop. return ' Else return. ' Subroutine: Shin ' Shift bits in from the thermometer. Reads 9 bits into the lsbs of DSin ' (w0). Shin is written to get 9 bits because the thermometer's temperature ' readings are 9 bits long. If you use Shin to read the configuration ' register, just ignore the 9th bit. Note that DSin overlaps with DSout. ' If you need to save the value shifted in, copy it to another register ' before the next Shout. Shin: input DQn ' Get ready for input from DQ. for clocks = 1 to 9 ' Receive 9 data bits. let DSin = DSin/2 ' Shift input right. low CLKn ' DQ is valid after falling edge of clock. let bit8 = DQp ' Get the data bit. high CLKn ' Raise the clock. next ' If less than 9 bits received, loop. return ' Else return.

BASIC Stamp II (BS2-IC) Program Listing: Thermometer


' Program: DS1620.BS2 (interface digital thermometer to BS2) ' This program interfaces the Digital Thermometer to ' the BS2. Input and output routines can be combined to set ' the 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 thermometer, 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 ================ DQ con 2 ' Pin 2 <=> DQ. CLK con 1 ' Pin 1 => CLK. RST con 0 ' Pin 0 => RST (high = active). DSdata var word ' Word variable to hold 9-bit data. Sign var DSdata.bit8 ' Sign bit of raw temperature data. T_sign var bit ' Saved sign bit for converted temperature. ' ===================== Define thermometer Constants =================== ' >>> Constants for configuring the thermometer Rconfig con $AC ' Protocol for 'Read Configuration.' Wconfig con $0C ' Protocol for 'Write Configuration.' CPU con %10 ' Config bit: serial thermometer mode. NoCPU con %00 ' Config bit: standalone thermostat 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.' ' >>> Constants for programming thermostat functions. RhiT con $A1 ' Protocol for 'Read High-Temperature Setting.' WhiT con $01 ' Protocol for 'Write High-Temperature Setting.' RloT con $A2 ' Protocol for 'Read Low-Temperature Setting.' WloT con $02 ' Protocol for 'Write Low-Temperature Setting.'

BASIC Stamp II Program Listing: Thermometer (cont.)


' ===================== Begin Program ============================ low RST ' Deactivate 'thermometer for now. high CLK ' Put clock in starting state. pause 100 ' Let things settle down a moment. high RST ' Activate the 'thermometer and set it for continuous.. shiftout DQ,CLK,lsbfirst,[Wconfig,CPU+Cont] ' ..temp conversions. low RST ' Done--deactivate. pause 50 ' Wait for the EEPROM to self-program. high RST ' Now activate it again and send the.. shiftout DQ,CLK,lsbfirst,[StartC] ' Send start-conversion protocol. low RST ' Done--deactivate. ' The loop below continuously reads the latest temperature from ' the DS1620. again: pause 1000 ' Wait a second between readings. high RST ' Activate the 'thermometer. 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.

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 18

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 19

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 20

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 21

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 22

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 23

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 24

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 25

Using the DS1620 Digital Thermometer / Thermostat

sales / technical support (916) 624-8333 fax (916) 624-8003 [email protected] [email protected]

Page 26

You might also like