PDA

View Full Version : Help with DS18B20 program



presario1425
- 19th November 2007, 20:44
I’m a complete newbie at this. I started programming PICs last week and so far I have gone through the basic “LED Blink” and “LCD – Hello World” programs. As my 3rd project, I wanted to interface a DS18B20 temperature sensor to a PIC18F2620 and read the temperature.

I wanted to keep the programming simple and right now I’m only interested in reading a positive temperature (since my setup is inside my house and at about 70 deg. F) and not worry about the fractional value. Thinking it will be easy, I created the below program (with help from a few source codes I found on the net). But when I run the program, my LCD reads “+15 C”, which is not the correct temp because 70 deg F is slightly above 21 deg C. I also printed the raw DQ data on my LCD and it always reads %0000 0000 1111 0000. Can anyone go through my program and let me know what is going wrong? Thanks in advance.

-- Jack

'========== Begin Program ==================
'1) DS18B20 connected to RB4 and Vcc with a 4.7k pull-up resistor.
'2) Just reading positive temp for now, without reading decimal values
'3) DS18B20's temp bit looks like: % ssss-s???-????-???? (s= sign bit)
'4) Using PIC18F2620 with default parallel LCD connections (LCD is 16x2)

Comm_Pin VAR PortB.4 ' One-wire Data-Pin on PortB.4
Sign VAR BYTE ' +/- sign for temp display
RAWTEMP VAR WORD 'Read raw temperature from DS18B20
SignBit VAR RAWTEMP.Bit11 'read sign bit. 0=positive, 1=negative
TempC VAR WORD
Busy VAR BIT

CMCON = 7 ' RA0-RA3 are digital I/O. Turn off camparators

TRISA = 0 'PORTA is output
TRISB = 0 ' PORTB is output

PAUSE 500 ' Wait 0.5 second to initialize LCD
LCDOUT $FE,1 ' Clear LCD

Start_Convert:
LCDOUT $FE,2 'Home cursor
OWOUT Comm_Pin, 1, [$CC, $44] ' Skip ROM search & do temp conversion

Wait_Up:
OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!

OWOUT Comm_Pin, 1, [$CC, $BE] ' Skip ROM search & read scratchpad memory
OWIN Comm_Pin, 2, [RAWTEMP] ' Read the raw temperature
GOSUB Convert_Temp
GOTO Start_Convert

Convert_Temp:
'Using indoors. So, only positive temp is measured for now

Sign = "+"
TempC=(RAWTEMP & 2032)/16

'Explain purpose of above operation
'Decimal 2032 is = %0111 1111 0000. So, ANDing RAWTEMP with 2032
'isolates the temperature value (multiplied by 16, since it's shifted to the left by 4 bits)

LCDOUT "Temp = ",Sign,dec TempC," C" 'output temperature on LCD
RETURN

END

'================== End Program ===============================

Bruce
- 19th November 2007, 20:56
Do you have A/D disabled for RB4?

presario1425
- 19th November 2007, 21:49
Do you have A/D disabled for RB4?

Have NO idea :D

Would you mind letting me know if I should disable it or not? And if I should disable it, how to do it? [Did I mention I'm a complete newbie at this? :D]

ADCON0.0=0 sounds right? (Page 233 fo 18F2620's datasheet)

mister_e
- 19th November 2007, 21:59
You interface a digital device, your I/O must follow this requirement.

ADCON0 refer to the analog to digital converter, not a bad idea to do so. Look a bit later in the datasheet (ADCON1) and you'll discover to how enable/disable it on specific I/O.

If my memory serves me well, i think you can also disable PORTB analog in the config fuses as well.

Bruce
- 19th November 2007, 22:24
If you plan to use a pin that has A/D on it for digital I/O, then you for sure want to disable
the A/D feature for that pin. Otherwise it reads as 0.

ADCON1 = 15 turns off all A/D. See the data sheet REGISTER 19-2: ADCON1: A/D CONTROL
REGISTER 1 section.

Where you see a D in the A/D Port Configuration Control Bits chart, this indicates Digital for
that pin. Where you see an A, this indicates the pin is configured for Analog.

Another option is to use PBADEN config bit. If PBADEN = 0 then AN0-AN7 are analog by default at POR
(power on reset). AN8 to AN12 are digital. If PBADEN = 1 then PCFG 2 to 0 = 1, which means AN0 to AN12
pins are analog

Look at the chart in your data sheet. If ADCON1 = 0 then all pins with analog capabilities are
Analog. If ADCON1 = 15 what are they?

presario1425
- 19th November 2007, 22:38
If ADCON1 = 15 what are they?

They are ALL digital, right? :)

Yeah - I was just reading about ADCON1 on the datasheet and seems like I should try again with ADCON1=15 at the beginning of my program. That will turn RB4 (which is also AN11) into a digital port (along with all the other A/Ds). My fingers are itching to try this out, but alas I don't have my programmer with me. Can't wait to try it out when I go home. Man, this stuff is addicting! :D

Thanks Bruce!

Bruce
- 19th November 2007, 22:48
They are ALL digital, right?
You're 100% correct. Easy stuff isn't it.?


Man, this stuff is addicting!
It really is..;o}

