- 阅读权限
- 150
- UID
- 48
- 帖子
- 30
- 精华
- 2
- 注册时间
- 2013-2-28
- 在线时间
- 27 小时
- UID
- 48
- 帖子
- 30
- 精华
- 2
- 注册时间
- 2013-2-28
- 在线时间
- 27 小时
|
看老大发了一个单DS18B20的温度计 ,我来一个多路并联的.
Proteus仿真图 支持多个18B20 并连,开机自动探测总 传感器数量,并且挨个显示温度 这个仿真图是9个并联.
- '-------------------------------------------------------------------------------
- 'Name : 18B20_M.bas
- 'Copyright : http://www.avrbase.com
- 'Micro : AtMega8
- 'Author : yoya0303
- '-------------------------------------------------------------------------------
- $regfile = "m8def.dat"
- $crystal = 8000000
- $baud = 19200
- $hwstack = 32 ' default use 32 for the hardware stack
- $swstack = 10 'default use 10 for the SW stack
- $framesize = 40 'default use 40 for the frame space
- Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.3 , Rs = Portb.2
- Config Lcdbus = 4
- Config 1wire = Portd.5 'use this pin
- Dim Reg_no(8) As Byte
- Dim K As Byte
- Dim I As Byte , W As Word
- Dim Dsint As Integer
- Dim Data1(2) As Byte
- Dim Sig As Byte
- Dim N As Integer
- Rst:
- Locate 1 , 1
- N = 1wirecount() '搜索联机 18B20个数
- Lcd "MAX= "
- Lcd Str(n) 'LCD 显示18B20个数
- Wait 2
- Cls
- 1wreset
- 1wwrite &HCC
- 1wwrite &H44 '跳过ROM匹配 全部开始转换
- K = 1
- Reg_no(1) = 1wsearchfirst() '搜索ROM
- ' 1wverify Reg_no(1) '校验ROM
- ' If Err = 1 Then '校验出错退出
- ' Goto Endthis
- ' End If
- For I = 1 To 8 '输出18B20 ROM号
- Print Hex(reg_no(i));
- Next
- 1wreset
- 1wwrite &H55 '匹配ROM
- For I = 1 To 8
- 1wwrite Reg_no(i)
- Next I
- 1wwrite &HBE '读取温度
- ''*************************************************
- Data1(1) = 1wread(2)
- Dsint = Makeint(data1(1) , Data1(2))
- If Data1(2) > 16 Then '判断温度正负
- Dsint = Not Dsint
- Dsint = Dsint + 1
- Dsint = Dsint / 16
- Sig = 1
- Else
- Sig = 0
- Dsint = Dsint / 16
- End If
- ''*************************************************
- Locate 1 , 1 '显示温度
- If Sig = 1 Then
- Lcd "-" ; Str(dsint) ; Chr(0)
- Print " -";
- Print Str(dsint)
- Else
- Print " +";
- Print Str(dsint)
- Lcd "+" ; Str(dsint) ; Chr(0)
- End If
- Locate 2 , 1 'LCD显示当前ROM
- For I = 1 To 8
- Lcd Hex(reg_no(i))
- Next I
- Wait 1
- Do
- Do
- 'Now search for other devices
- Reg_no(1) = 1wsearchnext() '继续搜索ROM
- ' 1wverify Reg_no(1) '校验ROM
- ' If Err = 1 Then '校验出错退出
- ' Goto Endthis
- ' End If
- For I = 1 To 8 '串口输出ROM
- Print Hex(reg_no(i));
- Next I
- 1wreset
- 1wwrite &H55
- For I = 1 To 8
- 1wwrite Reg_no(i)
- Next I
- 1wwrite &HBE '读取温度
- ''*************************************************
- Data1(1) = 1wread(2)
- Dsint = Makeint(data1(1) , Data1(2))
- If Data1(2) > 16 Then
- Dsint = Not Dsint
- Dsint = Dsint + 1
- Dsint = Dsint / 16
- Sig = 1
- Else
- Sig = 0
- Dsint = Dsint / 16
- End If
- ''*************************************************
- Locate 1 , 1
- If Sig = 1 Then
- Lcd "-" ; Str(dsint) ; Chr(0)
- Print " -";
- Print Str(dsint)
- Else
- Lcd "+" ; Str(dsint) ; Chr(0)
- Print " +";
- Print Str(dsint)
- End If
- Locate 2 , 1
- For I = 1 To 8
- Lcd Hex(reg_no(i))
- Next I
- Wait 1
- K = K + 1
- If K >= N Then '当计数到达总18B20个数,开始重新转换
- Goto Rst
- End If
- Loop Until Err = 1
- Locate 1 , 6
- Lcd "ERROR"
- Loop
- Endthis:
- Print Err
- Cls
- Locate 1 , 1
- Lcd "Error"
- End
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
|