' -----[ PrecTemp ]----------------------------------------------------------- 'This program uses the Dallas DS1620 temperature chip and returns a 'temperature reading accurate to 1/10 deg Celsius. The routine uses 2 'undocumented codes for the DS1620. (read counter & Load Slope) 'The DS1620 has a little thermal mass and does not respond instantly to 'contact changes in temperature but it is very accurate. ' The chip will actually heat up about 1/2 to 1 deg C in the first few ' seconds of operation due to the heat generated on the chip itself and 'the LP voltage regulator next to the chip. ' BS2 DS1620 ' ----- ------- ' Pin 11 DS1620.3 ' Pin 12 DS1620.2 ' Pin 13 DS1620.1 True CON 1 False CON 0 ' DS1620 pins ' Rst CON 11 ' DS1620.3 Clk CON 12 ' DS1620.2 DQ CON 13 ' DS1620.1 ' DS1620 commands ' RTmp CON $AA ' read temperature WTHi CON $01 ' write TH (high temp register) WTLo CON $02 ' write TL (low temp register) RTHi CON $A1 ' read TH RTLo CON $A2 ' read TL Strt CON $EE ' start conversion StpC CON $22 ' stop conversion WCfg CON $0C ' write configuration register RCfg CON $AC ' read configuration register 'non documented commands RCnt CON $A0 'Read Counter Command LSlo CON $41 'Load Slope Command ' -----[ Variables ]------------------------------------------------------- ' cmdByte VAR Byte ' command to send to DS1620 tmpIn VAR Word ' 9-bit temp input from DS1620 nFlag VAR tmpIn.Bit8 ' negative flag hlfBit VAR tmpIn.Bit0 ' half degree C bit RegIn VAR Byte 'DS1620 config register reading CovDon VAR RegIn.bit7 'Conversion done bit done=1 CouRem VAR Word 'Count remaining register reading COC VAR WORD 'Count per Deg C register tempC VAR Byte ' converted celcius value temp VAR Word ' temperature to display sign VAR Byte ' sign of temperature frac_d VAR byte '1/10 th fraction of temp DirH = %00111000 ' DS1620 pins Outs = $0000 ' all outputs off to start ' Initialize the DS1620 ' - use with CPU in single shot mode HIGH Rst ' alert the DS1620 cmdByte = WCfg ' prepare to write config GOSUB CmdOut ' send the command cmdByte = %00000011 ' with CPU; single shot mode GOSUB CmdOut LOW Rst PAUSE 10 ' pause for EE write cycle ' -----[ Main Code ]------------------------------------------------------- ' Main: HIGH Rst 'Alert the DS1620 cmdByte = Strt 'Start temp conversion GOSUB CmdOut LOW Rst T1: High Rst 'Alert the DS1620 CmdByte = Rcfg 'read the configuration register GOSUB CmdOut 'to see when the conversion is done. Gosub Rconfig LOW RST HIGH Rst ' alert the DS1620 cmdByte = RTmp ' read temperature GOSUB CmdOut GOSUB TempIn ' get the temperature LOW RST HIGH RST 'alert DS1620 CmdByte = RCnt 'Read Count Remaining register GOSUB CmdOut GOSUB ReadCount 'get result HIGH RST 'Alert Ds1620 CmdByte = LSlo 'Load slope command GOSUB CmdOut 'Loads Slope into counter Register LOW RST HIGH RST 'Alert DS1620 CmdByte = RCnt 'read count remain reg for slope GOSUB CmdOut GOSUB CountC 'Get Count per deg C '************************************************************************ ' Temperature = TmpIn - 0.25 + (CoC - CouRem)/CoC ' Temperature = TmpIn - 1/2 deg bit + (Count per deg - Count remaining)/ count per deg '************************************************************************ '************************************************************** ShowC: GOSUB GetC ' convert to celcius temp = tempC GOTO ShowT GOTO Main ' -----[ Subroutines ]----------------------------------------------------- ' send command byte to the DS1620 CmdOut: shiftout DQ ,clk ,0,[cmdbyte] RETURN ' read 9-bit temperature from DS1620 TempIn: tmpIn = 0 ' clear input variable shiftin DQ, clk,1,[tmpin\9] 'get 9 bit result RETURN 'Read Config register for Conversion done bit 7 1=done RConfig: RegIn = 0 'reset register to 0 Shiftin DQ, CLK, 1, [RegIn\8] 'get result LOW RST IF CovDon = False then T1 'if done bit not 1 do it again RETURN 'read count remaining register ReadCount: CouRem = 0 'Reset count remaining register shiftin DQ, clk, 1, [CouRem\9] 'get count remaining 9 bit LOW RST RETURN 'read count per deg c register CountC: CoC = 0 'Reset CoC register Shiftin DQ, CLK, 1, [Coc\9] 'get result 9 bit LOW RST RETURN ' convert reading from 1/2 degrees input (rounds up) ' GetC: IF nFlag = 0 THEN CPos ' check negative bit (8) sign = "-" ' set sign tempC = -tmpIn / 2 ' if neg, take 2's compliment discard hlfbit GOTO CDone CPos: sign = " " tempC = (tmpIn & $FE) / 2 'mask the half bit & convert to DEG C CDone: RETURN showT: frac_d = ((CoC - CouRem) * 10)/ CoC ' deals with BS2's lack of floating point '************************************************** 'data to lcd goes here if you are using a LCD display '************************************************** Debug DEC tempC, ".", Dec frac_d, "Deg C",cr,cr 'display temp to 1/10th deg pause 1200 'wait a little while GOTO Main ' do it again 'Program by Pat Sweeney ' The Circuit Tree ' Tenino Wa