Just remember, the data sheet is all you really need beyond the PBP manual. Lookup the
value you need to place in a peripheral register to configure it to work the way you want
it to, and BINGO you're off to the races.

It really is that simple.

presario1425
- 20th November 2007, 14:56
Well Bruce, seems like I'm in 1's hell :(

I tried putting ADCON1=15 at the beginning of my program and my raw temperature (the 16-bit data from DS18B20) reads %1111-1111-1111-1111 ... all the time.

Not only that, but I tried reading the unique 64-bit ROM address of the sensor using a modified version of the program I found at [http://www.rentron.com/PicBasic/PBP1-wire.htm]. The address reads FFFFFFFFFFFFh.

I connected a voltmeter at the DQ pin. It toggles between 4.7V and 4.4V while the program is gunning. I checked the pin-connections of the sensor (making sure I match the "bottom view" of the datasheet) and my GND pin is connected to ground, my Vdd to the output of the 5-Volt regulator, and my DQ to RB4 with a 4.7k resistor connected between RB4 and Vdd. Seems ok to me.

Any idea what the hell is going on? Thanks in advance for any suggestions.

Bruce
- 20th November 2007, 15:14
If it's reading all 1's my guess would be it's a bad sensor or you have it wired backwards.
I have wired a few of these backwards, and they refused to work afterwards. Even after
I re-wired it.

With the flat side facing you, pins downward, from left-to-right, are ground, DQ, Vdd.

presario1425
- 20th November 2007, 15:23
With the flat side facing you, pins downward, from left-to-right, are ground, DQ, Vdd.

Just like the way I have it connected.

Really don't remember ever connecting it the wrong way. Then again, I was too excited at the beginning :) So, I'm guessing either I messed it up at some point, or DigiKey sent me a bad sensor. Wish there was a way to check the sensor (like resistance values across pins etc.) and also wish that these sensors were not $5 a pop. You know any place where they sell these for less than $5?

I can live with a bad sensor knowing that my program had the potential to work :) Will try again. Anyway, thanks a lot Bruce for all your help.

KVLV
- 20th November 2007, 15:32
you might find the ds18b20 at http://canada.newark.com/



Just like the way I have it connected.

Really don't remember ever connecting it the wrong way. Then again, I was too excited at the beginning :) So, I'm guessing either I messed it up at some point, or DigiKey sent me a bad sensor. Wish there was a way to check the sensor (like resistance values across pins etc.) and also wish that these sensors were not $5 a pop. You know any place where they sell these for less than $5?

I can live with a bad sensor knowing that my program had the potential to work :) Will try again. Anyway, thanks a lot Bruce for all your help.

KVLV
- 20th November 2007, 15:41
the thing is you have not mention how fast you are running your PIC. I found that problem when I ran my pic18f4550 with 20HMz, all I read back from the ds18b20 are zero/not connected. If I lower the chip to 4HMz then I can read the temperature.

I don't know how to confirm this, the OWOUT and the OWIN command might be too fast for the ds18b20 to catch up with faster pic chip ?

Bruce
- 20th November 2007, 15:57
Check http://www.findchips.com This shows 6 distributors that stock the 18B20.
Where are you located? I'll send you a used one (at least we know it's working)
if you spring for the postage.

