8 uint8_t chipid_temp
= 0x00;
9 uint8_t stk8xxx_pid_list
[PID_SIZE
] = {STK8xxx_CHIPID_VAL
, STK8BA50_X_CHIPID_VAL
, STK8BA5X_CHIPID_VAL
, STK8327_CHIPID_VAL
};
11 void STK8xxx::ReadAccRegister(uint8_t reg
, uint8_t *data
)
13 Wire
.beginTransmission(STK8xxx_SLAVE_ADDRESS
);
15 Wire
.endTransmission();
17 Wire
.requestFrom(STK8xxx_SLAVE_ADDRESS
, 1); // request 1 bytes from slave device
18 *data
= Wire
.read(); // receive a byte
21 void STK8xxx::WriteAccRegister(uint8_t reg
, uint8_t data
)
23 Wire
.beginTransmission(STK8xxx_SLAVE_ADDRESS
);
26 Wire
.endTransmission();
30 * Anymotion init register
31 * Must be placed after the software reset
34 void STK8xxx::STK8xxx_Anymotion_init()
36 unsigned char ARegAddr
, ARegWriteValue
;
38 /* Enable X Y Z-axis any-motion (slope) interrupt */
39 ARegAddr
= STK8xxx_REG_INTEN1
;
40 ARegWriteValue
= STK8xxx_VAL_SLP_EN_X
| STK8xxx_VAL_SLP_EN_Y
| STK8xxx_VAL_SLP_EN_Z
;
41 WriteAccRegister(ARegAddr
, ARegWriteValue
);
43 /* Set anymotion Interrupt trigger threshold */
44 ARegAddr
= STK8xxx_REG_SLOPETHD
;
45 ARegWriteValue
= STK8xxx_VAL_SLP_DFLT
;
46 WriteAccRegister(ARegAddr
, ARegWriteValue
);
48 /* Enable any-motion */
49 ARegAddr
= STK8xxx_REG_SIGMOT2
;
50 ARegWriteValue
= STK8xxx_VAL_ANY_MOT_EN
;
51 WriteAccRegister(ARegAddr
, ARegWriteValue
);
53 /* Map any-motion (slope) interrupt to INT1 */
54 ARegAddr
= STK8xxx_REG_INTMAP1
;
55 ARegWriteValue
= STK8xxx_VAL_ANYMOT2INT1
;
56 WriteAccRegister(ARegAddr
, ARegWriteValue
);
60 * Sigmotion init register
61 * Must be placed after the software reset
64 void STK8xxx::STK8xxx_Sigmotion_init()
66 unsigned char SRegAddr
, SRegWriteValue
;
68 /* Enable X Y Z-axis sig-motion (slope) interrupt */
69 SRegAddr
= STK8xxx_REG_INTEN1
;
70 SRegWriteValue
= STK8xxx_VAL_SLP_EN_X
| STK8xxx_VAL_SLP_EN_Y
| STK8xxx_VAL_SLP_EN_Z
;
71 WriteAccRegister(SRegAddr
, SRegWriteValue
);
73 /* Set sig-motion Interrupt trigger threshold */
74 SRegAddr
= STK8xxx_REG_SLOPETHD
;
75 SRegWriteValue
= STK8xxx_VAL_SLP_DFLT
;
76 WriteAccRegister(SRegAddr
, SRegWriteValue
);
78 /* Enable significant motion */
79 SRegAddr
= STK8xxx_REG_SIGMOT2
;
80 SRegWriteValue
= STK8xxx_VAL_SIG_MOT_EN
;
81 WriteAccRegister(SRegAddr
, SRegWriteValue
);
83 /* Map significant motion interrupt to INT1 */
84 SRegAddr
= STK8xxx_REG_INTMAP1
;
85 SRegWriteValue
= STK8xxx_VAL_SIGMOT2INT1
;
86 WriteAccRegister(SRegAddr
, SRegWriteValue
);
90 * Diable motion register
91 * Must be placed after the software reset
94 void STK8xxx::STK8xxx_Disable_Motion()
96 unsigned char ARegAddr
, ARegWriteValue
;
98 /* Disable X Y Z-axis motion (slope) interrupt */
99 ARegAddr
= STK8xxx_REG_INTEN1
;
100 ARegWriteValue
= 0x00;
101 WriteAccRegister(ARegAddr
, ARegWriteValue
);
104 ARegAddr
= STK8xxx_REG_SIGMOT2
;
105 ARegWriteValue
= 0x00;
106 WriteAccRegister(ARegAddr
, ARegWriteValue
);
110 /* Disable Gsensor */
111 void STK8xxx::STK8xxx_Suspend_mode()
113 unsigned char RegAddr
, RegWriteValue
;
115 /* suspend mode enable */
116 RegAddr
= STK8xxx_REG_POWMODE
;
117 RegWriteValue
= STK8xxx_VAL_SUSPEND
;
118 WriteAccRegister(RegAddr
, RegWriteValue
);
121 bool STK8xxx::STK8xxx_Check_chipid()
123 uint8_t RegAddr
= STK_REG_CHIPID
;
124 int i
= 0, pid_num
= (sizeof(stk8xxx_pid_list
) / sizeof(stk8xxx_pid_list
[0]));
126 ReadAccRegister(RegAddr
, &chipid_temp
);
127 for (i
= 0; i
< pid_num
; i
++)
129 if (chipid_temp
== stk8xxx_pid_list
[i
])
131 DBGLN("read stkchip id ok, chip_id = 0x%x", chipid_temp
);
135 ERRLN("read stkchip id fail!");
140 * Initializes an example function
141 * The initial configuration is in active mode.
142 * STK8xxx is in the gear of ODR=2000Hz, and the range is set to +/-4G
145 int STK8xxx::STK8xxx_Initialization()
147 unsigned char RegAddr
, RegWriteValue
;
149 if(!STK8xxx_Check_chipid())
155 RegAddr
= STK8xxx_REG_SWRST
;
156 RegWriteValue
= STK8xxx_VAL_RST_DFLTS
;
157 WriteAccRegister(RegAddr
, RegWriteValue
);
160 /* set range, resolution */
161 RegAddr
= STK8xxx_REG_RANGESEL
;
162 RegWriteValue
= STK8xxx_RANGE_4G
; // range = +/-4g
163 WriteAccRegister(RegAddr
, RegWriteValue
);
166 RegAddr
= STK8xxx_REG_POWMODE
;
167 RegWriteValue
= STK8xxx_VAL_SLEEP_05
; // active mode
168 WriteAccRegister(RegAddr
, RegWriteValue
);
171 RegAddr
= STK8xxx_REG_BWSEL
;
172 RegWriteValue
= STK8xxx_VAL_BW_1000
; // bandwidth = 1000Hz
173 WriteAccRegister(RegAddr
, RegWriteValue
);
175 //STK8xxx_Anymotion_init();
176 //STK8xxx_Sigmotion_init();
178 /* set i2c watch dog */
179 RegAddr
= STK8xxx_REG_INTFCFG
;
180 RegWriteValue
= STK8xxx_VAL_I2C_WDT_EN
; // enable watch dog
181 WriteAccRegister(RegAddr
, RegWriteValue
);
184 RegAddr
= STK8xxx_REG_INTCFG1
;
185 RegWriteValue
= STK8xxx_VAL_INT_LV
; // INT1/INT2 push-pull, active high
186 WriteAccRegister(RegAddr
, RegWriteValue
);
191 int STK8xxx::STK8xxx_Get_Sensitivity()
194 if(0x86 == chipid_temp
)
196 //resolution = 10 bit
197 sensitivity
= 1 << 9;
201 //resolution = 12 bit
202 sensitivity
= 1 << 11;
205 sensitivity
= sensitivity
/ 4;
209 /* Read data from registers */
210 void STK8xxx::STK8xxx_Getregister_data(float *X_DataOut
, float *Y_DataOut
, float *Z_DataOut
)
212 uint8_t RegAddr
, RegReadValue
[2];
215 RegAddr
= STK8xxx_REG_XOUT1
;
216 RegReadValue
[0] = 0x00;
217 ReadAccRegister(RegAddr
, &RegReadValue
[0]);
218 RegAddr
= STK8xxx_REG_XOUT2
;
219 RegReadValue
[1] = 0x00;
220 ReadAccRegister(RegAddr
, &RegReadValue
[1]);
221 x
= (short int)(RegReadValue
[1] << 8 | RegReadValue
[0]);
223 RegAddr
= STK8xxx_REG_YOUT1
;
224 RegReadValue
[0] = 0x00;
225 ReadAccRegister(RegAddr
, &RegReadValue
[0]);
226 RegAddr
= STK8xxx_REG_YOUT2
;
227 RegReadValue
[1] = 0x00;
228 ReadAccRegister(RegAddr
, &RegReadValue
[1]);
229 y
= (short int)(RegReadValue
[1] << 8 | RegReadValue
[0]);
231 RegAddr
= STK8xxx_REG_ZOUT1
;
232 RegReadValue
[0] = 0x00;
233 ReadAccRegister(RegAddr
, &RegReadValue
[0]);
234 RegAddr
= STK8xxx_REG_ZOUT2
;
235 RegReadValue
[1] = 0x00;
236 ReadAccRegister(RegAddr
, &RegReadValue
[1]);
237 z
= (short int)(RegReadValue
[1] << 8 | RegReadValue
[0]);
239 if(0x86 == chipid_temp
)
241 //resolution = 10 bit
243 *X_DataOut
= (float) x
/ STK8xxx_Get_Sensitivity();
246 *Y_DataOut
= (float) y
/ STK8xxx_Get_Sensitivity();
249 *Z_DataOut
= (float) z
/ STK8xxx_Get_Sensitivity();
253 //resolution = 12 bit
255 *X_DataOut
= (float) x
/ STK8xxx_Get_Sensitivity();
258 *Y_DataOut
= (float) y
/ STK8xxx_Get_Sensitivity();
261 *Z_DataOut
= (float) z
/ STK8xxx_Get_Sensitivity();