Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

DS18B20 instead DS1820 - Help with code

Status
Not open for further replies.

fratello

New Member
I made the great project of "Digital thermostat (080090-11)" from Elektor july_august 2008 ; unfortunate I do not have DS1820 but DS18B20. The temperature show on display is like "xxx.xx 'C" !.
I read this : "In order to convert the HEX code to a temperature value, first you need to identify if you are using a DS18S20, or DS18B20 series sensor. The code to read the temperature needs to be slightly different for the DS18B20 (and DS1822), because it returns a 12-bit temperature value (0.0625 deg precision), while the DS18S20 and DS1820 return 9-bit values (0.5 deg precision)".
Can somebody help me to modify the source for using DS18B20 ?
Thanks in advance !
 

Attachments

  • 1820THER.ASM
    7.8 KB · Views: 729
That code is extremely hard to follow. Do you have access to the original Basic code and can you compile it?

Mike.
 
Thanks for reply. I have the code. I attach him, just change the extension.
 

Attachments

  • 1820THER_BAS.txt
    8.1 KB · Views: 726
My initial view is you should change
Code:
Temperature = (((Temperature >> 1) * 100) - 25) + (((Count_Per_C - Count_Remain) * 100) / Count_Per_C)

To,

Code:
Temperature = (((Temperature >> 4) * 100) - 25)

But, I could be completely wrong as it's late here.:eek:

If that doesn't work then I'll have another look tomorrow.

Mike.
 
My take on it is a bit different. I just don't know if your BASIC will handle it correctly. Could you try these?

Mike

Code:
Temperature = (Temperature * 100) / 16     ' DS18B20
Code:
Temperature = (Temperature * 100) >> 4     ' DS18B20
 
Last edited:
@pommie: the temperature increase/decrease with fix 1 'C, the decimal values don't change ( 23.75 -> 24.75 -> 25.75)
@Mike : the temperature increase/decrease correct, but....the thermometer wich I have, with LM35 show 23.9 'C and this with PIC show 25,14 'C. Witch of them show the real temperature ? I think though that with Lm35...
Thanks for Yours support ! Any ideas ?
 
@pommie: the temperature increase/decrease with fix 1 'C, the decimal values don't change ( 23.75 -> 24.75 -> 25.75)
@Mike : the temperature increase/decrease correct, but....the thermometer wich I have, with LM35 show 23.9 'C and this with PIC show 25,14 'C. Witch of them show the real temperature ? I think though that with Lm35...
Thanks for Yours support ! Any ideas ?

hi,
I'll jump in while Mike sleeps.:)

If you have a voltmeter, measure the voltage on the output of the LM35, its 10mV/Cdeg. The datasheet will explain.
From that measurement you should be able to determine which tempr is correct.
 
....the thermometer wich I have, with LM35 show 23.9 'C and this with PIC show 25,14 'C. Witch of them show the real temperature ? I think though that with Lm35...
Thanks for Yours support ! Any ideas ?

Not sure which one may be more accurate.

You might look at the raw 16 bit temperature word and compare it to the output to see if the BASIC math routines are accurate. Here are a few sample raw temperature values (TempH:L);

Code:
;                  TempH:L Neg  Output    Display
;     ============================================
;     125.0000°C   h'07D0'  0  02570000  257.0000°F 
;     100.0000°C   h'0640'  0  02120000  212.0000°F
;      77.0000°C   h'04D0'  0  01706000  170.6000°F
;      25.0000°C   h'0190'  0  00770000   77.0000°F
;      22.0000°C   h'0160'  0  00716000   71.6000°F
;       0.5000°C   h'0008'  0  00329000   32.9000°F
;       0.0625°C   h'0001'  0  00321125   32.1125°F
;       0.0000°C   h'0000'  0  00320000   32.0000°F
;     - 0.0625°C   h'FFFF'  0  00318875   31.8875°F
;     - 0.1250°C   h'FFFE'  0  00317750   31.7750°F
;     - 5.0000°C   h'FFB0'  0  00230000   23.0000°F
;     -10.0000°C   h'FF60'  0  00140000   14.0000°F
;     -17.0625°C   h'FEEF'  0  00012875    1.2875°F
;     -17.6875°C   h'FEE5'  0  00001625    0.1625°F
;     -17.7500°C   h'FEE4'  0  00000500    0.0500°F
;     -17.8125°C   h'FEE3'  1  00000625  - 0.0625°F
;     -25.0000°C   h'FE70'  1  00130000  -13.0000°F
;     -55.0000°C   h'FC90'  1  00670000  -67.0000°F
 
From an roumanian forum I receive these modifications....but the temperature still is different on the three termo witch I have...???
 

Attachments

  • 12biti.BAS.txt
    8.7 KB · Views: 514
From an roumanian forum I receive these modifications....but the temperature still is different on the three termo witch I have...???

Did you measure the LM35 output voltage.?
 

