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 One Wire Thermometer Swordfish Example

Status
Not open for further replies.

gramo

New Member
Yet another conversion complete, this one was rather simple as its rather similar to one of the samples that come with SF. This time its for the DS18B20, a Dallas One Wire thermometer.

There are 3 Dallas digital temperature sensors, so be sure to get the correct one. There is the 1820, 18S20 and 18B20. The 18S20 is the successor to the 1820, and does not drift over time. The 18B20 further enhanced, and offers faster conversions. Be aware that the 1820 and 18S20 will not function correctly with this 18B20 example.

**broken link removed**
**broken link removed**

Code:
Device = 18F452
Clock = 20

// import modules...
Include "DS18B20.bas"
Include "convert.bas"
Include "usart.bas"

// working variables...
Dim 
   TempA As ShortInt,
   TempB As Word
   
// program start...
SetBaudrate(br19200)
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 DS18B20.Find Then
    While true
      Convert
      GetTemp(TempA, TempB)
      USART.Write(DecToStr(TempA),".",DecToStr(TempB,4), " C", 13, 10)
      DelayMS(1000)
    Wend   
Else
    USART.Write("No device found", 13, 10)
EndIf

Be sure to attach the 4.7K pull-up resistor to the databus, this is a requirement of the Dallas 1-Wire system. Most of the popular One Wire devices have their own libraries within Swordfish, I'm using the DS18B20 library to make this as simple as possible. Note that Swordfish also has an extensive One Wire library should you want to create your own custom interfacing module.
 
I use love these things. They are great for building inexpensive sensing and control networks.

1 Wire networks can be quite long (up to 750 meters) and can exist in **broken link removed**. You can find drivers for them in many languages for many different microcontrollers.

The C# program CntrlOne is work in progress. It depends on 1 wire devices to monitor temperatures including water temp in the greenhouse pond. It uses text to voice to warn when temperatures fall outside acceptable limits.
 

Attachments

  • CntrlOne.jpg
    CntrlOne.jpg
    32 KB · Views: 850
Hi,

i am trying to use the code of gramo to test the ds18b20 in isis simulator and swordfish but without success.

i am a beginner in this two softwares and i dont know if i do everything right in isis. i only copied the code into swordfish and let it compile to an hex file.

then i build the circuit in isis like it is shown in the picture. but it shows me only "-1.0 C" repeated on the virtual terminal.

here you can find my files of the project:
**broken link removed**

it would be nice if someone can tell me what i am doing wrong.

greetz
Basti
 
The first thing you need to do is download the latest version of Swordfish SE

**broken link removed**

When you have done that and installed it, I would suggest you make the following changes to your program.

Code:
// device and clock speed...
Device = 18F452
Clock = 4

// import modules...
#option OW_PIN = PORTC.0
Include "DS18B20.bas"
Include "convert.bas"
Include "usart.bas"

//FAMILY $28 ($8E) ($000000B8C530)  <- ISIS DS18B20
Const Sensor_A(8) As Byte = ($28, $30, $C5, $B8, $00, $00, $00, $8E)

// 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)
End Sub

// program start...
SetBaudrate(br9600)
While true
   DisplaySensor(Sensor_A)
   DelayMS(1000)
Wend

A few things to note

(a) The use of #option OW_PIN = PORTC.0. Given that you are using a 4MHz clock, using indirect pin addressing with SetPin() is not really recommended. Using OW_PIN ensures fast port access when using the bus

(b) You ISIS schematic only has one 18B20. I've changed the code to reflect this

(c) You device ID is incorrect for the ISIS 18B20. I've changed this also. You can obtain the settings I used by compiling and running "OWSearch.bas" which is included in the Swordfish samples folder.

If you follow the above the temperature displayed in ISIS is correct, although do give the simulator a little time when running your program to see the results displayed
 
basti523 said:
i am trying to use the code of gramo to test the ds18b20 in isis simulator and swordfish but without success.
I tried that program too and it doesn't work. Have a look at this thread, 7th post down from the top. I got a 18B20 working on Junebug in Swordfish BASIC. The code there might help you.
 
ok i´ve tested both programs and they both work. the problem is the cpu load shows me 100%! and everything is very very slow...

in the swordfish wiki it says the OneWire (OW) library uses indirect addressing to access the OW port pin. and this cost many more processor clock cycles than if direct addressing was used.

so i tried the direct addressing method too. but all over the same high cpu load.

but at 20mhz clock 100%??? maybe this is only a problem in isis?
 
How can you ever get a CPU load less than 100%? What will it do when it's not running? If you put a big delay in your code it will still read the temperature from the 1820 and will still show 100% CPU usage!!

If it is very slow then I suspect that you are using the internal oscillator and it is not getting set to high speed until after the initialization code.

Mike.
 
I´ve found the problem. The simulation with ISIS of digital circuitry is two or three orders of magnitude (i.e. up to 1000 times) faster than the simulation of analogue circuitry. I changed the 4.7k resistor to a digital one and the cpu load goes down to a stable 90% value. Now everything is in realtime simulated :)

very big thanks to everyone!
 
Status
Not open for further replies.

Latest threads

Back
Top