Interface with DS18B20 1-W Temperature Sensor - PICAXE-18X

copyright, Peter H Anderson, Baltimore, MD, Jan, '04


' DS18B20_2.Bas - PICAXE-18X
'
' Illustrates an interface with the DS18B20.
'
' Continually measures the temperature and displays to two significant digits on the
' terminal.
'
' Note that a 4.7K pullup to +5 VDC is required on the DQ lead.  +5 VDC is required on the
' V+ terminal of the DS18B20.
'
'  18X											DS18B20
'
' INPUT6 (term 15) ------------------ DQ (term 2)
'
' copyright, Peter H Anderson, Baltimore, MD, Jan, '04


    Symbol TReading = W0
    Symbol Whole = B2
    Symbol Fract = B3
    Symbol SignBit = B4
    Symbol Dig = B5
    Symbol TempC_100 = W4


Top:
    ReadTemp12 6,TReading

    SignBit = TReading / 256 / 128
    If SignBit = 0 Then Positive
    ' its negative
    TReading = TReading ^ $ffff + 1

Positive:

    TempC_100 =  TReading * 6		' TC = value * 0.0625
    TReading = TReading * 25 / 100
    TempC_100 = TempC_100 + TReading

    GoSub DisplayTemp

    Wait 1
    GoTo Top


DisplayTemp:

   Whole = TempC_100 / 100
   Fract = TempC_100 % 100
   If SignBit = 0 Then DisplayTemp_1
   SerTxD ("-")

DisplayTemp_1:

   SerTxD (#Whole, ".")
  ' be sure the fractional is two digits
   Dig = Fract / 10
   SerTxD (#Dig)
   Dig = Fract % 10
   SerTxD (#Dig, 13, 10)

   Return