' DS2438_2  ( PICAXE-20X2 )
'
' Need description.
'
' copyright, Peter H Anderson, Baltimore, MD, Sept 16, '09

#picaxe 20x2
#Terminal 9600
#No_Table
#No_Data
#freq m4


    Symbol TC_100 = W1

    ' used by MeasTemp and DisplayTemp
    Symbol H = B4
    Symbol L = B5
    Symbol SignBit = B6

    Symbol Dummy = B7
    Symbol N = B8

    Symbol X = W5 ' B10 and B11
    Symbol Y = W6 ' B12 and B13

    Symbol Whole = B14
    Symbol FRact = B15

    Symbol Vad_100 = W8
    Symbol Vdd_100 = W9

    Symbol HiWordNew = W10
    Symbol LoWordNew = W11

    Symbol HiWord = W12
    Symbol LoWord = W13

    Symbol RH_raw_10 = W14  ' B28 and B29

    Symbol Vsource = B30



    dirsB = %11111111
    adcsetup = 0

    SerTxD ("_____________________________", CR, LF)

Do
    GoSub MeasTemp ' Result in TC_100
    GoSub DisplayTemp

    GoSub MeasRH
    GoSub DispRH_Raw

    Pause 2000
Loop


MeasTemp:
    OWOut C.0, %0001, ($cc, $4e, $00)
    OWOut C.0, %0001, ($cc, $44)

    Pause 1000

    OWOut C.0, %0001, ($cc, $b8, $00)  ' recall to scratch
    OWOut C.0, %0001, ($cc, $be, $00)  ' read scratch

    OWin C.0, %0000, (Dummy, L, H)

   ' now do the calculations
    X = H
    X = X * 256 + L
    signbit = X / 256 / 128
    If SignBit = 1 Then ' its negative
       X = X ^ $ffff + 1 ' take the two's comp
    EndIf

    X = X / 8

    ' multiply by 3.125 - This is 100 * Tc
    Y = X / 8   ' 0.125
    TC_100 = X * 3 + Y

    Return

DisplayTemp:

    Whole = TC_100 / 100
    Fract = TC_100 // 100

    SerTxD ("Tc = ")
    If SignBit = 1 Then
       SerTxD ("-")
    EndIf

    SerTxD (#Whole, ".")
    If Fract < 10 Then
        SerTxD ("0")
    Endif
    SerTxD (#Fract, CR, LF)

    Return

MeasRH:

    Vsource = 0
    GoSub MeasVoltage ' result in Vdd_100
    VSource = 1
    GoSub MeasVoltage ' result in Vad_100
    GoSub CalcRH_raw  ' result in RH_raw_10

MeasVoltage:

    OWOut C.0, %0001, ($cc, $4e, $00)

    If VSource = 0 Then
        OWOut C.0, %0000, ($08)
    Else
        OWOut C.0, %0000, ($00)

    EndIf

    OWOut C.0, %0001, ($cc, $b4) ' perform A/D

    Pause 1100    ' wait for A/D to complete

    OWOut C.0, %0001, ($cc, $b8, $00)  ' recall to scratch
    OWOut C.0, %0001, ($cc, $be, $00)  ' read scratch

    For N = 1 TO 3
        OWin C.0, %0000, (Dummy)
        Pause 100
    Next

    OWin C.0, %0000, (L, H)

    X = H
    X = X * 256 + L

    If VSource = 0 Then
       Vdd_100 = X
    Else
       Vad_100 = X
    EndIf


    Return

DisplayDec:
    SerTxD (#Whole, ".")
    If Fract < 10 Then
       SerTxD ("0", #Fract)
    Else
       SerTxD (#Fract)
    EndIf
    SerTxD (CR, LF)
    Return


CalcRH_Raw:

    HiWord = 1613 ** Vad_100		' perfrom the multiplication - 32 bit quantity
    LoWord = 1613 * Vad_100

    'SerTxD (#HiWord, "  ", #LoWord, 13, 10)  ' used for debugging

    For RH_raw_10 = 0 to 60000		' now, divide by Vdd_100 using repeated subtraction
       LoWordNew = LoWord - Vdd_100
       If LoWordNew > LoWord then
       ' underflow
          LoWord = LoWordNew
          HiWordNew = HiWord - 1
          If HiWordNew < HiWord  then ' no underflow
              HiWord = HiWordNew
          Else
              Goto CalcRH_Raw_Done
          EndIf
       Else
          LoWord = LoWordNew
       Endif
    Next

CalcRH_Raw_Done:
    RH_raw_10 = RH_raw_10 - 258

    Return

DispRH_Raw:

    Whole = RH_raw_10 / 10
    Fract = RH_raw_10 // 10
    SerTxD (#Whole, ".", #Fract, CR, LF)
    Return