圣龙扬特-AVR电子

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 966|回复: 1
打印 上一主题 下一主题

DS18B20温度计

[复制链接]

17

主题

2

好友

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
帖子
54
精华
0
注册时间
2013-2-20
在线时间
72 小时
跳转到指定楼层
楼主
发表于 2013-2-28 17:16:54 |只看该作者 |倒序浏览
DS18B20温度计(转载请注明出自http://www.avrbase.com

 
 利用DS18B20做了一个温度计,主要用于测量液体温度。使用效果还不错,现将电路和程序奉上。
  程序用BASCOM-AVR编写,比较简单,以前从没用过DS18B20,现学现编。采用9位精度,设置好后存入DS18B20的EEPROM。程序没仔细推敲,只是完成简单的显示温度功能。最多显示3位数字,(-10,100)显示1位小数,其它区间显示整数。
  




'-------------------------------------------------------------------------------
'Name                     : Thermometer(DS18B20).bas
'Copyright                : http://www.avrbase.com
'Micro                    : AtMega8

'Author                   : slyt/箫天
'-------------------------------------------------------------------------------
$regfile = "m8def.dat"
$crystal = 8000000
'$baud = 115200

$hwstack = 32
$swstack = 10
$framesize = 40

Config Portb = Output                                       '0-7接LED A-G-DOT
Config Portc = Output                                       '0,1,2分别接LED共阳8,9,12(分别是个位,十位,百位)

'Config Serialin = Buffered , Size = 32
'Config Serialout = Buffered , Size = 32
Config 1wire = Portd.2

Dim Sled As String * 1
Dim Idx As Byte
Dim Temp As Single
Dim Wendu As Single
Dim Waittime As Byte
Dim Ledstring As String * 4
Dim Ledstr As String * 4

Dim Myword As Word
Dim Dsdata(10) As Byte
Dim Myinteger As Integer

Portb = 0
Portc = 0
Osccal = &HB6

'转换精度是9位,精度0.5度,转换时间约为100ms
'先启动一次转换
'(
   '初始化指令
   1wreset
   '发送跳过ID码匹配指令
   1wwrite &HCC
   '1wreset
   'Write Scratchpad(写暂存存储器) [4EH]
   1wwrite &H4E
   'TH
   1wwrite 0
   'LH
   1wwrite 0
   '设置9位转换
   1wwrite &H1F

   1wreset
   1wwrite &HCC                                                '跳过ID码
   1wwrite &H48                                                '将RAM的数据写入EEPROM中

   Waitus 100
')

1wreset
1wwrite &HCC                                                '跳过ID码
1wwrite &HB8                                                '将EEPROM的数据读到RAM中

1wreset
'发送跳过ID码匹配指令
1wwrite &HCC
'发送温度转换指令
1wwrite &H44

Waitms 100

'(
   1wreset
   1wwrite &HCC
   '读取温度命令
   1wwrite &HBE
   Dsdata(1) = 1wread(2)

   Myinteger = Makeint(dsdata(1) , Dsdata(2))

   Temp = Myinteger * 0.0625

   If Err = 1 Then
      Ledstring = "----"
      Else
      Ledstring = Fusing(temp , "#.#")
   End If

   '1wreset
   '发送跳过ID码匹配指令
   '1wwrite &HCC
   '发送温度转换指令
   '1wwrite &H44
')


1wreset
1wwrite &HCC
'读取温度命令
1wwrite &HBE
Dsdata(1) = 1wread(2)
Myinteger = Makeint(dsdata(1) , Dsdata(2))
Temp = Myinteger * 0.0625
Ledstring = Fusing(temp , "#.#")

Do

   1wreset
   1wwrite &HCC
   '读取温度命令
   1wwrite &HBE
   Dsdata(1) = 1wread(2)

   Myinteger = Makeint(dsdata(1) , Dsdata(2))
   Temp = Myinteger * 0.0625
   Wendu = Wendu + Temp

   Incr Dsdata(10)

   If Dsdata(10) = 10 Then
      Wendu = Wendu / 10

      Ledstr = Fusing(wendu , "#.#")

      If Ledstr <> Ledstring Then
         Ledstring = Ledstr
      End If
      Wendu = 0
      Dsdata(10) = 0
   End If

   If Len(ledstring) < 4 Then
      Ledstring = "0" + Ledstring
   End If

   1wreset
   '发送跳过ID码匹配指令
   1wwrite &HCC
   '发送温度转换指令
   1wwrite &H44

   '显示第一个字符
   Waittime = 0
   Sled = Mid(ledstring , 1 , 1)
   Idx = Val(sled)
   If Idx = 0 Then
      If Sled = "-" Then
         Idx = 10
         Portb = Lookup(10 , Leddata)
         Waittime = 8
         Portc = &B100
      End If
      Else
      Portb = Lookup(idx , Leddata)
      Waittime = 8
      Portc = &B100
   End If
   Waitms Waittime


   '显示第二个字符
   Sled = Mid(ledstring , 2 , 1)
   Idx = Val(sled)
   If Sled = "-" Then
      Idx = 10
   End If
   Portb = Lookup(idx , Leddata)
   Portc = &B010
   If Mid(ledstring , 3 , 1) = "." Then
      Portb.7 = 0
   End If
   Waitms 8

   '显示第三个字符
   Sled = Mid(ledstring , 4 , 1)
   Idx = Val(sled)
   If Sled = "-" Then
      Idx = 10
   End If
   Portb = Lookup(idx , Leddata )
   Portc = &B001

Loop

End

'LED数字 (0-9)
Leddata:
Data &B11000000 , &B11111001 , &B10100100 , &B10110000 , &B10011001
Data &B10010010 , &B10000010 , &B11111000 , &B10000000 , &B10010000

'"-"
Data &B10111111

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

相关帖子

回复

使用道具 举报

14

主题

0

好友

3519

积分

超级版主

Rank: 8Rank: 8

UID
50
帖子
115
精华
1
注册时间
2013-2-28
在线时间
39 小时

突出贡献

沙发
发表于 2013-2-28 17:18:04 |只看该作者
顶顶~有时间的学习学习AVR啦~都忘得差不多了~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

Archiver|手机版|圣龙扬特-AVR电子 ( 鲁ICP备05022832号 )

GMT+8, 2024-5-4 03:21 , Processed in 0.238815 second(s), 31 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部