But, before I wrote it off as bad, I would check with what KVLV just said. You might
try using a different I/O-pin, different oscillator speed, etc.

Try a timing test with say an LED on a port pin. Toggle it on & off. Does it toggle at the
speed you expect? If not, it could be a timing issue, and that would definitely be a problem
with 1-wire comms.

LCDOUT can sometimes work just fine. Even if you have the wrong oscillator speed declared.
So I would run a series of tests before I wrote the sensor off as bad. Just in case.

presario1425
- 20th November 2007, 17:01
Both of you are absolutely right! I just talked with a colleague of mine who is kind of a PIC guru. The first thing that he said was "what is your oscillator setting?" Well, I didn't pay much attention to it when I was programming, so I just used the "INTRC" switch from the programmer software. And I suspect this could be the culprit. Wish I had an oscilloscope. But I will do the cheapo test with the blinking LED (actually this is another thing my colleague suggested ...... smart men think alike, eh?).

I will also try a 4-MHz crystal that I have sitting at home and use the XT switch when I program the PIC next time. Bruce, thanks a lot for the offer to send me the used sensor. It was very kind of you. But let me run these tests first and I will let you know.

Bruce
- 20th November 2007, 18:33
BINGO. You won't need a PIC guru

You're using the internal oscillator, but you're not writing to OSCCON to configure it for the
default 4MHz PBP will assume

Drop OSCCON = %01100000 in there, and I'll bet it starts working just as expected. Check
out the default value of OSCCON at power-up.

Its default setting is for 1MHz. Problem solved. Even without a PIC guru..;o}

presario1425
- 21st November 2007, 01:58
BINGO. You won't need a PIC guru

You're using the internal oscillator, but you're not writing to OSCCON to configure it for the
default 4MHz PBP will assume

Drop OSCCON = %01100000 in there, and I'll bet it starts working just as expected. Check
out the default value of OSCCON at power-up.

Its default setting is for 1MHz. Problem solved. Even without a PIC guru..;o}

Bruce -- It works like a charm now! :D

Can't believe .... all I needed to add was that one line for the OSCCON setup. Thanks so much!

BTW, one line of my program should be corrected. The original line - OWIN Comm_Pin, 2, [RAWTEMP] should be changed to ...
OWIN Comm_Pin, 2, [RAWTEMP.LowByte, RAWTEMP.HighByte] - since OWIN can read in either a BIT or a BYTE only.

CocaColaKid
- 21st November 2007, 13:30
Just a little contribution to this thread. This code should give you ºC, ºF and Kelvin to 100th of a degree or 100th Kelvin. It also works properly for both positive ºF and negative ºF. This seems to an over looked problem all the time. Especially it can be +ºC/+ºF, -ºC/+ºF or -ºC/-ºF.



;************************ 16F88 Configuration Fuses ****************************

@ __CONFIG _CONFIG1, _CP_OFF & _CCP1_RB3 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _PWRTE_ON & _WDT_ON & _XT_OSC
; Code Protection - _CP_OFF, CP_ON
; CCP1 Pin Selection - _CCP1_RB0, _CCP1_RB3
; In-Circuit Debugger Mode - _DEBUG_OFF, _DEBUG_ON
; Flash Program Memory Write Enable - _WRT_PROTECT_OFF, _WRT_PROTECT_256, _WRT_PROTECT_2048, _WRT_PROTECT_ALL
; Data EE Memory Code Protection - _CPD_OFF, _CPD_ON
; Low-Voltage Programming Enable - _LVP_OFF, _LVP_ON
; Brown-Out Reset - _BODEN_OFF, _BODEN_ON
; RA5/MCLR/Vpp Pin Function - _MCLR_OFF, _MCLR_ON
; Power-Up Timer - _PWRTE_OFF, _PWRTE_ON
; Watchdog Timer - _WDT_OFF, _WDT_ON
; Oscillator Selection - _EXTRC_CLKOUT, _EXTRC_IO, _INTRC_CLKOUT, _INTRC_IO, _EXTCLK, _HS_OSC, _XT_OSC, _LP_OSC,
;
@ __CONFIG _CONFIG2, _IESO_OFF & _FCMEN_ON
; Oscillator Switchover - _IESO_OFF, _IESO_ON
; Fail-Safe Clock Monitor - _FCMEN_OFF, _FCMEN_ON

