' {$STAMP BS2} ' 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/255). 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/$FF is the fraction. ' In the C-to-F conversion below, we multiply the C value by 1.8 ' with "*/ $01CC" since $CC/$FF (204/255) = 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 Constants =================== ' >>> Constants for configuring the DS 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.' ' ===================== Begin Program ============================ LOW RST ' Deactivate sonda HIGH CLK ' Put clock in starting state. PAUSE 100 ' Let things settle down a moment. HIGH RST ' Activate sonda 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 sonda. again: PAUSE 1000 ' Wait a second between readings. HIGH RST ' Activate 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.