Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

DS18B20 to display on LCD

Status
Not open for further replies.
Here you a sample of using more then 1 temp sensors
Code:
Device = 18F4520
Clock = 8
Config OSC = INTIO67
// import modules...
 #option LCD_DATA = PORTD.4
 #option LCD_RS = PORTD.2
 #option LCD_EN = PORTD.3

// import modules...
Include "intio8"        // you need to use this module to set the osccon you may 
                        // Have one use its name mine uses that name 
Include "DS18B20.bas"
Include "convert.bas"
Include "LCD.bas"


// on display temperature event...
Event OnDisplayTemp()
   Dim TempA As ShortInt
   Dim TempB As Word
   Dim Index As Byte
   
   RomID = SearchRomID
   GetTemp(TempA, TempB)
   LCD.Write(DecToStr(TempA),".",DecToStr(TempB,4),"C")
   
   // display ROM ID...
   LCD.Write(" ($")
   Index = 6
   Repeat
      LCD.Write(HexToStr(SearchRomID(Index),2))
      Dec(Index)
   Until Index = 0
   LCD.Write(")")
End Event
   
// program start...
SetPin(PORTC.0)

// if we have some DS1820 devices connected to the bus,
// then display temperature for each one...
If Count > 0 Then
   While true
      ConvertAll               
      FindAll(OnDisplayTemp)
      DelayMS(1000)
   Wend
EndIf
 
Last edited:
I have used internalosc on other code so I know I have it somewhere?
I don't recall every seeing the FINDALL statement but will try your code out.
I have all three RomID numbers and tried this https://www.sfcompiler.co.uk/wiki/pmwiki.php?n=SwordfishUser.DS18B20Article written by David Barker but it failed to work on the LCD? or even the uart.
That's another issue, the USART. Going to purchase one possibly two PICKIT2 from SURE electronics on Ebay Using Jons example with one Pickit connected to the ICSP while the other connected to the USART ports.Another issue is the Red LED comes on when using the USART.
They are clones but should work. Too many issues with the Junebug. It works better without the 1320 pic installed. Jessema over at DDIY mentioned about dip switch #6. never recall switch 6, yes one switch 1-3.
Here is the situation, I have 3 sensors that I have the RomIDs for, also have 3 port pins I can connect each sensdor to. Want to read the temps, add them all up divide by 3 for average.
Going to try your suggestions
THANKS
 
Tried code in post 21

All I get are squares. Will look over and compare with the code that I have here that displays temps but if I could dimension the temps then maybe I could add them and divide.
This code displays two sensor temps BUT if I hold one sensor the one changes but about 2 seconds later the second one changes. I think on the first page of this thread, Mrdebsnightmare stated that the while when dosn't allow the if then??? to execute.
Would have to go back and review the suggestion.
Code:
Device = 18F4520
Clock = 8
Config OSC = INTIO67
// import modules...
 #option LCD_DATA = PORTD.4
    #option LCD_RS = PORTD.2
    #option LCD_EN = PORTD.3
    #option LCD_INIT_DELAY =100
 
 
Include "DS18B20.bas"
Include "convert.bas"
Include "lcd.bas"
 
// working variables...
//Dim TempA As ShortInt
Dim
 TempA As ShortInt,
// TempZ as shortint
 TempB As Word
Dim sensorA As PORTB.3
Dim sensorB As PORTB.2
 
 
OSCCON = %01111111 
// Start Of Program...
 
SetPin(PORTB.2)
 
// Find()
// 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...
DelayMS(200)
LCD.Cls 
If DS18B20.Find Then
 While true
 Convert
 GetTemp(TempA, TempB)
 LCD.WriteAt(1,4,DecToStr(TempA))
 DelayMS(500)
 GetTemp(TempA,TempB)
 LCD.WriteAt(2,4,DecToStr(TempA))
 Wend 
Else
 LCD.Write("No device found")
EndIf
 
SetPin(PORTB.3)
// Find()
// 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...
DelayMS(200)
LCD.Cls 
If DS18B20.Find Then
 While true
 Convert
 
 DelayMS(500)
 GetTemp(TempA,TempB)
 LCD.WriteAt(2,4,DecToStr(TempA))
 Wend 
Else
 LCD.Write("No device found")
EndIf
 
