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.

MrDEB

Well-Known Member
been hacking at this but to no avail.
The LCD works as I first got the HELLO WORLD code to work.
I must be missing something?
I have only one DS18B20 connected but going to connect three sensors to three different pins but going one step at a time. All I am getting to display are a bunch of boxes (I had contrast adjust for the hello world test but tried readjusting to no avail.
Code:
Device = 18F4520
Clock = 8
Config OSC = INTIO67
// import modules...
Include "DS18B20.bas"
Include "convert.bas"
Include "lcd.bas"
 
// working variables...
Dim
 TempA As ShortInt,
 TempB As Word
 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.Write(1,1,DecToStr(TempB,4), " C")
 DelayMS(1000)
 Wend 
Else
 LCD.Write("No device found")
EndIf
 
you can do these
1) there must be a LCD initialization routine for the LCD. and i cant find it anywhere in your code.
2) check whether the control pins ie RS, E and R/W pins connected to the right pins of the micro controller and the data pin to the respective port.
3) try changing the display.
 
OUPS I forgot the OPTIONS for the LCD.
got it kinda displaying but don't need the TempA which is the romID
 
MrDEB, the Swordfish example works as written, as has been shown at Digital-DIY.com (I won't post a link as we know how much Electromaster hates that).

There are at least two problems in the code you posted:

1. The LCD connections are not defined. You're probably not writing to the LCD at all.

2. You have a huge lack of understanding about the routine to read the DS18B20. TempA and TempB are not the temperature readings of two sensors A & B! TempA & TempB are temporary variables to read the 12 bits of data from the DS18B20 which is converted to temperature of a single sensor. If you compare the dimension statements for TempA & TempB you should be able to see that they are not the same.

Copy&Paste (tm) programming doesn't work if you don't understand the original program. Ever.
 
Last edited:
making progress

You are right about I failed to include the LCD options
Trying to get around the TempA so I can add the temperatures of all three sensors (at present I have two sensors connected to B2 and B3
I have a hunch your on the right track about the scratch pad.
Was hoping to dimension the sensor on B2 different than B3 and B4 sensors
sensor(0), sensor(1), sensor(2). Trying to get a better understanding, making some progress but not much.
The code I post here is what I have been hacking on with very little copy n paste.
Am hoping if I can dimension the sensors then just use a FOR NEXT statement?
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
 
The way you want to do this with 3 different data pins could work. No comment on the efficiency of the method or the rational, which all "justification" aside, is the inability to understand how to use the ROMIDs of each sensor.

However, the logic flow of the code you have posted is screwed up. Sorry, that's the kindest thing that can be said about it.

Code:
If DS18B20.Find Then
    While True
       ...
       ...
       ...
    Wend

If a sensor is found, the While/Wend statement within the If/Then statement will be executed forever. The rest of the code will never be executed.

You need to determine EXACTLY what lines read the sensor and display the results and make a flow chart to read each sensor in turn.

Good luck and good bye.
 
Trying to decipher when and where the code reads the romID and temperature.
Considering using a FUNCTION sub routine??
 
not fully comprending TempA or TempB

wanting to be able to add all three DS18B20's together then divide by 3 but trying to separate the three temps??
This code only has 2 sensors connected but ant 3 sensors.
UNLESS there is an easier method?
I tried changing the DISPLAYALL.bas but failed. Using an LCD instead of the uart. Tried changing all the uart to lcd.


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)      //sensor ports

 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
 GetTemp(TempA, TempB)
 Convert
 TempB=1
 LCD.WriteAt(1,4,DecToStr(TempA))
 
// Wend
// EndIf
//xxxxxxxxxxxxxxxxxx
 DelayMS(500)
 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
 GetTemp(TempA, TempB)
 TempB=2
 LCD.WriteAt(2,4,DecToStr(TempA))
 
 Wend 
Else
 LCD.Write("No device found")
EndIf
 
David Barker (the creator of Swordfish Basic) has an example on the Swordfish website to do *EXACTLY* what you should be doing. He's reading several sensors connected to a single One-Wire network using known ROM IDs. He covers reading and displaying several temperatures.

If you copy&paste (TM) from that example you *MIGHT* be able to average and display the results. (It's a total piece of cake using staring from his example - I just don't know if YOU can make it work).

Reading Multiple One-Wire Sensors

So how do you know the ROM IDs of the sensors you have? One of the Swordfish One-Wire examples will read and display the ROM IDs of connected devices.

The solution on a silver platter for you.
 
I have looked at and tried altering the code your refering to but keep getting errors when I change USART to LCD.
Taking a fresh look at again this morning.
 
attempting to convert from USART to LCD

not having to much luck. Using the example you posted a link to. Will keep plugin away

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
 
// import modules...
#option OW_PIN = PORTb.3
Include "DS18B20.bas"
Include "convert.bas"
Include "usart.bas"
include "lcd.bas"
// FAMILY $28 ($1D) ($0000002CDBAE) <- DS18B20
// FAMILY $28 ($3F) ($0000002CD16E) <- DS18B20
Const Sensor_A(8) As Byte = ($28, $AE, $DB, $2C, $00, $00, $00, $1D)
Const Sensor_B(8) As Byte = ($28, $6E, $D1, $2C, $00, $00, $00, $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)
                             //USART.Write(DecToStr(TempA),".",DecToStr(TempB,2), $BA, "C",13,10)
   LCD.Cls
   lcd.writeat(1,1,DecToStr(TempA),(TempB))
End Sub

// program start...
                                   //SetBaudrate(br115200)
While true
   DisplaySensor(Sensor_A)
   DisplaySensor(Sensor_B)
   DelayMS(1000)
Wend
 
I need to figure out how to put the ROMID numbers on the LCD or reconfigure the usart/Junebug combo
the CONST statement is the ROM ID
 
Why on earth do I keep trying???

Please READ the link I posted instead of just Copy&pasting(tm).. The ROM IDs Must be the serial numbers of the sensors You actually have connected!

If you can get past that point and actually read a sensor, then you need to fix your LCD statement. You still don't get that TempA (or whatever it is in this version) is the whole-number part of the temperature and TempB is the fractional part of the temperature. These most assurately are not the temperatures of two different sensors.. STOP. DO NOT PASS GO until you understand this.
 
I understand what your saying. The romID numbers are what I need. Found **broken link removed** and going to try the ow viewer. Tried using a Uart but the Junebug/Uart has issues.
Working on getting the romID numbers. Trying to extract them and display on the LCD but havn't made any progress.
 
Mrdeb try this one first get it working and then add to more sensors

Code:
// if device and clock are omitted, then the compiler defaults to 
// 18F4520 @ 8MHz - they are just used here for clarity...
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"

// working variables...
Dim 
   TempA As ShortInt,
   TempB As Word
   
// program start...
SetPin(PORTC.0)

// 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
   LCD.Write("No device found")
Else
   While true
      Convert
      GetTemp(TempA, TempB)
      LCD.Write(DecToStr(TempA),".",DecToStr(TempB,4))
      DelayMS(1000)
   Wend
EndIf
 
Last edited:
To use more then one sensor you use FindAll then read them one at a time in a loop.

The FindAll looks for all of the sensors and can list there ID's or just save them as 1 , 2 , 3

and read them one at a time add them and then divide by 3
 
Last edited:
my posted code in post #5 works but it displays the same temperature.
Will try FINDALL
At present I have one sensor connected to B2 and a second on B3
WAs/AM hoping to just use the output from each sensor but ??
 
I commented on the code what it is its just a file saved in your user modules named int08 and if you open it it looks like this
Code:
Module intio8
OSCCON =$72

Now if you use FindAll to get the ID's of all 3 sensors and save that you could then do the math on them to get a average to work with.

But after looking at the modules I don't see a handler to do more then tell how many are on the wire so you have to add some code to make this work.

Which means your going to have to find how many you have find the ID's then
read them one at a time there a sample of how this works in the help file
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top