1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| local i2cslaveaddr = 0x68 local i2cid = 2 if i2c.setup(i2cid,i2c.SLOW) ~= i2c.SLOW then log.error("testI2c.init","fail") return end sys.taskInit(function () i2c.send(i2cid,i2cslaveaddr,{0X6b,0x80}) sys.wait(100) i2c.send(i2cid,i2cslaveaddr,{0X6b,0x00}) sys.wait(100) i2c.send(i2cid,i2cslaveaddr,{0x1b,0x80}) i2c.send(i2cid,i2cslaveaddr,{0x1c,0x00}) i2c.send(i2cid,i2cslaveaddr,{0x19,19}) i2c.send(i2cid,i2cslaveaddr,{0x38,0x00}) i2c.send(i2cid,i2cslaveaddr,{0x6a,0x00}) i2c.send(i2cid,i2cslaveaddr,{0x23,0x00}) i2c.send(i2cid,i2cslaveaddr,{0x37,0x80}) i2c.send(i2cid,i2cslaveaddr,0x75) local revData = i2c.recv(i2cid,i2cslaveaddr,1) log.info("i2c read",revData:toHex()) if revData:byte() == 0x68 then i2c.send(i2cid,i2cslaveaddr,{0x6b,0x01}) i2c.send(i2cid,i2cslaveaddr,{0x6c,0x00}) else log.info("i2c","address not right") return end function getTrueData(d) i2c.send(i2cid,i2cslaveaddr,d) local s = i2c.recv(i2cid,i2cslaveaddr,2) return s:byte()*256 + s:byte(2) end while true do sys.wait(50) local gx = getTrueData(0x43)/131 local gy = getTrueData(0x44)/131 local gz = getTrueData(0x45)/131 log.info("mpu6050.Gyroscope",gx,gy,gz) local ax = getTrueData(0x3b)/16 local ay = getTrueData(0x3c)/16 local az = getTrueData(0x3d)/16 log.info("mpu6050.Accelerometer",ax,ay,az) end end)
|