BMA250.lua 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. --- 模块功能:BMA250
  2. --- Author JWL 参考C代码:https://github.com/ControlEverythingCommunity/BMA250
  3. module(..., package.seeall)
  4. local BMP_CHIPID = 0x00
  5. local BMP_VERSION = 0x01
  6. local BMP_ACC_X_LSB = 0x02
  7. local BMP_ACC_X_MSB = 0x03
  8. local BMP_ACC_Y_LSB = 0x04
  9. local BMP_ACC_Y_MSB = 0x05
  10. local BMP_ACC_Z_LSB = 0x06
  11. local BMP_ACC_Z_MSB = 0x07
  12. local BMP_GRANGE = 0x0F -- g Range
  13. local BMP_BWD = 0x10 -- Bandwidth
  14. local BMP_PM = 0x11 -- Power modes
  15. local BMP_SCR = 0x13 -- Special Control Register
  16. local BMP_RESET = 0x14 -- Soft reset register writing 0xB6 causes reset
  17. local BMP_ISR1 = 0x16 -- Interrupt settings register 1
  18. local BMP_ISR2 = 0x17 -- Interrupt settings register 2
  19. local BMP_IMR1 = 0x19 -- Interrupt mapping register 1
  20. local BMP_IMR2 = 0x1A -- Interrupt mapping register 2
  21. local BMP_IMR3 = 0x1B -- Interrupt mapping register 3
  22. local DEV_ID = 0x18
  23. local I2C_ID = 0x02
  24. local function I2C_Open(id)
  25. if i2c.setup(id, i2c.SLOW) ~= i2c.SLOW then
  26. log.error("BMA250", "I2C.init is: fail")
  27. i2c.close(id)
  28. return
  29. else
  30. log.error("BMA250", "I2C.init is: succeed")
  31. end
  32. return i2c.SLOW
  33. end
  34. local function I2C_Write_Byte_BMA250(regAddress,content)
  35. i2c.send(I2C_ID, DEV_ID, {regAddress,content})
  36. end
  37. local function I2C_Read_Bytes_BMA250(regAddress,rdcnt)
  38. i2c.send(I2C_ID, DEV_ID, regAddress)
  39. return i2c.recv(I2C_ID, DEV_ID, rdcnt)
  40. end
  41. local function I2C_Read_Byte_BMA250(regAddress)
  42. i2c.send(I2C_ID, DEV_ID, regAddress)
  43. return i2c.recv(I2C_ID, DEV_ID, 1):byte(1)
  44. end
  45. sys.taskInit(function()
  46. sys.wait(2000)
  47. I2C_Open(I2C_ID)
  48. sys.wait(500)
  49. while true do
  50. I2C_Write_Byte_BMA250(BMP_GRANGE,0X03)
  51. sys.wait(100)
  52. I2C_Write_Byte_BMA250(BMP_BWD,0X08)
  53. sys.wait(100)
  54. local data = I2C_Read_Bytes_BMA250(BMP_ACC_X_LSB,6)
  55. if data~=nil and #data==6 then
  56. _, x, y, z = pack.unpack(data, "<HHH")
  57. log.info("x, y, z", x, y, z)
  58. else
  59. log.info("BMA250","error read!!")
  60. end
  61. end
  62. end)