DS18B20 WITH 18F8722 AT 10MHZ

General discussion on mikroBasic.
Post Reply
Author
Message
PortlandJohn
Posts: 251
Joined: 20 Jan 2006 11:17
Location: Portland UK

DS18B20 WITH 18F8722 AT 10MHZ

#1 Post by PortlandJohn » 11 Nov 2007 14:11

Hi
I am having trouble getting a DS18B20 to work.
The program is basically one of the examples provided with MB.
I am sure I have had it working before, but now it gives an output of
+85.0000 C. and does not change.

Can anyone please help.


Best regards

PortlandJohn

Code: Select all


'******************************************************************************
' microcontroller : P18F8722
'
' Project: onewire
'
' Date/Author: 2006-02-07 (MJ)
' Description:
' This project is designed to work with PIC P18F8722
'   with minor adjustments, it should work with any other PIC MCU
'
' This code demonstrates one-wire communication with temperature sensor DS18B20
' connected to RA5 pin.
' After reset, PIC obtaines temperature from sensor and prints it on LCD 4 bit
'******************************************************************************
program onewire

dim Raw_temp, TempC, comma as word
dim i, j1, j2, minus       as byte
dim text                   as string[14]

'* Initialise LCD **************************************************************

 Sub Procedure Init_LCD
 
 Lcd_Config(PORTH,7,6,5,4,PORTH,1,2,3)' set the Port and pin used for LCD
 LCD_Init(PORTH)                     ' configure PORTB 4 line LCD
 LCD_Cmd(LCD_CLEAR)                  ' Clear display
 LCD_Cmd(LCD_RETURN_HOME)            ' Turn cursor home
 Lcd_Cmd(LCD_CURSOR_Off)             ' Turn cursor OFF
 Delay_ms(500)

 LCD_Out(1,1,"BIGPIC4 10MHz ")       ' print to row 1 col 1
 LCD_Out(2,1,"Temp DS18B20      ")   ' print to row 2 col 2
 LCD_Out(4,1,"V1  Port J,0     ")    ' print to row 2 col 2
 Delay_ms(2000)
 LCD_Cmd(LCD_CLEAR)                  ' Clear display

End Sub

'* Read Temperature ************************************************************

Sub Procedure Read_Temp

     ow_reset(PORTJ, 1)                    ' onewire reset signal
     ow_write(PORTJ, 1, $CC)               ' SKIP ROM [CCh]
     ow_write(PORTJ, 1, $44)               ' CONVERT T [44h]

     delay_us(120)

     i = ow_reset(PORTJ, 1)                ' 0 - present, 1 - not present
     ow_write(PORTJ, 1, $CC)               ' SKIP ROM [CCh]
     ow_write(PORTJ, 1, $BE)               ' Write to SCRATCHPAD [BEh]

     j1 = ow_Read(PORTJ, 1)                ' Read LSB
     j2 = ow_Read(PORTJ, 1)                ' Read MSB

     minus = j2
     minus = minus >> 3

     if minus = $1F then                   ' Checking temp.(+ or -)
        j2 = not j2
        j1 = not j1
        j1 = j1 + 1
     end if

     Raw_temp = (j2 << 8) or j1            ' Getting RAW data

     if minus = $1F then
       Lcd_Chr(2, 1, "-")                  ' Write temp. sign (+ or -) on LCD
     else
       Lcd_Chr(2, 1, "+")
     end if

     TempC = (Raw_temp and $0FF0) >> 4     ' WHOLE NUMBER
     comma = (j1 and $0F) * 625            ' DECIMAL

 End Sub
 
'* Display Temperature *********************************************************

 Sub Procedure Disp_Temp

     WordToStrWithZeros(TempC, text)       ' Write temp. whole number on LCD

     Lcd_Chr(2, 3, text[3])
     Lcd_Chr(2, 4, text[4])
     Lcd_Chr(2, 5, ".")

     WordToStrWithZeros(comma, text)       ' Write temp. decimal number on LCD

     Lcd_Chr(2, 6, text[1])
     Lcd_Chr(2, 7, text[2])
     Lcd_Chr(2, 8, text[3])
     Lcd_Chr(2, 9, text[4])
     Lcd_Chr(2, 10, 223)                    ' degree' character
     Lcd_Chr(2, 11, "C")

     Delay_ms(500)
                                     ' endless loop
 End Sub

