hmc5883l.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. --- 模块功能:ADC功能测试.
  2. -- ADC测量精度(12bit)
  3. -- 每隔1s读取一次ADC值
  4. -- @author openLuat
  5. -- @module adc.testAdc
  6. -- @license MIT
  7. -- @copyright openLuat
  8. -- @release 2018.12.19
  9. module(...,package.seeall)
  10. PROJECT = "hmc5883l_demo"
  11. VERSION = "1.0.0"
  12. require "log"
  13. require "sys"
  14. require "misc"
  15. -- i2c ID
  16. local i2cid = 2
  17. -- i2c 速率
  18. --打开vlcd电压域
  19. pmd.ldoset(15,pmd.LDO_VLCD)-- GPIO 0、1、2、3、4
  20. local addr = 0x1e --addr
  21. local speed = 100000 --iic速率
  22. local Config_a = 0x00 --配置寄存器a:设置的数据输出速率和测量配置 0 11 100 00 0X70
  23. local Config_b = 0x01 --配置寄存器b:设置装置的增益 001 00000 0Xe0
  24. local mode = 0x02 --模式寄存器: 默认单一测量模式01,连续00 000000 00 0X00/0x01
  25. local Msb_x = 0x03 --x 高位数据输出
  26. local Lsb_x = 0x04 --x 低位数据输出
  27. local Msb_y = 0x07 --x 高位数据输出
  28. local Lsb_y = 0x08 --x 低位数据输出
  29. local Msb_z = 0x05 --x 高位数据输出
  30. local Lsb_z = 0x06 --x 低位数据输出
  31. local status = 0x09 -- 状态寄存器 0x00
  32. local recogn_a = 0x0a -- 识别寄存器a 0x48
  33. local recogn_b = 0x0b -- 识别寄存器b 0x34
  34. local recogn_c = 0x0c -- 识别寄存器c 0x33
  35. --写数据
  36. local function I2C_Write_Byte(regAddress,val,val2)
  37. i2c.send(i2cid, addr, {regAddress,val,val2})
  38. end
  39. --读取单个字节
  40. local function I2C_Read_Byte(regAddress)
  41. i2c.send(i2cid, addr, regAddress)
  42. local rdstr = i2c.recv(i2cid, addr, 1)
  43. log.info("rdstr:toHex()",rdstr:toHex())
  44. return rdstr:byte(1)--变成10进制数据
  45. end
  46. --读取多个字节
  47. local function I2C_Read_Bytes(regAddress,cnt)
  48. i2c.send(i2cid, addr, regAddress)
  49. local rdstr = i2c.recv(i2cid, addr, cnt)
  50. --log.info("rdstr:toHex()-------",rdstr:toHex())
  51. return rdstr
  52. end
  53. -- 初始化
  54. function init()
  55. if i2c.setup(i2cid, speed, addr) ~= speed then
  56. log.error("i2c", "setup fail", addr)
  57. i2c.close(i2cid)
  58. return
  59. end
  60. log.info("dev i2c init_ok")
  61. return true
  62. end
  63. function hmc5883l_int()
  64. I2C_Write_Byte(Config_a,0x70) --写配置a寄存器数据
  65. I2C_Write_Byte(Config_b,0x20) --写配置b寄存器数据 增益660
  66. I2C_Write_Byte(mode,0x00) --写模式寄存器数据
  67. end
  68. function hmc5883l_read()
  69. local hx=I2C_Read_Byte(Msb_x)
  70. local lx=I2C_Read_Byte(Lsb_x)
  71. local x_data=hx*256+lx
  72. local hy=I2C_Read_Byte(Msb_y)
  73. local ly=I2C_Read_Byte(Lsb_y)
  74. local y_data=hy*256+ly
  75. local hz=I2C_Read_Byte(Msb_z)
  76. local lz=I2C_Read_Byte(Lsb_z)
  77. local z_data=hz*256+lz
  78. if(x_data>32768) then
  79. x_data= -(0xFFFF - x_data + 1)
  80. end
  81. if(y_data>32768) then
  82. y_data = -(0xFFFF - y_data + 1)
  83. end
  84. if(z_data>32768) then
  85. z_data = -(0xFFFF - z_data+ 1)
  86. end
  87. local Angle= math.atan2(y_data,x_data)*(180/3.14159265)+180;--单位:角度 (0~360)
  88. Angle= Angle
  89. log.info("x,y,z-----------", x_data,y_data,z_data )
  90. log.info("Angle",string.format("%.1f", Angle))
  91. return x_data,y_data,z_data
  92. end
  93. sys.taskInit(function()
  94. sys.wait(3000)
  95. while true do
  96. sys.wait(2000)
  97. if init() then
  98. --初始化hmc588配置
  99. hmc5883l_int()
  100. --读取x,y,z数值
  101. hmc5883l_read()
  102. i2c.close(i2cid)
  103. end
  104. end
  105. end)