圣龙扬特-AVR电子

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

来一个 多个DS18B20并联的温度检测

[复制链接]

7

主题

0

好友

3176

积分

超级版主

Rank: 8Rank: 8

UID
48
帖子
30
精华
2
注册时间
2013-2-28
在线时间
27 小时
跳转到指定楼层
楼主
发表于 2013-3-12 18:53:54 |只看该作者 |倒序浏览
看老大发了一个单DS18B20的温度计 ,我来一个多路并联的.
Proteus仿真图 支持多个18B20 并连,开机自动探测总 传感器数量,并且挨个显示温度 这个仿真图是9个并联.
  1. '-------------------------------------------------------------------------------
  2. 'Name                     : 18B20_M.bas
  3. 'Copyright                : http://www.avrbase.com
  4. 'Micro                    : AtMega8
  5. 'Author                   : yoya0303
  6. '-------------------------------------------------------------------------------
  7. $regfile = "m8def.dat"
  8. $crystal = 8000000
  9. $baud = 19200
  10. $hwstack = 32                                               ' default use 32 for the hardware stack
  11. $swstack = 10                                               'default use 10 for the SW stack
  12. $framesize = 40                                             'default use 40 for the frame space

  13. Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.3 , Rs = Portb.2

  14. Config Lcdbus = 4

  15. Config 1wire = Portd.5                                      'use this pin


  16. Dim Reg_no(8) As Byte
  17. Dim K As Byte

  18. Dim I As Byte , W As Word

  19. Dim Dsint As Integer
  20. Dim Data1(2) As Byte
  21. Dim Sig As Byte
  22. Dim N As Integer

  23. Rst:
  24. Locate 1 , 1
  25.    N = 1wirecount()                                         '搜索联机 18B20个数
  26.    Lcd "MAX= "
  27.    Lcd Str(n)                                               'LCD 显示18B20个数
  28.    Wait 2
  29.    Cls
  30.    1wreset
  31.    1wwrite &HCC
  32.    1wwrite &H44                                             '跳过ROM匹配 全部开始转换
  33.    K = 1
  34.    Reg_no(1) = 1wsearchfirst()                              '搜索ROM
  35.   ' 1wverify Reg_no(1)                                       '校验ROM

  36.   ' If Err = 1 Then                                          '校验出错退出
  37.   '    Goto Endthis
  38.   ' End If
  39.    For I = 1 To 8                                           '输出18B20 ROM号
  40.       Print Hex(reg_no(i));
  41.    Next

  42.    1wreset
  43.    1wwrite &H55                                             '匹配ROM
  44.    For I = 1 To 8
  45.       1wwrite Reg_no(i)
  46.    Next I
  47.    1wwrite &HBE                                             '读取温度
  48.       ''*************************************************
  49.    Data1(1) = 1wread(2)

  50.    Dsint = Makeint(data1(1) , Data1(2))
  51.    If Data1(2) > 16 Then                                    '判断温度正负
  52.       Dsint = Not Dsint
  53.       Dsint = Dsint + 1
  54.       Dsint = Dsint / 16
  55.       Sig = 1
  56.    Else
  57.       Sig = 0
  58.       Dsint = Dsint / 16
  59.    End If
  60.          ''*************************************************
  61.    Locate 1 , 1                                             '显示温度
  62.    If Sig = 1 Then
  63.       Lcd "-" ; Str(dsint) ; Chr(0)
  64.       Print " -";
  65.       Print Str(dsint)
  66.    Else
  67.       Print " +";
  68.       Print Str(dsint)

  69.       Lcd "+" ; Str(dsint) ; Chr(0)
  70.    End If
  71.    Locate 2 , 1                                             'LCD显示当前ROM
  72.    For I = 1 To 8
  73.          Lcd Hex(reg_no(i))
  74.    Next I

  75.    Wait 1
  76. Do

  77.    Do

  78. 'Now search for other devices


  79.       Reg_no(1) = 1wsearchnext()                            '继续搜索ROM
  80. '     1wverify Reg_no(1)                                    '校验ROM

  81. '      If Err = 1 Then                                       '校验出错退出
  82. '         Goto Endthis
  83. '      End If
  84.       For I = 1 To 8                                        '串口输出ROM
  85.             Print Hex(reg_no(i));
  86.       Next I
  87.       1wreset
  88.       1wwrite &H55
  89.       For I = 1 To 8
  90.          1wwrite Reg_no(i)
  91.       Next I
  92.       1wwrite &HBE                                          '读取温度
  93.       ''*************************************************
  94.          Data1(1) = 1wread(2)

  95.          Dsint = Makeint(data1(1) , Data1(2))
  96.          If Data1(2) > 16 Then
  97.          Dsint = Not Dsint
  98.          Dsint = Dsint + 1
  99.          Dsint = Dsint / 16
  100.          Sig = 1
  101.          Else
  102.          Sig = 0
  103.          Dsint = Dsint / 16
  104.          End If
  105.        ''*************************************************
  106.          Locate 1 , 1
  107.       If Sig = 1 Then
  108.          Lcd "-" ; Str(dsint) ; Chr(0)
  109.          Print " -";
  110.          Print Str(dsint)
  111.       Else
  112.          Lcd "+" ; Str(dsint) ; Chr(0)
  113.          Print " +";
  114.          Print Str(dsint)
  115.       End If
  116.           Locate 2 , 1
  117.       For I = 1 To 8
  118.             Lcd Hex(reg_no(i))
  119.       Next I
  120.       Wait 1


  121.       K = K + 1
  122.      If K >= N Then                                         '当计数到达总18B20个数,开始重新转换
  123.      Goto Rst
  124.      End If
  125.    Loop Until Err = 1

  126.        Locate 1 , 6
  127.        Lcd "ERROR"
  128. Loop

  129. Endthis:
  130. Print Err
  131. Cls
  132. Locate 1 , 1
  133. Lcd "Error"



  134. End
复制代码

本帖子中包含更多资源

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

回复

使用道具 举报

67

主题

4

好友

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
2
帖子
343
精华
0
注册时间
2013-2-20
在线时间
366 小时
沙发
发表于 2013-3-12 19:33:18 |只看该作者
有电路有程序,这个要顶,加精。
回复

使用道具 举报

2

主题

0

好友

3175

积分

超级版主

Rank: 8Rank: 8

UID
6
帖子
32
精华
0
注册时间
2013-2-26
在线时间
18 小时
板凳
发表于 2013-3-12 21:00:27 |只看该作者
支持喷神
回复

使用道具 举报

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

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

GMT+8, 2024-6-17 12:15 , Processed in 0.214454 second(s), 18 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部