'**** main Program  ************************************************************

main:

  CMCON = CMCON or $07                 ' turn off comparators
  ADCON1 = $0F                         ' set analog inputs
  MEMCON.EBDIS = 1                     ' disable external memory bus

  TRISJ  = %11111111                   ' designate porte as input

  Init_LCD

    while TRUE
    Read_temp
    Disp_Temp
    wend

 end.
'*********  End of Program ****************************************************

Never underestimate the value of a reply.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#2 Post by yo2lio » 11 Nov 2007 18:33

Hi,
You must wait min 750 ms after convert :

Code: Select all

...
ow_write(PORTJ, 1, $44)               ' CONVERT T [44h] 
delay_ms(750)             ' wait for conversion complete
...
or

Code: Select all

...
ow_write(PORTJ, 1, $44)               ' CONVERT T [44h] 
loop                             ' wait for conversion complete
until ow_Read(PORTJ, 1) = 255
...
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

PortlandJohn
Posts: 251
Joined: 20 Jan 2006 11:17
Location: Portland UK

#3 Post by PortlandJohn » 12 Nov 2007 01:04

Hi Florin
Thanks for your comments.
I tried a delay of 750ms but it still did not work.

I tried the do -loop. It took about 9 seconds to display a result which was +27.9375C
But this value is wrong and does not change.

Best regards

Portland John
Never underestimate the value of a reply.

Zoka
Posts: 19
Joined: 21 Apr 2007 11:39
Location: Bosnia & Herzegovina

Zoka

#4 Post by Zoka » 12 Nov 2007 10:11

Hi PortlandJohn,

what about hardware connection, have you put 4.7K pull-up resistor on data line?

BR

Zoka
Zoka

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#5 Post by milan » 12 Nov 2007 11:46

Hi,

which development system you use? Have you turned the diodes on OW line off?

PortlandJohn
Posts: 251
Joined: 20 Jan 2006 11:17
Location: Portland UK

#6 Post by PortlandJohn » 12 Nov 2007 12:22

Hi Milan and Zoka.

I am using a 10K pull up resistor, my board is set up like a BIGPIC 4 board with 10k network next to the 10way connector. I am not using any other resistors.

What are the diodes on the OW line you mentioned?
how do I turn them off?

I am using MB 5.02 and MB6.

Do you think its a hardware problem I have?

Best regards

John
Never underestimate the value of a reply.

Charlie
Posts: 2744
Joined: 01 Dec 2004 22:29

#7 Post by Charlie » 12 Nov 2007 14:54

Hi PortlandJohn,

I tried your original code on a bigpic4 and it works for me.I had to change PortJ to PortA,because thats the port the temp sensor is connected to.Everything else is the same and it works great.
Regards Charlie M.

PortlandJohn
Posts: 251
Joined: 20 Jan 2006 11:17
Location: Portland UK

#8 Post by PortlandJohn » 12 Nov 2007 15:31

Thanks Charlie.
That narrows the search.
I am now convinced its a hardware problem I have.

Best regards

John
Never underestimate the value of a reply.

User avatar
milan
mikroElektronika team
Posts: 1013
Joined: 04 May 2006 16:36
Contact:

#9 Post by milan » 12 Nov 2007 16:04

Hi,

I thought that you use some of the mikroElektronika's development systems then you should turn off PORT LED diodes. If you have your own HW ignore this.

Can you make the same HW connection as in mikroBasic v6 example project and try the provided hex file?

guyfoc
Posts: 297
Joined: 09 Sep 2005 11:34
Location: belgium

#10 Post by guyfoc » 13 Nov 2007 13:00

normaly 85c is the value stored at pwr on in the module
so if you read that probably conv not done ?( conv depend of the precision used and can be about 1 sec)
do you use the parasistic mode or use 5v on Vdd pin 3?
things we are knowing are always easy

PortlandJohn
Posts: 251
Joined: 20 Jan 2006 11:17
Location: Portland UK

#11 Post by PortlandJohn » 14 Nov 2007 15:20

Thanks every one for all your help.
It turned out I was using a DS18B20P, which is a parasitic device.
I am now using a DS18B20 and it works.

Happy Days. :D
Never underestimate the value of a reply.

Post Reply

Return to “mikroBasic General”