Advertisement  

Thursday, 03 July 2025
     
 
Main Menu
Home Home
Shop Shop
News News
BASCOM-AVR BASCOM-AVR
BASCOM-8051 BASCOM-8051
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-AVR Help BASCOM-AVR Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login
Username

Password

If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News
 
     
 

 
   
     
 
AN #04 - Reading the Dallas temperature sensor DS1821 Print
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