Look at post 5 by Playnice not Mrdebsnightmare.
I have an idea who Mrdebsnightmare is.
 
If your getting squares your lcd is not hooked up as code mrdeb or the osc is not running at 8mhz
 
The LCD works fine using the code in post 23 without any contrast adjustments.
BUT it still could be a clock error. Will run the code in post 23 but with the clock settings etc from your suggestion.
THANKS
 
Going from GOOD to worse??

I added a 20mhz osccilator GREAT. The board was slowing down after I added another sensor?
Found that the setpin dosn't seem to change anything. Can set for B2 or B3 or B4 but it always reads B3
Once a sensor is read I can change the sensor and the display goes from a temp reading to -1
Plug a different sensor into same port and -1. Must be reading the ROMID.
Now onto something really weird. This code I have an LED connected to C4 but it won't blink.
I confirmed using just a blink code and LED blinks as it should.
I looked at the code flow and pretty sure it should blink the LED. Going to get the LED blinking first then work on the sensor issue?
Code:
Device = 18F4520
Clock = 20

// import modules...
 #option LCD_DATA = PORTD.4
    #option LCD_RS = PORTD.2
    #option LCD_EN = PORTD.3
    #option LCD_INIT_DELAY =100
 
 
Include "DS18B20.bas"
Include "convert.bas"
Include "lcd.bas"
 
      
        // working variables...
        //Dim TempA As ShortInt
        Dim
         TempA As ShortInt,
        // TempZ as shortint
         TempB As Word
       
        Dim LED_g As PORTC.4
        Dim LED_R As PORTC.5 
        LED_g=0
        LED_R=0  
      
        // Start Of Program...
         
        SetPin(PORTB.4)
       
        // Find()
        // 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...
        
        LCD.Cls
        DelayMS(200) 
        LED_g=1
        If DS18B20.Find Then
         While true
         Convert
         GetTemp(TempA, TempB)
         LCD.WriteAt(1,1,DecToStr(TempA))
         Toggle (LED_g)
         DelayMS(1000)
         Toggle (LED_g)
         Wend 
        Else
         LCD.Write("No device found")
        EndIf
 
Mrdeb I hate to tell you this but one wire means one wire the module doesn't handle more then one line your using two. The code i posted can read 6 of the 18b20 your code only read one.
 
Going to re try your code but add the LED.
Still wonder why the LED won't blink.
I think the reason your code failed before was a timming issue as I stated before. Now that I have an external oscillator will give it a go.
 
It appears to work!!
I just need to s l o w it down and decipher what it is displaying.
It looks like the temp then the ROMID
I am wondering if the link I posted to David Barers code where it labels each sensor as A, B, C then just add them up etc.
 
Your code works BUT

I still need to extract the temperatures then add them together and divide but I can't do that until I DIM each sensor as in this code but this code dosn't seem to cooperate.
I was wondering if by chance (I rechecked the ROMIDs) the IDs can be numbered say A, B, C but only using part of the ID numbers, instead of all that hex?
Your code gets and displays the ROMIDs but they are not easy to read as the entire screen is numbers. Thinking inserting a LCD.CLS in between the readings??
After connecting the external osc everything runs better.
Code:
}
// device and clock speed...
Device = 18F4520
Clock = 20
//#option OW_PIN = PORTB.2
 #option LCD_DATA = PORTD.4
    #option LCD_RS = PORTD.2
    #option LCD_EN = PORTD.3
    #option LCD_INIT_DELAY =100
  
// import modules... 
Include "DS18B20.bas"
Include "convert.bas"
Include "lcd.bas"
Include "STRING.BAS"

// FAMILY $28 ($1D) ($0000002CDBAE) <- DS18B20
// FAMILY $28 ($3F) ($0000002CD16E) <- DS18B20
Const Sensor_A(8) As Byte = ($28, $E3, $00, $00, $04, $09, $47, $1D)
Const Sensor_B(8) As Byte = ($28, $C8, $00, $00, $04, $09, $1D, $3F)
Const Sensor_c(8) As Byte = ($28, $B0, $00, $00, $04, $09, $66, $3F)

// display a sensor value...
Sub DisplaySensor(ByRefConst pID() As Byte)
   Dim TempA As ShortInt
   Dim TempB As Word
   RomID = pID
   Convert
   GetTemp(TempA, TempB)
   LCD.Cls
   DelayMS(300)
   LCD.Write(DecToStr(TempB,2), $BA)
