PDA

View Full Version : Returning whole numbers for DS1820?



jessey
- 27th January 2007, 10:31
Hello,

I'm using an example code for a DS1820 that does the math for the display to show either Fahrenheit or Celsius to 2 decimal places. I don't understand the math that's done here and was wondering if anyone could suggest a way to do the conversion without the decimal places.

This works fine and isn't a problem in my programs but I'd like to use Darrel Taylor's "Temp_Convert" include file so I can display negative temperatures and I think the math here is screwing up the conversion in the include file. In his example code "Test_Temp_Convert" the temperatures are declared before the conversion with whole numbers so I thought if I could get whole numbers returned with the code I'm using below then the include file might work. Has anyone used Darrels "Temp_Convert" include file with a DS1820? I'd be grateful for any help.

Thanks
jessey



Update_Pond_Temperature:
OWOut Sensor_Input, 1, [$CC, $44] ' Start Water_Temp conversion
waitloop:
OWIn Sensor_Input, 4, [count_remain] ' Check for still busy converting
IF count_remain = 0 THEN waitloop

OWOut Sensor_Input, 1, [$CC, $BE] ' Read the Water_Temp
OWIn Sensor_Input, 0, [Water_Temp.LOWBYTE, Water_Temp.HIGHBYTE, _
Skip 4, count_remain, count_per_c]

IF Temp_Displayed = Celsius then
' Calculate Water_Temp in degrees C to 2 decimal places
Water_Temp = (((Water_Temp >> 1) * 100) - 25) _
+ (((count_per_c - count_remain) * 100) / count_per_c)
RETURN
ENDIF

IF Temp_Displayed = Fahrenheit then
' Calculate Water_Temp in degrees C to 2 decimal places
Water_Temp = (((Water_Temp >> 1) * 100) - 25) _
+ (((count_per_c - count_remain) * 100) / count_per_c)
' Convert Water_Temp from Celsius degrees to Fahrenheit to 2 decimal places
Water_Temp = (Water_Temp */ 461) + 3200
RETURN
ENDIF


Then to display I use this:


IF Temp_Displayed = Fahrenheit then
LCDOut $fe, $c0,"H2o=",DEC Water_Temp / 100,0,"F "
ENDIF
IF Temp_Displayed = Celsius then
LCDOut $fe, $c0,"H2o=",DEC Water_Temp / 100,0,"C "
ENDIF

J. Mark Wolf
- 27th January 2007, 15:54
If I understand your question correctly, I think all you need to do is to shift right (not rotate right) the intial temperature result.

On the DS18S20, each count of the 9-bit result is one-half degree Celsius. In other words the least significant bit is the half-degree "count". After shifting right once, this bit is "tossed" and the least significant bit will hold the whole-degree "count".

Thereafter, you can process the result as an 8-bit whole number.

Darrel Taylor
- 27th January 2007, 22:36
Hi Jessey,

Try this...
http://www.picbasic.co.uk/forum/showthread.php?p=20470

Should work with both Positive and Negative temperatures.
<br>

jessey
- 29th January 2007, 03:09
Hi Darrel,

Are you suggesting that your code snip will work without using your "Temp_Convert" include file with my DS1820? I've tried it every which way with & without the include file and I can't seem to get it to work. Is there a sample on the list here that shows how its set up using a DS1820?



Print_To_2nd_Line_Of_Lcd:' ,".",DEC2 Water_Temp,
Sign = TempC.15
TempC = ABS(TempC)
TempC =(TempC>>1)*10 + (TempC.0 * 5)
IF Sign then
IF Its = Dark THEN
IF Temp_Displayed = Fahrenheit then
LCDOut $fe, $c0,1,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"F"
ENDIF
IF Temp_Displayed = Celsius then
LCDOut $fe, $c0,1,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"C"
ENDIF
ELSE'if its light then show the print below
IF Temp_Displayed = Fahrenheit then
LCDOut $fe, $c0,2,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"F"
ENDIF
IF Temp_Displayed = Celsius then
LCDOut $fe, $c0,2,"H2o = -",DEC Water_Temp / 10,".",DEC2 Water_Temp,0,"C"
ENDIF
ENDIF
ELSE
IF Its = Dark THEN
IF Temp_Displayed = Fahrenheit then
LCDOut $fe, $c0,1,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
"F ",DEC Lite_Level
ENDIF
IF Temp_Displayed = Celsius then
LCDOut $fe, $c0,1,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
"C ",DEC Lite_Level
ENDIF
ELSE'if its light then show the print below
IF Temp_Displayed = Fahrenheit then
LCDOut $fe, $c0,2,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
"F ",DEC Lite_Level
ENDIF
IF Temp_Displayed = Celsius then
LCDOut $fe, $c0,2,"H2o=",DEC Water_Temp / 100,".",DEC2 Water_Temp,0, _
"C ",DEC Lite_Level
ENDIF
ENDIF
ENDIF
RETURN


