HX711.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. --- 模块功能:CCS811
  2. --- Author JWL
  3. module(..., package.seeall)
  4. require "pins"
  5. local bverify =true
  6. local GapValue = 430
  7. local Weight_Total =nil
  8. local Weight_Shiwu =nil
  9. local Weight_Maopi =0
  10. local HX711_SCK = pins.setup(pio.P0_3,0)
  11. local HX711_DOUT=nil
  12. local dummy_cnt=0
  13. local function Delay__hx711_us(cnt)
  14. cnt =cnt or 1
  15. for i=1,cnt do
  16. dummy_cnt = dummy_cnt+1
  17. end
  18. end
  19. --this function can only be read by TASK!!
  20. function HX711_Read()
  21. local count=0
  22. HX711_DOUT = pins.setup(pio.P0_1,1) --配置为输出
  23. Delay__hx711_us(100)
  24. HX711_SCK(0)
  25. HX711_DOUT = pins.setup(pio.P0_1, function() end, pio.NOPULL) --配置为输入
  26. while HX711_DOUT() ==1 do end
  27. local tb={}
  28. for i=1,24 do
  29. tb[i]=0
  30. end
  31. for i=1,24 do
  32. HX711_SCK(1)
  33. HX711_SCK(0)
  34. tb[i]=HX711_DOUT()
  35. end
  36. if "1111111" == table.concat(tb,"",18) then --最后7位不能全部为1
  37. return 0
  38. end
  39. for i=1,24 do
  40. count = bit.lshift(count,1)
  41. if tb[i] ==1 then count = count+ 1 end
  42. end
  43. HX711_SCK(1)
  44. count = bit.bxor(count,0x800000)
  45. Delay__hx711_us()
  46. HX711_SCK(0)
  47. return count
  48. end
  49. --此函数只能被任务调用!!!
  50. local function Get_Maopi()
  51. log.info("Weight_Maopi =", "want to get")
  52. while Weight_Maopi == 0 do
  53. Weight_Maopi = HX711_Read()
  54. sys.wait(200)
  55. end
  56. log.info("Weight_Maopi =", Weight_Maopi)
  57. end
  58. --此函数只能被任务调用!!!
  59. function Get_Weight()
  60. Weight_Total = HX711_Read()
  61. if Weight_Total >0 then
  62. Weight_Shiwu = Weight_Total - Weight_Maopi;
  63. log.info("totalweight =", Weight_Total, "Maopi=", Weight_Maopi, " delta=",Weight_Shiwu,"Weight_Shiwu" , "----> ", Weight_Shiwu/GapValue)
  64. else
  65. log.info("[ALM]","Weight_Shiwu==0")
  66. end
  67. end
  68. sys.taskInit(function()
  69. pmd.ldoset(15, pmd.LDO_VLCD)
  70. local times=0
  71. sys.wait(2000)
  72. while true do
  73. if bverify then
  74. Get_Maopi()
  75. bverify=false
  76. end
  77. Get_Weight()
  78. sys.wait(100)
  79. end
  80. end)
  81. --测试仓库所有权变更
  82. local keyName = {
  83. [0] = {},
  84. [255] = {
  85. [255] = "PWRKEY"
  86. }
  87. }
  88. local function keyMsg(msg)
  89. log.warn("key",msg.key_matrix_row,msg.key_matrix_col)
  90. if keyName[msg.key_matrix_row][msg.key_matrix_col] == "PWRKEY" then
  91. if msg.pressed then
  92. --短按开机键校准
  93. bverify =true
  94. end
  95. end
  96. end
  97. --注册按键消息的处理函数
  98. rtos.on(rtos.MSG_KEYPAD, keyMsg)
  99. --初始化键盘阵列
  100. rtos.init_module(rtos.MOD_KEYPAD, 0, 0x1F, 0x1F)
  101. pio.pin.setdebounce(10)--不要调用这个,否则影响GPIO 读取