End Sub
 
// program start...
 setpin(portb.3)

While true
   DisplaySensor(Sensor_A)
   DisplaySensor(Sensor_B)
   DelayMS(1000)
Wend
 
MrDeb the code I posted may need a delay to slow it down in the main loop I don't have and 18B20 to try it. but thats easy to fix. Post the code you have from Swordfish site or a link.

The code I posted was from the help file it was for Uart I changed it for the LCD
I saves the temp's from each 18B it finds and displays it and the ID it would be easy to do some adding and get a mean value
 
There a lot of ifs with Swordfish you don't need this
#option LCD_INIT_DELAY =100
there a default value its guess what? you give up 100 you don't need to set it only if you want it longer LOL

If you use INOSC you need to use a module with the osccon setting in it and if the code using Jerry's SetDigitalIO or the old one you need to have
SetAllDigital
In your code or you'll get a bunch of boxes on the LCD

Try this one on your LCD
Code:
Device = 18f4520
Clock = 8
Config osc = intio67

// import LCD library...
// LCD...

#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3

// import LCD library...
Include "intio8"
Include "SetDigitalIO"
Include "LCD.bas" 
 SetAllDigital
//
//Include "utils.bas"
Cls
While true
WriteAt(1,1,"Mrdeb")
WriteAt(2,1,"was here    ")
DelayMS (1000)
WriteAt (1,1,"And had fun  ")
WriteAt(2,1,"with Swordfish ")
DelayMS (1000)
Wend

The Module intio8 module is just a file saved in your user modules named intio8 and has this in it
Code:
Module intio8
OSCCON =$72

Now there a easy way to do this I save a copy in my folder with My code so I can make changes as needed seems to be easier that way on Windows 7
 
Last edited:
See if this don't give you time to see what's happening
Code:
Device = 18F4520
Clock = 8  // comment this out and use next one
//Clock = 20 // if you use a crystal uncomment this one
Config OSC = INTIO67 // and comment this one out
// import modules...
 #option LCD_DATA = PORTD.4
 #option LCD_RS = PORTD.2
 #option LCD_EN = PORTD.3
 
// import modules... // comment this one out to if crystal is used
Include "intio8"        // you need to use this module to set the osccon you may 
                        // Have one use its name mine uses that name 
Include "DS18B20.bas"
Include "convert.bas"
Include "LCD.bas"
 
 
// on display temperature event...
Event OnDisplayTemp()
   Dim TempA As ShortInt
   Dim TempB As Word
   Dim Index As Byte
 
   RomID = SearchRomID
   GetTemp(TempA, TempB)
   LCD.Write(DecToStr(TempA),".",DecToStr(TempB,4),"C")
 
   // display ROM ID...
   LCD.Write(" ($")
   Index = 6
   Repeat
      LCD.Write(HexToStr(SearchRomID(Index),2))
      Dec(Index)
   Until Index = 0
   LCD.Write(")")
End Event
 
// program start...
SetPin(PORTC.0)
 
// if we have some DS1820 devices connected to the bus,
// then display temperature for each one...
If Count > 0 Then
   While true
      ConvertAll
      delayms (1000)               
      FindAll(OnDisplayTemp)
      DelayMS(1000)
   Wend
EndIf
 
Last edited:
I slowed it down last night but it displays the RomIDs all bunched together plus it dosn't dimension senspor a from sensorb etc.
At this point, or until I get my Pickit2 I ordered,(going to use in tandem with Junebug (ICSP and USART) going to use just one sensor and use TempA compared to desired temperature.
 
Almost there BUT

When I pt the If DS18B20.find routine in I get syntax errors.
Tried putting it in different sections as well as try to build a subroutine.
The ADC, LCD, LED sections all work together but need to insert the DB18S20
Been ironing out the ADC etc and all work BUT the temperature. Only using one sensor.
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 10/6/2011                                                      *
*  Version : 1.0                                                            *
*  Notes   :compute voltage to temperature. Start with sensor at 50ohms.
*          : 180-200 ohms melts solder                                                               *
*          :                                                                *
*****************************************************************************
}
Device = 18F4520
Clock = 20
// some LCD options...
#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
// uses LCD and AD libraries...
Include "LCD.bas"
Include "ADC.bas"
Include "convert.bas"
include "DS18B20.bas"
Dim ADVal As Word
Dim LED_g As PORTB.0
Dim LED_r As PORTB.1
Dim Vref As Word
Dim  Vsensor As Word
Dim deg_s As Word         //DESIRED TEMP
Dim drf As Word           //DEFROST TIME
Dim TempA As Word
Dim TempB As ShortInt
      