;************************************************* ******************************

;------------------------- Port Assignments --------------------------------

; PORTB.6 DS18B20 - Data Line

;--------------------------- Setup DEFINE;s ----------------------------------

DEFINE OSC 4
DEFINE ADC_BITS 10 ; Set resolution of conversion
DEFINE ADC_CLOCK 8 ; Set clock source (x/FOSC or FRC)
DEFINE ADC_SAMPLEUS 50 ; Set sampling time (in uS)

;------------------------ Port Initialization -------------------------------

CCP1CON = %00000000 ; Disable CCP Module
SSPCON.5 = 0 ; Disable SSP Module
TXSTA.5 = 0 ; Disable AUSART Tx
RCSTA.7 = 0 ; Disable Serial Port
ANSEL = %01000000 ; Set analog ports to digital mode except RB7/AN6
CMCON = %00000111 ; Turn off comparator
OPTION_REG.6 = 0
ADCON1 = %10000000

TRISA = %100000
TRISB = %11110101

;----------------------------- Debug Settings ----------------------------------

cmd con 254 ; Control byte
clr con 1 ; Clear the display
line1 con 128 ; Move cursor to home position on line 1
line2 con 192 ; Move cursor to home position on line 2
line3 con 148 ; Move cursor to home position on line 3
line4 con 212 ; Move cursor to home position on line 4

; ----------------------------- Port Aliases ---------------------------------

DSDataPin var portb.6

;---------------------- Miscellaneous Variables ------------------------------

i var byte ; Loop counter
x var word ; Variable for calculations
DataBit var bit ; Bit shift variable
dummy var word ; Dummy variable for DIV32 function
DataByte var byte ; Byte to perform CRC calculation on
DQ var byte[9] ; 9 Byte Arrray for DS18B20 data
CRCCalc var byte ; Calculated CRC value
NegBit var rawtemp.Bit11 ; +/- indicator: 1 = < 0ºC
RawTemp var word ; Raw Temperature
SignC var byte ; +/- Sign for celcius temperature
SignF var byte ; +/- Sign for fahrenheit temperature
TempC var word ; Temperature in ºC
TempF var word ; Temperature in ºF
TempK var word ; Temperature in Kelvin
CRCError var byte ; Temperature Sensor CRC Error Flag

;--------------------------- Main Program Loop -------------------------------

main:
gosub ReadDS18B20
lcdout cmd,line1,SignC,dec3 TempC/100,".",dec2 TempC//100,223,"C"
lcdout cmd,line2,SignF,dec3 TempF/100,".",dec2 TempF//100,223,"F"
lcdout cmd,line3," ",dec3 TempK/100,".",dec2 TempK//100," K"
goto main

;---------------------------- Read DS18B20 Sensor ------------------------------

ReadDS18B20:
owout DSDataPin,1,[$CC,$44] ; Tell sensor to start a temperature conversion
owout DSDataPin,1,[$CC,$BE] ; Tell sensor you want to read the temperature
owin DSDataPin,0,[STR DQ\9] ; Receive temperature from the sensor
RawTemp.Byte0 = DQ[0] ; Extract the temperature portion and put it in its one word variable
RawTemp.Byte1 = DQ[1]
gosub GetCRC ; Calculate the CRC for comparison
gosub ConvertCelcius ; Convert temperature to ºC
gosub ConvertToFahrenheit ; Convert temperature to ºF
gosub ConvertToKelvin ; Convert temperature to K
return

;------------------------ Calculate the CRC from Byte 9 ------------------------

GetCRC:
for x = 0 to 7 ; Get CRC for each of the 8 bytes
DataByte = DQ[x] ; Assign array pointer using value of x
gosub CalcCRC ; Get CRC value
next x ; Repeat until all bytes are done
if DQ[8] <> CRCCalc then ; Do the CRC values match?
CRCError = 1 ; Set flag indicating a problem with this sensor
else
CRCError = 0 ; Set flag indicating no problem with this sensor
endif
CRCCalc = 0 ; Reset CRC calculation variable
return

;--------------------- CRC Bit Calcuation Method -------------------------------

