DS18B20 and crossing zero

lewisg

Senior Member
I'm finishing up a refrigerator/freezer control, logging, display and statistics project using the DS18B20 as my sensors. The way this chip handles crossing zero is problematic for simpletons like me. To save a new minimum temperature requires code like this:
Code:
if RCTemp<128 AND Rmin<128 then
  if RCTemp < Rmin then 
    Rmin = RCTemp
  end if
elseif RCTemp>127 AND Rmin>127 then
  if RCTemp > Rmin then 
   Rmin = RCTemp
  end if	
elseif RCTemp<128 AND Rmin>127 then	
  'temp>0  min<0	DO NOTHING!	
elseif RCTemp>127 AND Rmin<128 then
  if RCTemp > Rmin then 
   Rmin = RCTemp
  end if	
end if
Fairly nasty stuff...

So I hit on the idea of rescaling the DS18B20 output to get a linear progression of numbers. The rescale code looks like this:
Code:
if RCTemp < 127 then
  RCTemp = RCTemp + 100
else
  RCTemp = 128 - RCTemp + 100
end if
Now saving a new minimum looks like this:
Code:
if RCtemp < Rmin then
  Rmin = RCtemp
end if
WHEW! That is easier. Imagine how much easier the compressor cut in and cut out code would be! (I never even tried...)

The DS18B20 output, rescale and temperature conversions work out like this:
Code:
DS18B20  Rescale   C    F
-------  -------  ---  ---
   10      110    10    50
    9      109     9    48
    8      108     8    46
    7      107     7    45
    6      106     6    43
    5      105     5    41
    4      104     4    39
    3      103     3    37
    2      102     2    36
    1      101     1    34
    0      100     0    32
  129       99    -1    30
  130       98    -2    28
  131       97    -3    27
  132       96    -4    25
  133       95    -5    23
  134       94    -6    21
  135       93    -7    19
  136       92    -8    18
  137       91    -9    16
  138       90   -10    14
  139       89   -11    12
  140       88   -12    10
  141       87   -13     9
  142       86   -14     7
  143       85   -15     5
  144       84   -16     3
  145       83   -17     1
  146       82   -18     0
  147       81   -19    -2
  148       80   -20    -4
  149       79   -21    -6
  150       78   -22    -8
  151       77   -23    -9
  152       76   -24   -11
  153       75   -25   -13
Conversions work like this:
Code:
'convert RESCALED whole number temperature values from READTEMP to Celsius

SYMBOL Temp = b0
SYMBOL Sign = b1

Main:
   
  sertxd ("Enter RESCALED DS18B20 value", cr,lf)
  Serin C.1,N9600_8,Temp

  ptr = 0
  @ptr = Temp
  
  if Temp > 44 AND Temp < 226 then
  
    gosub RescaledDS18B20toC
  
    ptr = 0
    Temp = @ptr
    ptr = 1
    Sign = @ptr
      
    SERTXD (Sign, #Temp,"C", cr, lf) 
      
  else
    SERTXD ("Values must be above 44 and below 226", cr, lf)
  end if
  

GOTO Main


RescaledDS18B20toC:

  if @ptr = 100 then      '0C
    ptr = 1
    @ptr = " "
    ptr = 0 
    @ptr = 0
  else if @ptr < 100 then 'below 0C
    ptr = 1 
    @ptr = "-"
    ptr = 0
    @ptr = 100 - @ptr 
  else                    'above 0C
    ptr = 1 
    @ptr = "+"
    ptr = 0
    @ptr = @ptr - 100
  endif

Return
Code:
'convert RESCALED whole number temperature values from READTEMP to Fahrenheit
'mostly stolen from marks and ehow.com 
'www.ehow.com/how_7414093_convert-negative-celsius-fahrenheit.html

SYMBOL Temp = b0
SYMBOL Sign = b1

Main:
   
  sertxd ("Enter RESCALED DS18B20 value", cr,lf)
  Serin C.1,N9600_8,Temp

  ptr = 0
  @ptr = Temp
  
  if Temp > 44 AND Temp < 226 then
  
    gosub RescaledDS18B20toF
  
    ptr = 0
    Temp = @ptr
    ptr = 1
    Sign = @ptr
      
    SERTXD (Sign, #Temp,"F", cr, lf) 
      
  else
    SERTXD ("Values must be above 44 and below 226", cr, lf)
  end if
  

GOTO Main


RescaledDS18B20toF:

  if @ptr = 82 then       '0F
    ptr = 1
    @ptr = " "
    ptr = 0 
    @ptr = 0
  else if @ptr < 82 then  'below 0F
    ptr = 1 
    @ptr = "-"
    ptr = 0
    @ptr = @ptr * 9 / 5
    @ptr = 148 - @ptr 
  else                    'above 0F
    ptr = 1 
    @ptr = "+"
    ptr = 0
    @ptr = @ptr * 9 / 5
    @ptr = @ptr - 148
  endif

Return
The above two code samples are designed to be copied and pasted into the PICAXE Program Editor and then run in Simulate. These also demonstrate a fairly slick (to me) way of passing variables to a sub procedure.

What do you all think?

Was there a much easier way to do this that I missed?
 
Top