Attachments

  • esp03 Dec. 29.gif
    esp03 Dec. 29.gif
    5.4 KB · Views: 473
I measure the LM35 output voltage with an Multimeter - Fluke DT830B, on scale of 2000 mV. The result is almost similar with the thermometer made with DS18B20+12F675+Nokia display (code attached).
Just this last thermo, with PIC 16F628A, show different values.... I think I go crazzy...
 

Attachments

  • Ds18B20TempLcd.bas.txt
    13.3 KB · Views: 364
Thank you for replays !!!
I wish to all a happy NEW YEAR 2009 !

I think I go crazzy with this little thermostat probleme ; I write ( based on Yours great codes) four routines ; everyone display a different temperature. Which of them is -in yours opinion - the best ?

1.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [Temperature.Lowbyte, Temperature.Highbyte]
Convert_Temp:
Dummy = 625 * Temperature
TempC = DIV32 10
TempC = (Temperature & $7FF) >> 4
Float = ((Temperature.Lowbyte & $0F ) * 25 )>>2
Temperature = TempC*100 + Float

2.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [RAWTEMP.Lowbyte, RAWTEMP.Highbyte]
Convert_Temp:
dummy = RAWTEMP * 625
TempC = DIV32 100
Temperature = TempC

3.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [Temperature.Lowbyte, Temperature.Highbyte]
Convert_Temp:
TempC = (Temperature & $7FF) >> 4
Float = ((Temperature.Lowbyte & $0F ) * 25 )>>2
Temperature = TempC*100 + Float

4.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 0, [Temperature.LowByte, Temperature.HighByte, Skip 4, Count_Remain, Count_Per_C]
Convert_Temp :
Temperature = (((Temperature >> 1) * 100) - 25) + (((Count_Per_C - Count_Remain) * 100) / Count_Per_C)
if Temperature > 32767 then '32767 check if BIT12=1
Temperature= ~Temperature
temperature=temperature>>3 ; divide by 8

Thanks in advance for your support ; I hope I don't became boring !!!!
 
There's a simple solution on Philip Anderson's Arduino/DS18B20 web page for two decimal places;

Code:
     if (temperature & 0x8000)                       // if negative reading
       temperature = (temperature ^ 0xffff) + 1;     // 2's comp
     Tc_100 = (6 * temperature) + temperature / 4;   // multiply by (100 * 0.0625) or 6.25
     Whole = Tc_100 / 100;                           // whole portion
     Fract = Tc_100 % 100;                           // fractional portion
 
Last edited:
This is correct ?

Convert_Temp:

TempC = (Temperature.Highbyte << 8)
If (TempC & $8000) then
TempC = (TempC ^ $FFFF)+1
Twist = 1
Endif

TempC = (TempC * 6) + TempC/4
Float = TempC/100
Temperature = TempC/100 + Float
 
Based on the information that I found on the web I write four routines for calc temperature :

1.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [Temperature.Lowbyte, Temperature.Highbyte]
Convert_Temp:
Dummy = 625 * Temperature
TempC = DIV32 10
TempC = (Temperature & $7FF) >> 4
Float = ((Temperature.Lowbyte & $0F ) * 25 )>>2
Temperature = TempC*100 + Float

2.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [RAWTEMP.Lowbyte, RAWTEMP.Highbyte]
Convert_Temp:
dummy = RAWTEMP * 625
TempC = DIV32 100
Temperature = TempC

3.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 2, [Temperature.Lowbyte, Temperature.Highbyte]
Convert_Temp:
TempC = (Temperature & $7FF) >> 4
Float = ((Temperature.Lowbyte & $0F ) * 25 )>>2
Temperature = TempC*100 + Float

4.OWOut DQ, 1, [$CC, $BE]
OWIn DQ, 0, [Temperature.LowByte, Temperature.HighByte, Skip 4, Count_Remain, Count_Per_C]
Convert_Temp :
Temperature = (((Temperature >> 1) * 100) - 25) + (((Count_Per_C - Count_Remain) * 100) / Count_Per_C)
if Temperature > 32767 then '32767 check if BIT12=1
Temperature= ~Temperature
temperature=temperature>>3 ; divide by 8


Everyone make the same thing. I tested all and works fine.
All the best !
 
Based on this book : "PIC Microcontrollers: Know It All" from Lucio Di Jasio, Tim Wilmshurst, Dogan Ibrahim, John Morton, Martin Bates, Jack Smith, D.W. Smith, and Chuck Hellebuyck - witch is my inspiration, I write the code for using this schematic with two sensors DS18B20, so now I have one thermometer witch show two temperatures (In/Out) and have the function of thermostat too.
Hope will useful. If somebody can improved this, please do it !
I wish You all the best !
PS Sorry for my poor english...and thanks again for support !
 

Attachments

  • dual_thermometer_&_thermostat.txt
    12 KB · Views: 350
The code was from the July/August 2008 Elektor, is it also in the book. I'm not a fan D.W. Smith code examples but I'll keep my eyes open for the book.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top