Dallas 18B20 example

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

MarkW
Posts: 73
Joined: Fri Oct 27, 2006 8:09 pm

Dallas 18B20 example

Post by MarkW » Thu Feb 28, 2008 10:00 pm

I tried to get the example code to work for the Dallas 18B20 and it seems to hang in the FIND routine.

Can anyone tell if I missed something when I copied the example over to the PIC I am using? I added a few extra debug statements but the only thing I see on the degug monitor is the "Program start" message. The only thing connected to the PIC is the DS18B20 and the debug monitor.
Thanks, Mark

Device = 18F2321
Clock = 32

'Internal osc
Config
OSC = INTIO2,
Debug = OFF,
MCLRE = OFF,
BOR = OFF,
WDT = OFF,
WDTPS = 32

Include "DS18B20.bas"
Include "convert.bas"
Include "usart.bas"

//Carriage return and Line feed
Const CR = 13
Const LF = 10

// working variables...
Dim
TempA As ShortInt,
TempB As Word

OSCCON = %01110000 'Set internal osc to 8 MHz
OSCTUNE.6 = 1 'PLL on

TRISA = %00000000
TRISB = %00000000
TRISC = %00000000

//All comparators OFF
CMCON = %00000111

//All digital
ADCON1 = %00001111

// program start...
SetBaudrate(br9600)
SetPin(PORTC.0)

DelayMS(100)
USART.Write("Program start",CR,LF)

// Find() will search the bus for a single DS1820 device
// and load its ROM ID into the DS1820 public variable RomID - you
// could do this manually. For example, RomID = MyRomID...

If Not Find Then
USART.Write("No device found", 13, 10)
Else
USART.Write("Found one",CR,LF)
While true
USART.Write("Convert",CR,LF)
Convert

USART.Write("Get Temp",CR,LF)
GetTemp(TempA, TempB)

USART.Write(DecToStr(TempA),".",DecToStr(TempB,4), $BA, "C", 13, 10)
DelayMS(1000)
Wend
EndIf

End

User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Post by blueroomelectronics » Fri Feb 29, 2008 4:33 pm

Are you using a DS18S20? The module won't work without changes.
A user over at Electro Tech Online http://www.electro-tech-online.com
wrote this for the DS18S20

Code: Select all

// Unicorn 1-Wire test
Device = 18F4550
Clock = 48
Config
   PLLDIV = 5,
   CPUDIV = OSC1_PLL2,
   USBDIV = 2,
   FOSC = HSPLL_HS,
   VREGEN = OFF

// tell the LCD module where it's connected
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTE.0
#option LCD_RW = PORTE.1
#option LCD_EN = PORTE.2

Include "ow.bas"
Include "convert.bas"
Include "LCD.bas"           

// working variables...
Dim Temperature As Word   
Dim i As Byte
Dim TempString As String
// program start...
ADCON1 = $0F                //All digital
CMCON = $07                 //No comparators
High(PORTA.2)               //power DS18S20
OW.SetPin(PORTA.3)          //One Wire is on A3 - needs pullup
LCD.Cls
If(Reset)Then               //returns true if device found
    WriteByte(owSkipROM)    //Skip ROM
    WriteByte($44)          //Convert
    WaitForHigh()           //Wait for conversion to complete
    Reset()
    WriteByte(owSkipROM)    //Skip Rom
    WriteByte($be)          //Read Scratch Pad
    Temperature=ReadByte()
    Temperature=Temperature+ReadByte()*256
    TempString = DecToStr(Temperature*5)   // get temperature * 10
//this is a bodge to display temperature with one decimal place    
    LCD.WriteAt(1,1,"Temp=",TempString(0),TempString(1),".",TempString(2)," Deg C")
//to just display it normal use this next line
//    LCD.WriteAt(1,1,"Temp = ",dectostr(Temperature/2)," Deg C")
    Reset()
    WriteByte(owReadROM)    //read rom
    For i=0 To 7
        LCD.WriteAt(2,(7-i)*2,HexToStr(ReadByte,2))
    Next
Else
    LCD.WriteAt(1,1,"No Device Found!")
EndIf
End

MarkW
Posts: 73
Joined: Fri Oct 27, 2006 8:09 pm

Post by MarkW » Fri Feb 29, 2008 9:36 pm

The module says it is for the B version. Is this not correct?

User avatar
blueroomelectronics
Posts: 46
Joined: Mon Apr 23, 2007 3:48 pm
Location: Toronto
Contact:

Post by blueroomelectronics » Sat Mar 01, 2008 3:36 am

