AN #04 - Reading the Dallas temperature sensor DS1821 |
|
Reading the Dallas temperature sensor DS1821
Here is a good example on how to read the Dallas DS1821.
It is written by Denis Bernard.
Download source code in Bas file
' Written by Denis Bernard
'
bernard@xs4all.nl
' 1 wire
communication with Dallas temperature sensor DS1821
' small PR35
Package, 3 pin , ± 2$. See Conrad elec.(europe)
' Temperatures
are printed in degres when >-1° ,
<125°
' for temperature
<0 , 256 - Temp_buf will give from -55 to -1
$crystal = 24000000 : $baud = 2400
Config 1wire = P3.3 'use P3.3 for 1 wire communication
Enable Int1 : Enable Interrupts 'Int1 optional (24MHz)
Dim Temp_buf As Byte , Stat_buf As Byte
1wreset
Print Err ' optional
1wwrite &H0C ' write status
1wwrite &B01000010 ' continu convertion
Debut:
1wreset
1wwrite &HEE ' start conversion
1wreset
1wwrite &HAA ' get temperature
Temp_buf = 1wread()
1wreset ' optional
1wwrite &HAC ' optional read status
Stat_buf = 1wread() ' optional 82
when DS1821 run
Print Temp_buf ; " " ; Stat_buf ; " " ; Tcon ' temperature is
Temp_buf
Goto Debut
End
'Note that int1 pin is used because of the 24 Mhz Crystal
'this slow-down the communication (with out Isr code) just
enough.
'make sure Tcon.2 (IT1) remain zero. if not [ Reset tcon.2
]
'with a 12 MHh crystal any pin can be used and no need for
interrupt.
' DS1821 pin1 = Gnd / pin2 = data in/out / pin 3 = +5v
' tested on Bascom-8151 + Bascom Lt
|