PDA

View Full Version : Quick DS18B20 question



gti_uk
- 20th June 2009, 21:49
Hi, Ive hooked up the DS18B20 and have got it converting fine, but im confused by something.

In the datasheet for the part it says it defaults to 12bit mode which should be 0.0625°C steps.

In the example grid below its got an example of 0000 0111 1101 0000 = 125c
so 0000 0111 1101 0000 = 2000 2000 * 0.0625 = 125 which tallys with their example. But why on my device is it sending out 101011110 which needs to be /10 to return the correct result of 35C. I would have thought going from the sheet it should have needed to be /0.0625

Could someone explain where im going wrong reading the datasheet, as im clearly beeing stupid.

Thanks

Trev

Darrel Taylor
- 21st June 2009, 01:13
35°C?, Dude, turn on the Air Conditioner!
You can't think straight when the room is 95°F. :)

Your example of 101011110, says it's 21.875°C (71.375°F) sounds much more comfortable to me.

But since I don't know where the device is or how your program is coded, there's really no way to answer the question without joking.

Have you seen this page?
http://www.rentron.com/PicBasic/one-wire3.htm
<br>

gti_uk
- 21st June 2009, 08:21
Hi Darrel, thanks for the reply, ill have a good look at the link. Below is the code ive been working with. It seems to work fine, but thinking of it 30odd c does seem high :)

Trev


OPTION_REG = %11010111 'Set PIC options (see datasheet).
CMCON = 7 'turn analogue outputs to digital
TRISA = %11111111 'all RA ports are inputs
TRISB = %0 'all RB ports are inputs
TRISD = %00000000 'all RB ports are inputs


DEFINE LCD_DREG PORTD ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTD ' Set LCD Register Select port
DEFINE LCD_RSBIT 3 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTD ' Set LCD Enable port
DEFINE LCD_EBIT 2 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us

TEMPSEN VAR PORTD.1
temperature Var Word ' Temperature storage
negTemp var temperature.bit11 ' if 1 then its negative temp

count_remain Var Byte ' Count remaining
count_per_c Var Byte ' Count per degree C



PAUSE 500 'GIVE TIME TO SETTLE

LCDOUT $FE,1
LCDOUT "Hello"
PAUSE 500

LOOP:




OWOut TEMPSEN, 1, [$CC, $44] ' Start temperature conversion

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

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


Lcdout $fe, 1,dec temperature, "_", dec1 temperature,"_",dec2 temperature
Lcdout $fe, $c0,bin temperature

Pause 5000


If negTemp = 0 Then

lcdout $fe, 1, " Temp = ",DEC2 temperature/10,".", dec1 temperature," ",$DF,"C"
Lcdout $fe, $c0,dec temperature,"_", dec temperature/10,"_",DEC2 temperature/10
Else
Lcdout $fe, 1, "Not Done"
Endif

Pause 100


GOTO LOOP





END

Darrel Taylor
- 21st June 2009, 09:55
Looks like you started out with a DS1820 example, since the DS18B20 doesn't have COUNT_REMAIN and COUNT_PER_C registers.

To answer your original question, yes it does normally default to 12-bit.
However, any resolution can be stored in non-volitile memory, in which case it will power-up at that reolution instead.

But with any resolution, the calculation is the same ( * 625, DIV32 10).

Bruce's page I pointed to previously covers that, and more.

Hope it works out for you.
<br>

gti_uk
- 21st June 2009, 17:24
Thanks Darrel for the help, its much appreciated. Im trying to get used to reading the datasheets to understand how to communicate with the devices but as you can see not reading them so well at the moment.
I did try to read the datasheet for the (real time Ic) DS1302 last night but well that just confused me, i think i need the picture book version for ages 1 to 5 :D

ill have a good read of that link you gave me.


Trev