PDA

View Full Version : ds18b20 code



leemarrow
- 5th May 2013, 14:04
Hi,
I wanted to know if the code is correct for 10-bit

Get_temp:
OWOUT DQ,1,[$CC, $44] ' Skip ROM search & do temp conversion
pause 188 ' Conversion time for 10 bit reading
OWOUT DQ,1,[$CC, $BE] ' Skip ROM search & read scratchpad memory
OWIN DQ,2,[TempR.Lowbyte,TempR.Highbyte]' Read two bytes / end comms

Convert_Temp:
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
TempC = (TempR & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((TempR.Lowbyte & $0F) * 25) ' Lower 4-bits of result * 25

I have some doubts on the quantization:

for 12 bit 750.0mS 0.0625C 1/16 degree so lower 4-bits of result *625

for 10 bit 187.5mS 0.25C 1/4 degree the lower 4-bits of result *25?

Thank you

leemarrow
- 6th May 2013, 09:08
no suggestions?

Ioannis
- 6th May 2013, 11:47
Thefollowing example sets the 18B20 in 10bit mode, makes the conversions in the sub Convert_Temp and displays the current temperature on an LCD. Bruce Reynolds and others on this forum have contributted to make this function.

You will need to adapt for your LCD probably.



DQ var portd.7
count_remain var byte
R_Temp VAR WORD ' RAW Temperature readings
TempC VAR WORD ' Temp in deg C
Float VAR WORD ' Holds remainder for + temp C display
Cold_Bit VAR R_Temp.Bit11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
Real_Cold CON 1 ' Define Real_Cold = 1
Deg CON 223 ' Data to display Deg ° symbol
Sign VAR BYTE ' +/- sign for temp display
Dummy VAR BYTE ' Dummy for Div32

lcdout $fe,1
OWOut DQ, 1, [$CC,$4E, $00,$00,$3F] ;set DS18B20 at 10bit

main11: OWOut DQ, 1, [$CC, $44] ' Start temperature conversion

waitloop: OWIn DQ, 4, [count_remain] ' Check for still busy converting
IF count_remain = 0 Then waitloop

OWOut DQ, 1, [$CC, $BE] ' Read the temperature
OWIn DQ, 2, [R_temp.LOWBYTE, R_temp.HIGHBYTE]', Skip 4, count_remain, count_per_c]
gosub convert_temp

goto main11

Convert_Temp: ' +32.0 to +257 F
IF Cold_Bit = Real_Cold THEN Yikes ' If Cold_Bit = 1, it's below "0" deg C
Sign = "+"
Dummy = 625 * R_Temp ' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
Dummy = 1125 * R_Temp
TempC = (R_Temp & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((R_Temp.Lowbyte & $0F) * 625)/100 ' Lower 4-bits of result * 625
lcdout $fe,$80, " TempC = ",Sign,DEC TempC,".",DEC2 Float,Deg,"C "
lcdout $fe,$C0, "Raw", IBIN16 R_Temp
RETURN

Yikes: ' Display full range -C to -F conversion
Sign = "-" ' Display - symbol for negative temp
Dummy = 625 * ~R_Temp+1' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
lcdout $fe,$80, " TempC = ",Sign,DEC TempC DIG 4,DEC TempC DIG 3,".",DEC TempC,Deg,"C "
RETURN

END

Hope this helps a little
Ioannis

leemarrow
- 6th May 2013, 13:24
thanks for your answer, so the result lower 4-bits of result is always *625
Good

leemarrow
- 7th May 2013, 08:50
Ioannis,
I thank you for the help you have given me, now my code for DS18B20 works well.
I was not able to search the forums for the original code temp.bas Bruce, I know you indicate in which post is or do you have the original?
I want to try to change the code before the expiry time of PBP3 before I buy the full version
thank you very much

Ioannis
- 7th May 2013, 23:45
I think you mean this:

http://www.rentron.com/PicBasic/one-wire3.htm

Ioannis

iw2fvo
- 29th July 2013, 15:38
Good day ,
I am using three DS18b20 temperature sensors.
I would like to have them setted for 9 bit resolution to save time .
How could i do that ?
Any code available for setting up resolution in multiple sensor enviroment ?
Thanks for helping
Regards,
Ambrogio

mikebar
- 2nd March 2016, 15:31
It was explained wonderfully.
Thanks a LOT.

iw2fvo
- 3rd March 2016, 07:31
I am currently looking for a piece of code for reading upto 10 sensors.
Any help please ?
Ambro

Ioannis
- 3rd March 2016, 08:49
You need this:

http://www.picbasic.co.uk/forum/showthread.php?t=520

Ioannis

Heckler
- 3rd March 2016, 15:14
If you look up under the wiki link at the top of the forum and then go to projects you will find this...
http://www.picbasic.co.uk/forum/content.php?r=374-How-to-read-a-one-wire-DS18B20-temperature-sensor-or-nine-of-them

that might help you