CalcCRC:
for i = 0 to 7 ; Do for all 8 bits in DataByte
DataBit = CRCCalc.0 ^ DataByte.0 ; XOR bit0 of DataByte and CRC
DataByte = DataByte >> 1 ; Position DataByte for next bit test
if DataBit = 0 then Shift ; If test bit not set, just shift CRCCalc
CRCCalc = CRCCalc ^ $18 ; If set, account for EXOR feedback

shift:
CRCCalc = CRCCalc >> 1 ; Shift right the CRCCalc byte
CRCCalc.7 = DataBit ; CRC bit 0 to bit bucket
next i ; Data bit rotates into CRC bit 7
return

;--------------------------- Convert to Celcius --------------------------------

ConvertToCelcius:
if negbit = 1 then
SignC = "-"
else
SignC = "+"
endif
if SignC = "-" then
dummy = 0
RawTemp.Byte0 = RawTemp.Byte0 ^ 255
RawTemp.Byte1 = RawTemp.Byte1 ^ 255
dummy = 625 * RawTemp + 1
TempC = DIV32 100
else
dummy = 625 * RawTemp
TempC = DIV32 100
endif
return

;------------------------ Convert to Fahrenhiet --------------------------------

ConvertToFahrenheit:
if SignC = "+" then
SignF = "+"
dummy = TempC * 18
TempF = div32 10
TempF = TempF + 3200
else
TempF = (tempc + 5000) * 900
TempF = DIV32 500
if TempC => 1778 and SignC = "-" then
TempF = TempF - 12200
SignF = "-"
else
TempF = 12200 - TempF
SignF = "+"
endif
endif
return

;-------------------------- Convert to Kelvin ----------------------------------

ConvertToKelvin:
if SignC = "-" then
TempK = 27315 - TempC
else
TempK = tempC + 27315
endif
return

KVLV
- 22nd November 2007, 03:52
Hi,
I just copied the code that Cocacolakid posted, modified a bit, compile the program with version 2.46 of PbP, program run ok. When compile with version 2.50 of PBP, program just stuck/stop at "hello". Both case, I use PM assembler. The chip i am using is pic16f876 @20MHz.

I don't know if v2.50pbp has problem or with the 1-wire commands (onwin and owout).

i attached the file below for checking.

If anyone has time, please confirm/verify/correct me if i did anything wrong.

Thanks,

Archangel
- 22nd November 2007, 05:05
Hi,
I just copied the code that Cocacolakid posted, modified a bit, compile the program with version 2.46 of PbP, program run ok. When compile with version 2.50 of PBP, program just stuck/stop at "hello". Both case, I use PM assembler. The chip i am using is pic16f876 @20MHz.

I don't know if v2.50pbp has problem or with the 1-wire commands (onwin and owout).

i attached the file below for checking.

If anyone has time, please confirm/verify/correct me if i did anything wrong.

Thanks,

Hi KVLV,
when you port from one PIC to Another, be aware the configs are likely different, your copied code compiles in MPASM perfectly with the configs commented out as you have them, included they will not compile as the 16f88 chip has different configs I E config1, config2, the 16f876.inc file has the default configs built in which must be commented out with a ; if you wish not to use them, if you do this then you may include your own config like:
@ __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _PWRTE_ON & _WDT_ON & _HS_OSC

and it will compile in VER 2.50

KVLV
- 22nd November 2007, 05:43
Joe,
maybe I rephrase it.
The program I have attached compile ok with v2.46 and v2.50, no error. I use pic16f876.
when compile using version 2.46 of pbp, the program is running ok when downloaded to the chip using bootloader. it displays/transmit the temperature reading to the lcd/terminal ok.

the problem only when compile using v2.50 of pbp, the program (hex file) downloaded/program to the chip using bootloader, the program cannot run/stop/freeze.

i commented out the configurations because it is not needed when using bootloader (i think it is ok to do so).

Archangel
- 22nd November 2007, 05:52
Joe,

i commented out the configurations because it is not needed when using bootloader (i think it is ok to do so).

Without the config statements your program uses the default config statement located in the .inc file of the same name/number as your chip, in this case 16f876 , and the default is for XT_OSC not HS_OSC, and could account for your program's failure to launch as it is set to run at 20 MHZ, the PIC OSC requires more power to operate at 20 than at 4 and the HS_OSC provides that power.
JS

