- 阅读权限
- 200
- UID
- 1
- 帖子
- 54
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 72 小时
- UID
- 1
- 帖子
- 54
- 精华
- 0
- 注册时间
- 2013-2-20
- 在线时间
- 72 小时
|
利用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
|
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
|