3 @summary l3g4200d 三轴数字陀螺仪传感器
6 @author Dozingfiretruck
8 --注意:因使用了sys.wait()所有api需要在协程中使用
10 local l3g4200d = require "l3g4200d"
13 sys.taskInit(function()
14 i2c.setup(i2cid,i2c_speed)
15 l3g4200d.init(i2cid)--初始化,传入i2c_id
17 local l3g4200d_data = l3g4200d.get_data()
18 log.info("l3g4200d_data", l3g4200d_data.x,l3g4200d_data.y,l3g4200d_data.z)
26 local sys
= require
"sys"
29 local L3G4200D_ADDRESS_ADR
31 local L3G4200D_ADDRESS_ADR_LOW
= 0x68
32 local L3G4200D_ADDRESS_ADR_HIGH
= 0x69
34 local L3G4200D_CHIP_ID_CHECK
= 0x0F
35 local L3G4200D_CHIP_ID
= 0xD3
39 local L3G4200D_CTRL_REG1
= 0x20
40 local L3G4200D_CTRL_REG2
= 0x21
41 local L3G4200D_CTRL_REG3
= 0x22
42 local L3G4200D_CTRL_REG4
= 0x23
43 local L3G4200D_CTRL_REG5
= 0x24
44 local L3G4200D_REFERENCE
= 0x25
45 local L3G4200D_OUT_TEMP
= 0x26
46 local L3G4200D_STATUS_REG
= 0x27
47 local L3G4200D_OUT_X_L
= 0x28
48 local L3G4200D_OUT_X_H
= 0x29
49 local L3G4200D_OUT_Y_L
= 0x2A
50 local L3G4200D_OUT_Y_H
= 0x2B
51 local L3G4200D_OUT_Z_L
= 0x2C
52 local L3G4200D_OUT_Z_H
= 0x2D
53 local L3G4200D_FIFO_CTRL_REG
= 0x2E
54 local L3G4200D_FIFO_SRC_REG
= 0x2F
55 local L3G4200D_INT1_CFG
= 0x30
56 local L3G4200D_INT1_SRC
= 0x31
57 local L3G4200D_INT1_TSH_XH
= 0x32
58 local L3G4200D_INT1_TSH_XL
= 0x33
59 local L3G4200D_INT1_TSH_YH
= 0x34
60 local L3G4200D_INT1_TSH_YL
= 0x35
61 local L3G4200D_INT1_TSH_ZH
= 0x36
62 local L3G4200D_INT1_TSH_ZL
= 0x37
63 local L3G4200D_INT1_DURATION
= 0x38
66 local function chip_check()
67 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR_HIGH
, L3G4200D_CHIP_ID_CHECK
)--读器件地址
68 local revData
= i2c
.recv(i2cid
, L3G4200D_ADDRESS_ADR_HIGH
, 1)
69 if revData
:byte() ~= nil then
70 L3G4200D_ADDRESS_ADR
= L3G4200D_ADDRESS_ADR_HIGH
72 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR_LOW
, L3G4200D_CHIP_ID_CHECK
)--读器件地址
74 local revData
= i2c
.recv(i2cid
, L3G4200D_ADDRESS_ADR_LOW
, 1)
75 if revData
:byte() ~= nil then
76 L3G4200D_ADDRESS_ADR
= L3G4200D_ADDRESS_ADR_LOW
78 log.info("Can't find L3G4200D device")
83 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR
, L3G4200D_CHIP_ID_CHECK
)--读器件地址
84 local revData
= i2c
.recv(i2cid
, L3G4200D_ADDRESS_ADR
, 1)
85 if revData
:byte() == L3G4200D_CHIP_ID
then
86 log.info("Device i2c id is: L3G4200D")
89 log.info("Can't find L3G4200D device")
96 @api l3g4200d.init(i2c_id)
102 function l3g4200d
.init(i2c_id
)
105 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR
, {L3G4200D_CTRL_REG1
,0x0F})
106 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR
, {L3G4200D_CTRL_REG2
,0x00})
107 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR
, {L3G4200D_CTRL_REG3
,0x08})
108 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR
, {L3G4200D_CTRL_REG4
,0x30})
109 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR
, {L3G4200D_CTRL_REG5
,0x00})
116 @api l3g4200d.get_data()
117 @return table l3g4200d 数据
119 local l3g4200d_data = l3g4200d.get_data()
120 log.info("l3g4200d_data", l3g4200d_data.x,l3g4200d_data.y,l3g4200d_data.z)
122 function l3g4200d
.get_data()
123 local l3g4200d_data
= {}
124 i2c
.send(i2cid
, L3G4200D_ADDRESS_ADR
,L3G4200D_OUT_X_L
)
125 local data
= i2c
.recv(i2cid
, L3G4200D_ADDRESS_ADR
, 6)
126 _
, l3g4200d_data
.x
, l3g4200d_data
.y
, l3g4200d_data
.z
= pack
.unpack(data
, "<h3")