KVLV
- 22nd November 2007, 06:08
thanks Joe, lets me try again. and post if any result.

CocaColaKid
- 22nd November 2007, 13:11
I can't understand why the program would work when compiled with 2.46 but not with 2.50. I don't believe the config fuses are the problem as I believe they are not used with a bootloader and are simply not implemented at loading time. Unfortunately though I am running 2.45 myself so I won't be of much more help on this issue.

presario1425
- 26th November 2007, 16:41
I've been noticing something very interesting with my DS18B20 setup. Let me first reiterate a few things for clarity -

1) My PBP program looks very similar to what I posted initially (very little change made)
2) I am powering the sensor with an external +5V supply (NOT using the parasitic mode)
3) I have the whole setup on a solderless breadboard and powering the circuit using a 9V battery

What I'm noticing is that after I connect the battery to the circuit, the temperature slowly creeps up. Let's say upon power-up, the temp will read 22.5C. If I wait 10-15 minutes, the temp could read as high as 23 or even 24C! But all the while my whole setup is at the same place, under same conditions. Any idea why this might happen? The power supply heating up the actual sensor itself?

CocaColaKid
- 26th November 2007, 16:58
We had a problem similar to this but with the DS1620. The sensors actually measure the temperature of the circuit board, in your case the breadboard. How close is the voltage regulator to the sensor? Can you move the sensor to the opposite end of the board or remotely mount it? This will give you the most accurate temperature measurement since the thermal pathway is the pins on the part itself and not the plastic piece.

Bruce
- 26th November 2007, 17:05
If you have the 18B20 temp sensor anywhere near a requlator, it will definitely pick up on the temperature change over a period of time.

Have you tried with the sensor located away from the regulator?

presario1425
- 26th November 2007, 18:44
How close is the voltage regulator to the sensor?

I'm using one of those cheap $4.00 (400 contacts) breadboards. So, my sensor is probably sitting only 1.5" away and directly across the regulator. I will remount the sensor at the bottom right corner of the breadboard (since my regulator is on the upper left corner) and see what happens. Will post results as soon as I can. Thanks for your comments/suggestions.

CocaColaKid
- 26th November 2007, 18:50
With it being that close I'm quite certain that is the source of your problems. Any little bit of heat and the sensor will find it.

lebanon
- 6th May 2012, 14:58
hello dears..
May anyone helps me to get the code of the ds18b20 on proton ide that will show me the temperature on a graphical lcd?I used this one but it's not working can anyone find the error in this code??

Device 16F877
XTAL=4
Declare LCD_DTPORT PORTB
Declare LCD_RSPIN PORTC.7
Declare LCD_RWPIN PORTC.6
Declare LCD_ENPIN PORTC.5
Declare LCD_CS1PIN PORTC.4
Declare LCD_CS2PIN PORTC.3
Declare LCD_TYPE GRAPHIC
GLCD_CS_INVERT On
INTERNAL_FONT On
FONT_ADDR=0
Declare ADIN_RES 10
Declare ADIN_TAD 2_FOSC
Declare ADIN_STIME 50
ADCON1 = %10000000
TRISA = $ff
TRISB = $00
TRISC =%11000000
TRISD =%11000000
DelayMS 200
Dim SPTemperature As Word ' scratchpad temperature storage
Dim Reserved0 As Byte ' scratchpad reserved variable
Dim Reserved1 As Byte ' scratchpad reserved variable
Dim Reserved2 As Byte ' scratchpad reserved variable
Dim Reg_TH As Byte ' scratchpad TH register or User Byte 1
Dim Reg_TL As Byte ' scratchpad TL register or User Byte 2
Dim Reg_Config As Byte ' scratchpad configuration register
Dim CRC As Byte ' scratchpad CRC register
Dim Temperature As Float ' temperature result
Symbol DQ = PORTD.1 ' one-wire data pin
While 1=1
OWrite DQ, 1, [$CC, $44] ' skip ROM search And start temperature conversion
While ORead DQ, 4 = 0 ' check for still busy converting
Wend

