Dallas 18B20 example
Moderators: David Barker, Jerry Messina
Dallas 18B20 example
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
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
- blueroomelectronics
- Posts: 46
- Joined: Mon Apr 23, 2007 3:48 pm
- Location: Toronto
- Contact:
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
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
- blueroomelectronics
- Posts: 46
- Joined: Mon Apr 23, 2007 3:48 pm
- Location: Toronto
- Contact:
- David Barker
- Swordfish Developer
- Posts: 1214
- Joined: Tue Oct 03, 2006 7:01 pm
- Location: Saltburn by the Sea, UK
- Contact:
What about DS1822?
Hi guys,
DS1822 doesn't work with this code.
What should I change to make it work?
PLS, Help!
DS1822 doesn't work with this code.
What should I change to make it work?
PLS, Help!
DS18B20
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
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