Help with DS18B20 program
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 ===============================
Re: Help with DS18B20 program
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:)
Re: Help with DS18B20 program
Re: Help with DS18B20 program
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
Re: Help with DS18B20 program
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.
Re: Help with DS18B20 program
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
Re: Help with DS18B20 program
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.
Code:
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
Re: Help with DS18B20 program
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
Re: Help with DS18B20 program
Good question. If I were you, I would compare your code to the working code example ( http://www.picbasic.co.uk/forum/show...6483#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.
Re: Help with DS18B20 program
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
Re: Help with DS18B20 program
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.
Re: Help with DS18B20 program
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.