Yes it's for the B version. Just saying check the number on the 1820, they are different beasts. There is also a -PAR version which requires you to GND pins 1 & 3.
Also are you using a 4.7K pullup?

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Sat Mar 01, 2008 2:53 pm

> seems to hang in the FIND routine

This happens if there is a problem with the physical hardware connection - you need to check out fully (also see above post)

MarkW
Posts: 73
Joined: Fri Oct 27, 2006 8:09 pm

Post by MarkW » Mon Mar 03, 2008 3:36 pm

It was indeed a physical connection problem. My old prototype board has seen many projects and it appears that one of the contacts has finally failed. Turned out to be the DQ lead on the Dallas part. Moved it to a different area and it works as expected. Thanks to all.

skartalov
Posts: 37
Joined: Fri Apr 09, 2010 10:50 am
Location: BULGARIA

What about DS1822?

Post by skartalov » Tue Sep 27, 2011 1:24 pm

Hi guys,
DS1822 doesn't work with this code.
What should I change to make it work?

PLS, Help!

Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

DS18B20

Post by Mast » Mon Dec 12, 2011 4:34 pm

Hi all,
just a little problem with my code. It work fine but the result is 20.50 or 20.25 ou 20.75, is it posible to have 20.10 or 20.20 or 20.30, thanks

Code: Select all

 Device = 18F452 
Clock = 20   
#option GLCD_CS_COUNT = 3
#option GLCD_SCREEN_WIDTH = 192
#Option GLCD_DATA          = PORTD 

// EASY PIC4 a la place de 128x64 origine
#Option GLCD_RS            = PORTB.2 
#Option GLCD_CS1           = PORTB.0 
#Option GLCD_CS2           = PORTB.1 
#Option GLCD_CS3           = PORTC.5
#Option GLCD_RW            = PORTB.3 
#Option GLCD_EN            = PORTB.4
#Option GLCD_RST           = PORTB.5


Include "GLCD_192x94.bas"
Include "DS18B20.bas"
Include "convert.bas"
Include "utils.bas"
Include "Digit2fontgrand.bas"

Const Sensor_A(8) As Byte = ($28, $9D, $41, $A9, $03, $00, $00, $1F)

Sub Initialize()
     SetAllDigital() 
 End Sub 


Sub DisplaySensor(ByRefConst pID() As Byte)

   Dim TempA As ShortInt
   Dim TempB As Word
   RomID = pID
   Convert
   GetTemp(TempA, TempB)
   GLCD.SetFont(digit2fontgrand)
   WriteAt(1,64,DecToStr(TempA))
   WriteAt(95,64,DecToStr(TempB,2))

   Rectangle (88,63,94,58)
   Rectangle (89,62,93,59)
   Rectangle (90,61,92,60)
End Sub

// program start...
SetPin(PORTA.5)
ADCON1 = $07
 
GLCD.Cls

Initialize()

While true

If DS18B20.Find = TRUE Then

   DisplaySensor(Sensor_A)
   DelayMS(1000)
 
Else
     WriteAt(10,64,"EEEE")
    DelayMS(1000)
    Cls 
     
EndIf
Wend 

User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Post by Jason » Tue Dec 13, 2011 3:40 pm

Hi Mast,
Thanks for posting your code. I have wanted to give one of those screens a try for ages and you posted a great starter code for me.

Those Dallas sensors are great. I wrote a program that scrolls through all connected 18b20 sensors. It's cool how each one has a unique serial number.

Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

Post by Mast » Tue Dec 13, 2011 3:47 pm

Hi Jason,
no problem for the code, but i think the forum is a little dead :lol:

User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Post by Jason » Tue Dec 13, 2011 4:05 pm

Yea true. How can anyone not be into microcontrollers?

Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

Post by Mast » Tue Dec 13, 2011 4:12 pm

not here

User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Post by Jason » Wed Dec 14, 2011 3:07 am

Anyway Mast, to answer you question, I think the 18B20+ is a higher resolution temp sensor. maybe that one is what your after.

User avatar
Jason
Posts: 50
Joined: Mon Mar 10, 2008 1:10 pm
Location: Australia

Post by Jason » Wed Dec 14, 2011 3:10 am

Hey Mast,
Where did you get that GLCD_192x94.bas file from?

and what glcd screen are you using with this code?

Thanks in advance

Jason

Mast
Posts: 65
Joined: Wed Aug 29, 2007 6:24 am
Location: France

Post by Mast » Wed Dec 14, 2011 7:03 am

Jason wrote:Anyway Mast, to answer you question, I think the 18B20+ is a higher resolution temp sensor. maybe that one is what your after.
GetTemp(TempA, TempB)
tempb = tempb / 1000

DS18B20.SetResolution(cr12Bit)

Post Reply