OWrite DQ, 1, [$CC, $BE] ' skip ROM search And read the temperature
ORead DQ, 0, [SPTemperature.LowByte, SPTemperature.HighByte, Reg_TH,Reg_TL,Reg_Config,Reserved0,Reserved1,Reser ved2,CRC]
Temperature = 0.0625 * SPTemperature ' convert to degrees C
Print At 1,1,@Temperature, "C",
Wend
End

Many thanks:)

ScaleRobotics
- 6th May 2012, 15:18
You might try the Proton forum. http://www.protonbasic.co.uk/forum.php

lebanon
- 6th May 2012, 15:50
Should i find it there?I tried but u didnt find what i need can u copy it if u find it pleaseee..

Best Regards

ScaleRobotics
- 6th May 2012, 15:58
Our manual does not cover any of these commands, because it is written in Proton Basic rather than PICBasic, which is what all people on this forum use as a compiler.

But even if we had the manual for your compiler (which 99% of us do not), it would be hard to help you without knowing what graphic LCD you are using.

I would try asking your question on the forum for your compiler, Proton, as mentioned before.

lebanon
- 8th May 2012, 18:13
Many thanks dear..

Im using the graphical lcd 128*64 (LGM12641BS1R) on the proteus isis simulation.But my problem is the program stop on the loop While ORead DQ, 4 = 0 ' check for still busy converting
Wend

As i wrote the program can we skip rom search and start temperature conversion directly?we should put the serial number and the family code of the sensor??
Best Regards

ScaleRobotics
- 9th May 2012, 14:14
What happens when you remove the while Oread line? The cocaCola Kid doesn't use that, he just does straight writes to it, and then reads it (in post #17) and it seems to work for him.



ReadDS18B20:
owout DSDataPin,1,[$CC,$44] ; Tell sensor to start a temperature conversion
owout DSDataPin,1,[$CC,$BE] ; Tell sensor you want to read the temperature
owin DSDataPin,0,[STR DQ\9] ; Receive temperature from the sensor
RawTemp.Byte0 = DQ[0] ; Extract the temperature portion and put it in its one word variable
RawTemp.Byte1 = DQ[1]
gosub GetCRC ; Calculate the CRC for comparison
gosub ConvertCelcius ; Convert temperature to ºC
gosub ConvertToFahrenheit ; Convert temperature to ºF
gosub ConvertToKelvin ; Convert temperature to K
return

lebanon
- 10th May 2012, 16:04
Dear ScaleRobotics ..
When i removed the oread line the graphical lcd put 655.10 all the time and nothing change when i increase or decrease the ds18b20.
what shall i do to have the temperature variation??!!!!
Many thanks

ScaleRobotics
- 10th May 2012, 17:26
Good question. If I were you, I would compare your code to the working code example ( http://www.picbasic.co.uk/forum/showthread.php?t=7533&p=46483#post46483 ) and see what's different about them, as far as how they read and write to the sensor. Also, I would see what you get when you pull the sensor out of the breadboard, if there is one. If you have a scope or better yet, a logic analyzer like: http://www.saleae.com/logic . I would connect it to the signal pin, and see what's happening. If you have a diagram of how you are connecting the sensor, I would include it here, so others can help you better.

lebanon
- 3rd June 2012, 13:23
heyy.. The problem was something got me laugh..It was just a missed resistance between the pin and and the vcc and now the sensor is working good on the positive temperature just in the negative ones is not correct.But now i have another issue..Im looking to make an interrupt when i push a button for 2 seconds on port a.3.It's possible to do it?Always many thanks

ScaleRobotics
- 3rd June 2012, 16:05
That pin does not operate any interrupt. But you could have an interrupt every x ms, and poll that pin to see if it changed. If you were using PicBasic, you could also use the Button command. But it sounds like you are using Proton, so I am not sure what commands are available for you.

tekart
- 22nd August 2012, 00:50
I want to thank CocaColaKid for posting his beautiful, awesome, well documented code! I was under the gun to get a temperature sensor working and ported it into my code and it works flawlessly. Thanks for saving me hours of head bashing! :)

[QUOTE=CocaColaKid;46483]Just a little contribution to this thread. This code should give you ºC, ºF and Kelvin to 100th of a degree or 100th Kelvin. It also works properly for both positive ºF and negative ºF. This seems to an over looked problem all the time. Especially it can be +ºC/+ºF, -ºC/+ºF or -ºC/-ºF.