// Read the AD ports and scale 
Function REF_V() As Word
 result = (ADC.Read(1) +1) /100  //1024 is divide by  tip sensor
End Function
Function sensor() As Word
 result = (ADC.Read(0) + 1)/100  // 100  //1024 is divide by  temp ref desired
End Function
 
// Start Of Program...
Input(PORTA.0)                  // DESIRED TEMP
Input(PORTA.1)                  // DEFROST TIMER
Low (LED_g)                     //set lEDs OFF
Low (LED_r)
ADCON1 = %10000000              //set up ADC regesters
ADCON0.7 = 1
ADCON0.6 = 1

       
DelayMS (150)
LCD.Cls
//========================================================

 While true
        // call function REF_V() and assign the result to word variable 'Vref'
        Vref = REF_V()
        Vsensor = sensor() 
             
Select Vref
       Case <1 deg_s = 32
       Case <2 deg_s = 33
       Case <3 deg_s = 34
       Case <4 deg_s = 35
       Case <5 deg_s = 36
       Case <6 deg_s = 37
       Case <7 deg_s = 38
       Case <8 deg_s = 39
       Case <9 deg_s = 40
End Select
Select Vsensor
       Case <1 drf = 10
       Case <2 drf = 12
       Case <3 drf = 14
       Case <4 drf = 16
       Case <5 drf = 17
       Case <6 drf = 19
       Case <7 drf = 21
       Case <8 drf = 23
       Case <9 drf = 25
       
End Select

  
   
   

    // main program loop...
     
   
        LCD.MoveCursor (1,1)
        LCD.Write("Desired temp= ", DecToStr(deg_s))
        DelayMS(250)
     
   // call function sensor() and assign the result to word variable 'Vsensor'
       
        LCD.MoveCursor (2,1)
        LCD.Write("DEFROST = ", DecToStr(drf),"    ")
        DelayMS(250)
        Wend
     if DS18B20.find then    
        while true
         convert 
         GetTemp(TempA, TempB)
   // LCD.WriteAt(1,1,"BX temp = "DecToStr(TempA))
          wend 
       endif     
     
  
    {   
   // compare Vsensor and Vref values
        If Vsensor < Vref Then      // not hot enough
            LED_r=1 
            LED_g=0
        Else                        // Vsensor must be >= Vref, so desired temperature
            LED_g=1 
            LED_r=0
        End If
        
    Wend
 
Sure there a
syntax error
your using find the wrong way. You need to look at what I posted you the part that shows how to use find your missing the Find(XXXXX) part
 
This is what find does
public function Find() as boolean
OW.Search(owSearchROM,OnFindFirst)
Result = OW.SearchAbort
end function

you could use it like this
Code:
while true
If Not find then
  XXXX  /do something here
endif 
// Then you end the while true with a 
wend

Or
Code:
while find
convert 
         GetTemp(TempA, TempB)
   LCD.WriteAt(1,1,"BX temp = "DecToStr(TempA))
             
 wend

You have the "wend" before the "endif"
 
Last edited:
over at Bigrof sitehttp://www.bradsprojects.com/electronics/BradsProjects-Welcome.html he suggested using this sub

Code:
Public Sub sens_tep()
    dim myTemp as word
     
        If DS18B20.Find Then 
            DS18B20.Convert
            mytemp = DS18B20.GetTemp()
            LCD.WriteAt(1,1,"BX temp = ",DecToStr(myTemp))
        EndIf
End Sub

at this point I am trying anythig. Been looking at the Swordfish Bible for possiable answers as well as looking at other codes for ideas of what and how to setup a sub seeing how the syyntax says a subroutine is needed.
 
And you didn't look at what I posted you had the wend before the endif
 
Status
Not open for further replies.

Latest threads

Back
Top