main.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 如需切换到其他脚, 参考Air780EPM目录下的onewire_multi_18b20_swich_read
  19. ]]
  20. local function read_ds18b20(id)
  21. local tbuff = zbuff.create(10)
  22. local succ,crc8c,range,t
  23. local rbuff = zbuff.create(9)
  24. --如果有多个DS18B20,需要带上ID
  25. tbuff:write(0x55)
  26. tbuff:copy(nil, id)
  27. tbuff:write(0xb8)
  28. --如果只有1个DS18B20,就用无ID方式
  29. --tbuff:write(0xcc,0xb8)
  30. while true do
  31. tbuff[tbuff:used() - 1] = 0x44
  32. succ = onewire.tx(0, tbuff, false, true, true)
  33. if not succ then
  34. return
  35. end
  36. while true do
  37. succ = onewire.reset(0, true)
  38. if not succ then
  39. return
  40. end
  41. if onewire.bit(0) > 0 then
  42. log.info("温度转换完成")
  43. break
  44. end
  45. sys.wait(10)
  46. end
  47. tbuff[tbuff:used() - 1] = 0xbe
  48. succ = onewire.tx(0, tbuff, false, true, true)
  49. if not succ then
  50. return
  51. end
  52. succ,rx_data = onewire.rx(0, 9, nil, rbuff, false, false, false)
  53. crc8c = crypto.crc8(rbuff:toStr(0,8), 0x31, 0, true)
  54. if crc8c == rbuff[8] then
  55. range = (rbuff[4] >> 5) & 0x03
  56. -- rbuff[0] = 0xF8
  57. -- rbuff[1] = 0xFF
  58. t = rbuff:query(0,2,false,true)
  59. t = t * (5000 >> range)
  60. t = t / 10000
  61. log.info(t)
  62. else
  63. log.info("RAM DATA CRC校验不对", mcu.x32(crc8c), mcu.x32(rbuff[8]))
  64. return
  65. end
  66. sys.wait(500)
  67. end
  68. end
  69. local function test_ds18b20()
  70. local succ,rx_data
  71. local id = zbuff.create(8)
  72. local crc8c
  73. onewire.init(0)
  74. onewire.timing(0, false, 0, 500, 500, 15, 240, 70, 1, 15, 10, 2)
  75. while true do
  76. id:set() --清空id
  77. succ,rx_data = onewire.rx(0, 8, 0x33, id, false, true, true)
  78. if succ then
  79. if id[0] == 0x28 then
  80. crc8c = crypto.crc8(id:query(0,7), 0x31, 0, true)
  81. if crc8c == id[7] then
  82. log.info("探测到DS18B20", id:query(0, 7):toHex())
  83. read_ds18b20(id)
  84. log.info("DS18B20离线,重新探测")
  85. else
  86. log.info("ROM ID CRC校验不对", mcu.x32(crc8c), mcu.x32(id[7]))
  87. end
  88. else
  89. log.info("ROM ID不正确", mcu.x32(id[0]))
  90. end
  91. end
  92. log.info("没有检测到DS18B20, 5秒后重试")
  93. sys.wait(5000)
  94. end
  95. end
  96. if onewire then
  97. sys.taskInit(test_ds18b20)
  98. else
  99. log.info("no onewire")
  100. end
  101. -- 用户代码已结束---------------------------------------------
  102. -- 结尾总是这一句
  103. sys.run()
  104. -- sys.run()之后后面不要加任何语句!!!!!