HDC1000.lua 1000 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. --- 模块功能:HDC1000功能测试.
  2. -- @module i2c
  3. -- @author openLuat
  4. -- @license MIT
  5. -- @copyright OpenLuat.com
  6. -- @release 2021.9.9
  7. require "sys"
  8. -- 打开 I2C 地址为7位地址 0x40
  9. local function OPEN(id)
  10. if i2c.setup(id, i2c.SLOW, 0x40) ~= i2c.SLOW then
  11. log.error("i2c.init is: ", "fail")
  12. end
  13. end
  14. -- 发送指令
  15. function SEND(id, addr, data)
  16. i2c.send(id, addr, data)
  17. sys.wait(80)
  18. end
  19. sys.taskInit(function ()
  20. sys.wait(5000)
  21. -- I2C 通道为 2
  22. local id = 2
  23. OPEN(id)
  24. SEND(id, 0x40, {0x02, 0x1000})
  25. while true do
  26. -- 读取温度
  27. SEND(2, 0x40, 0x01)
  28. SEND(2, 0x40, 0x00)
  29. local temp = tonumber(i2c.recv(id, 0x40, 2):toHex(), 16)
  30. log.info("温度", temp / 65536 * 165 - 40)
  31. -- 读取湿度
  32. SEND(2, 0x40, 0x00)
  33. SEND(2, 0x40, 0x00)
  34. local humi = tonumber(i2c.recv(id, 0x40, 2):toHex(), 16)
  35. log.info("湿度", humi / 65536 * 100)
  36. sys.wait(1000)
  37. end
  38. end)