18F DS18B20 Example

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 is further enhanced, and offers faster conversions. Be
aware that the 1820 and 18S20 will not function correctly with this
18B20
example.
The
DS18B20
can be brought from
www.newark.com
The basic
wiring diagram

Note the PIC's
power supply/oscillator are not shown
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.
Device = 18F452
Clock = 20
// import modules...
Include "DS18B20.bas"
Include "convert.bas"
Include "usart.bas"
// working variables...
Dim
TempA As ShortInt,
TempB As Word
// Start Of Program...
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
Where you can get the components;

 | Site Tutorial Index |
|  | 16F PIC Examples |
|  | 18F PIC Examples |
| |  | 7 Segment Displays |
| |  | 7 Segment Displays |
| |  | RS232 and UART |
| |  | Code Snippets |
|  | Handy Tips |