This is how I read the temperature:


Update_Pond_Temperature:
OWOut Sensor_Input, 1, [$CC, $44] ' Start Water_Temp conversion
waitloop:
OWIn Sensor_Input, 4, [count_remain] ' Check for still busy converting
IF count_remain = 0 THEN waitloop

OWOut Sensor_Input, 1, [$CC, $BE] ' Read the Water_Temp
OWIn Sensor_Input, 0, [Water_Temp.LOWBYTE, Water_Temp.HIGHBYTE, _
Skip 4, count_remain, count_per_c]
' Calculate Water_Temp in degrees C to 2 decimal places
Water_Temp = (((Water_Temp >> 1) * 100) - 25) _
+ (((count_per_c - count_remain) * 100) / count_per_c)

IF Temp_Displayed = Celsius then RETURN

'IF the Temp_Displayed = Fahrenheit then Convert C to F before returning
Water_Temp = (Water_Temp */ 461) + 3200
RETURN


When the temperature drops below 32 degrees F then it displays 556 instead of 31. I'd surely appreciate any further help you could provide.

Thanks
jessey

Darrel Taylor
- 29th January 2007, 11:41
Oops, I should have looked closer. Didn't see you're using the extended resolution. That routine was for .5 deg resolution.

OK, so when the temp drops below 32°F, it means that the °C is going negative. And negative values can't be multiplied or divided in PBP. So the "extended Resolution" formula gives the wrong answer.

Same problem with the Fahrenheit conversion, And the LCDOUT statements.

Here's what I think should work...(untested)

After reading the DS1820, change the second half of Update_Pond_Temperature: to this...
' Calculate Water_Temp in degrees C to 2 decimal places
Sign = Water_Temp.15
Water_Temp = ((ABS(Water_Temp) >> 1) * 100)
if Sign then Water_Temp = -Water_Temp
Water_Temp = Water_Temp - 25 _
+ (((count_per_c - count_remain) * 100) / count_per_c)

IF Temp_Displayed = Celsius then RETURN

'IF the Temp_Displayed = Fahrenheit then Convert C to F before returning

Sign = Water_Temp.15
Water_Temp = (ABS(Water_Temp) */ 461)
if Sign then Water_Temp = -Water_Temp
Water_Temp = Water_Temp + 3200
RETURN

Then change the lcd routine to this...
Print_To_2nd_Line_Of_Lcd:
LCDOut $fe, $c0
IF Its = Dark THEN
LCDOUT 1
else
LCDOUT 2
endif
LCDOut "H2o = "
if Water_Temp.15 then LCDOut "-"
Water_Temp = ABS(Water_Temp)
LCDOut DEC Water_Temp / 100,".",DEC2 Water_Temp,0
IF Temp_Displayed = Fahrenheit then
LCDOut "F "
else
lcdout "C "
endif
IF Its <> Dark THEN
lcdout DEC Lite_Level
endif
RETURN


You don't need the Temp_Convert include file.

HTH,

jessey
- 12th February 2007, 07:44
Hi Darrel,

Looks like I owe you some more beer, your code works excellent. My Pond Pump Controller circuit works great now. Really appreciated.

Thanks
jessey

Darrel Taylor
- 12th February 2007, 12:15
Woohoo! It's like Beer in the bank.

Now I just need to find a local Branch office so I can make a withdrawl. :)

__________________
DT

P.S. That's gotta be one COLD pond in B.C. in February.
<br>