main.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. -- LuaTools需要PROJECT和VERSION这两个信息
  2. PROJECT = "onewiredemo"
  3. VERSION = "1.0.0"
  4. -- sys库是标配
  5. sys = require("sys")
  6. -- 以 log.info("ABC", "DEF", 123) 为例, 假设该代码位于main.lua的12行
  7. -- 调试风格1, 添加额外的调试信息
  8. -- I/main.lua:12 ABC DEF 123
  9. log.style(1)
  10. --[[
  11. 接线说明:
  12. DS18B20 Air8000
  13. 1. GND -> GND
  14. 2. VDD -> 3.3V
  15. 3. DATA -> GPIO2
  16. 注意:
  17. 1. ONEWIRE功能支持在4个引脚使用, 但硬件通道只有一个, 默认是GPIO2
  18. 2. 如需切换到其他脚, 参考如下切换逻辑, 选其中一种
  19. mcu.altfun(mcu.ONEWIRE, 0, 17, 4, 0) -- GPIO2, 也就是默认值
  20. mcu.altfun(mcu.ONEWIRE, 0, 18, 4, 0) -- GPIO3
  21. mcu.altfun(mcu.ONEWIRE, 0, 22, 4, 0) -- GPIO7
  22. mcu.altfun(mcu.ONEWIRE, 0, 53, 4, 0) -- GPIO28
  23. ]]
  24. local function read_ds18b20(id)
  25. local tbuff = zbuff.create(10)
  26. local succ,crc8c,range,t
  27. local rbuff = zbuff.create(9)
  28. --如果有多个DS18B20,需要带上ID
  29. tbuff:write(0x55)
  30. tbuff:copy(nil, id)
  31. tbuff:write(0xb8)
  32. --如果只有1个DS18B20,就用无ID方式
  33. --tbuff:write(0xcc,0xb8)
  34. while true do
  35. tbuff[tbuff:used() - 1] = 0x44
  36. succ = onewire.tx(0, tbuff, false, true, true)
  37. if not succ then
  38. return
  39. end
  40. while true do
  41. succ = onewire.reset(0, true)
  42. if not succ then
  43. return
  44. end
  45. if onewire.bit(0) > 0 then
  46. log.info("温度转换完成")
  47. break
  48. end
  49. sys.wait(10)
  50. end
  51. tbuff[tbuff:used() - 1] = 0xbe
  52. succ = onewire.tx(0, tbuff, false, true, true)
  53. if not succ then
  54. return
  55. end
  56. succ,rx_data = onewire.rx(0, 9, nil, rbuff, false, false, false)
  57. crc8c = crypto.crc8(rbuff:toStr(0,8), 0x31, 0, true)
  58. if crc8c == rbuff[8] then
  59. range = (rbuff[4] >> 5) & 0x03
  60. -- rbuff[0] = 0xF8
  61. -- rbuff[1] = 0xFF
  62. t = rbuff:query(0,2,false,true)
  63. t = t * (5000 >> range)
  64. t = t / 10000
  65. log.info(t)
  66. else
  67. log.info("RAM DATA CRC校验不对", mcu.x32(crc8c), mcu.x32(rbuff[8]))
  68. return
  69. end
  70. sys.wait(500)
  71. end
  72. end
  73. local function test_ds18b20()
  74. local succ,rx_data
  75. local id = zbuff.create(8)
  76. local crc8c
  77. onewire.init(0)
  78. onewire.timing(0, false, 0, 500, 500, 15, 240, 70, 1, 15, 10, 2)
  79. while true do
  80. id:set() --清空id
  81. succ,rx_data = onewire.rx(0, 8, 0x33, id, false, true, true)
  82. if succ then
  83. if id[0] == 0x28 then
  84. crc8c = crypto.crc8(id:query(0,7), 0x31, 0, true)
  85. if crc8c == id[7] then
  86. log.info("探测到DS18B20", id:query(0, 7):toHex())
  87. read_ds18b20(id)
  88. log.info("DS18B20离线,重新探测")
  89. else
  90. log.info("ROM ID CRC校验不对", mcu.x32(crc8c), mcu.x32(id[7]))
  91. end
  92. else
  93. log.info("ROM ID不正确", mcu.x32(id[0]))
  94. end
  95. end
  96. log.info("没有检测到DS18B20, 5秒后重试")
  97. sys.wait(5000)
  98. end
  99. end
  100. if onewire then
  101. sys.taskInit(test_ds18b20)
  102. else
  103. log.info("no onewire")
  104. end
  105. -- 用户代码已结束---------------------------------------------
  106. -- 结尾总是这一句
  107. sys.run()
  108. -- sys.run()之后后面不要加任何语句!!!!!