'Written in PicBasic
'variable definitions
'b0 used in bit tests
symbol first=b5 ;Celsius value
symbol second=w3 ;b6 & b7 Fahrenheit value
symbol temp=b9 ;data being passed
symbol cntr=b10 ;bit counter
symbol ctemp=b11 ;temp counter
symbol rtemp=b12 ;temp zero counter
START: call RESET_P ;1-wire reset (and presence)
if temp=0 then NO_GOT_P ;check for no device
temp=$CC : cntr=8 ;SKIP_ROM command (it's 8 bits long)
call W_B ;1-wire send
temp=$44 : cntr=8 ;Convert Temperature command (it's 8 bits long)
call W_B ;1-wire send
pause 500 ;wait for conversion
call RESET_P ;1-wire reset (and presence)
if temp=0 then NO_GOT_P ;check for no device
temp=$CC : cntr=8 ;SKIP_ROM command (it's 8 bits long)
call W_B ;1-wire send
temp=$BE : cntr=8 ;READ_SCRATCHPAD (it's 8 bits long)
call W_B ;1-wire send
cntr=8 ;8 bits
call R_B ;1-wire read
first=temp/2 ;first=value read (divide by 2 for nearest whole degree)
second=temp*9 ;here's where the conversion
second=second/10 ;to degrees F
second=second+32 ;is done
serout 7, n2400,(12) ;form feed
serout 7, n2400,("The Temperature is...",13,10)
serout 7, n2400,(13,10,#first," Celsius",13,10,#second," Fahrenheit")
goto START ;do it all again...
NO_GOT_P:
serout 7, n2400,("No device",13,10)
goto START
Listing 2-This BASIC program (using assembly routines from Listing 1) communicates with a DS1820 1-wire digital thermometer and displays the temperature (in Celsius and Fahrenheit) on a PC or LCD.