DS18B20.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. local DS18B20 = {}
  2. new_temp=-100--中间过渡数据
  3. gongshui_temp=-1000--供水温度/进水温度
  4. huishui_temp=-1000--回水温度/出水温度
  5. mark=0--采集成功标志
  6. TEMP_mark=0--温度采集成功标志
  7. --=============================================================
  8. --匹配温度ID,并发送温度转换命令并做温度转换
  9. local function read_ds18b20(id)
  10. local tbuff = zbuff.create(10)
  11. local succ,crc8c,range,t
  12. local rbuff = zbuff.create(9)
  13. --如果有多个DS18B20,需要带上ID
  14. tbuff:write(0x55)
  15. tbuff:copy(nil, id)
  16. tbuff:write(0xb8)
  17. --如果只有1个DS18B20,就用无ID方式
  18. --tbuff:write(0xcc,0xb8)
  19. while true do
  20. tbuff[tbuff:used() - 1] = 0x44
  21. succ = onewire.tx(0, tbuff, false, true, true)
  22. if not succ then
  23. return
  24. end
  25. while true do
  26. succ = onewire.reset(0, true)
  27. if not succ then
  28. return
  29. end
  30. if onewire.bit(0) > 0 then
  31. log.info("温度转换完成")
  32. break
  33. end
  34. sys.wait(10)
  35. end
  36. tbuff[tbuff:used() - 1] = 0xbe
  37. succ = onewire.tx(0, tbuff, false, true, true)
  38. if not succ then
  39. return
  40. end
  41. succ,rx_data = onewire.rx(0, 9, nil, rbuff, false, false, false)
  42. crc8c = crypto.crc8(rbuff:toStr(0,8), 0x31, 0, true)
  43. if crc8c == rbuff[8] then
  44. range = (rbuff[4] >> 5) & 0x03
  45. -- rbuff[0] = 0xF8
  46. -- rbuff[1] = 0xFF
  47. t = rbuff:query(0,2,false,true)
  48. t = t * (5000 >> range)
  49. t = t / 10000
  50. new_temp=KeepDecimalPlace(t, 2)--处理数据四舍五入
  51. mark=1--采集成功
  52. log.info("处理后的温度=",new_temp)
  53. else
  54. log.info("RAM DATA CRC校验不对", mcu.x32(crc8c), mcu.x32(rbuff[8]))
  55. new_temp=-100--数据校验错误
  56. mark=0--采集失败
  57. return
  58. end
  59. sys.wait(50)
  60. end
  61. end
  62. --=============================================================
  63. --读温度ID及判断数据
  64. local function test_ds18b20()
  65. local succ,rx_data
  66. local id = zbuff.create(8)
  67. local crc8c
  68. --onewire.init(0)
  69. --onewire.timing(0, false, 0, 500, 500, 15, 240, 70, 1, 15, 10, 2)
  70. --for i=1,5 do
  71. while true do
  72. onewire.init(0)
  73. onewire.timing(0, false, 0, 500, 500, 15, 240, 70, 1, 15, 10, 2)
  74. id:set() --清空id
  75. succ,rx_data = onewire.rx(0, 8, 0x33, id, false, true, true)
  76. if succ then
  77. if id[0] == 0x28 then
  78. crc8c = crypto.crc8(id:query(0,7), 0x31, 0, true)
  79. if crc8c == id[7] then
  80. log.info("探测到DS18B20", id:query(0, 7):toHex())
  81. read_ds18b20(id)
  82. log.info("DS18B20离线,重新探测")
  83. new_temp=-200--异常处理
  84. mark=2--识别出,但是采集失败
  85. else
  86. log.info("ROM ID CRC校验不对", mcu.x32(crc8c), mcu.x32(id[7]))
  87. new_temp=-300--异常处理
  88. mark=3--识别出,CRC校验不对
  89. end
  90. else
  91. log.info("ROM ID不正确", mcu.x32(id[0]))
  92. new_temp=-400--异常处理
  93. mark=4--识别出,D不正确不对
  94. end
  95. end
  96. log.info("没有检测到DS18B20, 5秒后重试")
  97. new_temp=-500--异常处理
  98. mark=5--识别出,D不正确不对
  99. sys.wait(100)
  100. end
  101. end
  102. --=============================================================
  103. local function select_ds18b20()
  104. local water_in_pin=11
  105. local water_out_pin=9
  106. local water_in=gpio.setup(water_in_pin, 0)--初始化状态
  107. local water_out=gpio.setup(water_out_pin, 0)--初始化状态
  108. while true do
  109. water_in(1)
  110. water_out(0)
  111. mark=0--重置一下标志位
  112. onewire.deinit(0) -- 切换的时候一定要先关闭单总线,在读取的时候重新初始化
  113. sys.wait(1000)
  114. TEMP_mark=0--初始化一下
  115. log.info("进水标志位mark=",mark)
  116. if mark==1 then
  117. gongshui_temp=new_temp
  118. log.info("采集供水/进水成功,温度=",gongshui_temp)
  119. else
  120. gongshui_temp=-1000
  121. log.info("采集供水/进水失败,温度=",gongshui_temp)
  122. end
  123. water_in(0)
  124. water_out(1)
  125. mark=0--重置一下标志位
  126. onewire.deinit(0) -- 切换的时候一定要先关闭单总线,在读取的时候重新初始化
  127. sys.wait(1000)
  128. log.info("回水标志位mark=",mark)
  129. log.info("采集供水/进水成功,电源状态切换")
  130. if mark==1 then
  131. huishui_temp=new_temp
  132. log.info("采集回水/出水成功,温度=",huishui_temp)
  133. else
  134. huishui_temp=-1000
  135. log.info("采集回水/出水失败,温度=",huishui_temp)
  136. end
  137. TEMP_mark=1--温度采集成功
  138. end
  139. end
  140. --=============================================================
  141. if onewire then
  142. sys.taskInit(test_ds18b20)
  143. else
  144. log.info("no onewire")
  145. end
  146. sys.taskInit(select_ds18b20)
  147. --=============================================================