forked from n0m1/MMA8453_n0m1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMA8452_ahref.cpp
451 lines (382 loc) · 12.4 KB
/
MMA8452_ahref.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/************************************************************************************
*
* Name : MMA8452_ahref Library
* Author : Noah Shibley, Michael Grant, NoMi Design Ltd. http://n0m1.com, Richard Fox
* Date : Feb 10th 2012
* Version : 0.1
* Notes : Arduino Library for use with the Freescale MMA8452Q via i2c.
Some of the lib source from Kerry D. Wong
http://www.kerrywong.com/2012/01/09/interfacing-MMA8452Q-with-arduino/
Edited xyz method uses process from Nathan Seidle's MMA8452Q code
https://github.com/sparkfun/MMA8452_Accelerometer
Uses MMA8453_n0m1 as a base: https://github.com/n0m1/MMA8453_n0m1
*
*
* This file is part of MMA8452_ahref.
*
* MMA8452_ahref is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AtTouch is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MMA8452_ahref. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************************************/
#include "MMA8452_ahref.h"
MMA8452_ahref* MMA8452_ahref::pMMA8452_ahref = 0;
MMA8452_ahref::MMA8452_ahref()
{
pMMA8452_ahref = this;
dataMode_ = false;
shakeMode_ = false;
ISRFlag = false;
shake_ = false;
shakeAxisX_ = false;
shakeAxisY_ = false;
shakeAxisZ_ = false;
I2CAddr = 0x1c; //The i2C address of the MMA8453 chip. 0x1D is another common value.
gScaleRange_ = 2; //default 2g
}
/***********************************************************
*
* setI2CAddr
*
*
*
***********************************************************/
void MMA8452_ahref::setI2CAddr(int address)
{
I2CAddr = address; I2CAddr;
}
/***********************************************************
*
* update
*
*
*
***********************************************************/
void MMA8452_ahref::update()
{
if(dataMode_)
{
xyz(x_,y_,z_);
}
if(shakeMode_ == true || motionMode_ == true)
{
clearInterrupt();
}
}
/***********************************************************
*
* clearInterrupt
*
*
*
***********************************************************/
void MMA8452_ahref::clearInterrupt()
{
if(ISRFlag)
{
byte sourceSystem;
I2c.read(I2CAddr,REG_INT_SOURCE,byte(1),&sourceSystem); //check which system fired the interrupt
if((sourceSystem&0x20) == 0x20) //Transient
{
//Perform an Action since Transient Flag has been set
//Read the Transient to clear system interrupt and Transient
byte srcTrans;
shake_ = true;
I2c.read(I2CAddr,REG_TRANSIENT_SRC ,byte(1),&srcTrans);
if(srcTrans&0x02 == 0x02)
{
shakeAxisX_ = true;
}
if(srcTrans&0x08 == 0x08)
{
shakeAxisY_ = true;
}
if(srcTrans&0x20 == 0x20)
{
shakeAxisZ_ = true;
}
}
if((sourceSystem&0x04) == 0x04) //FreeFall Motion
{
byte srcFF;
I2c.read(I2CAddr,REG_FF_MT_SRC ,byte(1),&srcFF);
motion_ = true;
}
ISRFlag = false;
}
}
/*************************************************************
*
* xyz
*
* Get accelerometer readings (x, y, z)
* by default, standard 12 bits mode is used.
*
* If accelerometer is initialized to use low res mode,
* isHighRes must be passed in as false.
*
*************************************************************/
void MMA8452_ahref::xyz(int& x, int& y, int& z)
{
byte buf[6];
if (highRes_)
{
I2c.read(I2CAddr, REG_OUT_X_MSB, 6, buf);
x = ((buf[0] << 8) | buf[1]);
y = ((buf[2] << 8) | buf[3]);
z = ((buf[4] << 8) | buf[5]);
x >>= 4;
y >>= 4;
z >>= 4;
if(buf[0] > 0x7F) {
x = ~x + 1;
x *= -1;
}
if(buf[2] > 0x7F) {
y = ~y + 1;
y *= -1;
}
if(buf[4] > 0x7F) {
z = ~z + 1;
z *= -1;
}
}
else
{
//I dont know if this is still correct I just guessed
I2c.read(I2CAddr, REG_OUT_X_MSB, 3, buf);
x = buf[0] << 8;
y = buf[1] << 8;
z = buf[2] << 8;
}
}
/*************************************************************
* x_f
* Get the x axis value in G's
*
*************************************************************/
float MMA8452_ahref::x_f(){
return (float) x_ / (4096/(2*gScaleInt_));
}
/*************************************************************
* y_f
* Get the y axis value in G's
*
*************************************************************/
float MMA8452_ahref::y_f(){
return (float) y_ / (4096/(2*gScaleInt_));
}
/*************************************************************
* z_f
* Get the z axis value in G's
*
*************************************************************/
float MMA8452_ahref::z_f(){
return (float) z_ / (4096/(2*gScaleInt_));
}
/***********************************************************
*
* dataMode
*
*
*
***********************************************************/
void MMA8452_ahref::dataMode(boolean highRes, int gScaleRange)
{
highRes_ = highRes;
gScaleRange_ = gScaleRange;
gScaleInt_ = gScaleRange;
dataMode_ = true;
byte statusCheck;
byte activeMask = 0x01;
byte resModeMask = 0x02;
//setup i2c
I2c.begin();
//register settings must be made in standby mode
I2c.read(I2CAddr,REG_CTRL_REG1,byte(1),&statusCheck);
I2c.write(I2CAddr, REG_CTRL_REG1, byte(statusCheck & ~activeMask));
if( gScaleRange_ <= 3){ gScaleRange_ = FULL_SCALE_RANGE_2g; } //0-3 = 2g
else if( gScaleRange_ <= 5){ gScaleRange_ = FULL_SCALE_RANGE_4g; } //4-5 = 4g
else if( gScaleRange_ <= 8){ gScaleRange_ = FULL_SCALE_RANGE_8g; }// 6-8 = 8g
else if( gScaleRange_ > 8) { gScaleRange_ = FULL_SCALE_RANGE_8g; } //boundary
I2c.write(I2CAddr,REG_XYZ_DATA_CFG, byte(gScaleRange_));
//set highres 10bit or lowres 8bit
I2c.read(I2CAddr,REG_CTRL_REG1,byte(1),&statusCheck);
if(highRes){
I2c.write(I2CAddr, REG_CTRL_REG1, byte(statusCheck & ~resModeMask));
}
else {
I2c.write(I2CAddr, REG_CTRL_REG1, byte(statusCheck | resModeMask));
}
//active Mode
I2c.read(I2CAddr,REG_CTRL_REG1,byte(1),&statusCheck);
I2c.write(I2CAddr, REG_CTRL_REG1, byte(statusCheck | activeMask));
}
/***********************************************************
*
* shakeMode
*
*
*
***********************************************************/
void MMA8452_ahref::shakeMode(int threshold, boolean enableX, boolean enableY, boolean enableZ, boolean enableINT2,int arduinoINTPin)
{
if(arduinoINTPin == 2 || arduinoINTPin == 3)
{
arduinoINTPin = arduinoINTPin - 2;
attachInterrupt(arduinoINTPin,accelISR,FALLING);
//DataSheet pg40, When IPOL is ‘0’ (default value) any interrupt event will signaled with a logical 0
}
else
{
#ifdef PINCHANGE_INT
pinMode(arduinoINTPin, INPUT); digitalWrite(arduinoINTPin, HIGH);
PCintPort::attachInterrupt(arduinoINTPin,accelISR,FALLING);
#else
Serial.println("no INT on pin, define PINCHANGE_INT");
#endif
}
boolean error = false;
byte statusCheck;
//setup i2c
I2c.begin();
I2c.write(I2CAddr, REG_CTRL_REG1, byte(0x18)); //Set device in 100 Hz ODR, Standby
byte xyzCfg = 0x10; //latch always enabled
if(enableX) xyzCfg |= 0x02;
if(enableY) xyzCfg |= 0x04;
if(enableZ) xyzCfg |= 0x08;
I2c.write(I2CAddr, REG_TRANSIENT_CFG, xyzCfg); //XYZ + latch 0x1E
I2c.read(I2CAddr, REG_TRANSIENT_CFG, byte(1), &statusCheck);
if(statusCheck != xyzCfg) error = true;
if(threshold > 127) threshold = 127; //8g is the max.
I2c.write(I2CAddr, REG_TRANSIENT_THS, byte(threshold)); //threshold
I2c.read(I2CAddr, REG_TRANSIENT_THS, byte(1), &statusCheck);
if(statusCheck != byte(threshold)) error = true;
I2c.write(I2CAddr, REG_TRANSIENT_COUNT, byte(0x05)); //Set the Debounce Counter for 50 ms
I2c.read(I2CAddr,REG_TRANSIENT_COUNT, byte(1), &statusCheck);
if(statusCheck != 0x05) error = true;
I2c.read(I2CAddr, REG_CTRL_REG4, byte(1), &statusCheck);
statusCheck |= 0x20;
I2c.write(I2CAddr, REG_CTRL_REG4, statusCheck); //Enable Transient Detection Interrupt in the System
byte intSelect = 0x20;
if(enableINT2) intSelect = 0x00;
I2c.read(I2CAddr, REG_CTRL_REG5, byte(1), &statusCheck);
statusCheck |= intSelect;
I2c.write(I2CAddr, REG_CTRL_REG5, statusCheck); //INT2 0x0, INT1 0x20
I2c.read(I2CAddr, REG_CTRL_REG1, byte(1), &statusCheck); //Read out the contents of the register
statusCheck |= 0x01; //Change the value in the register to Active Mode.
I2c.write(I2CAddr, REG_CTRL_REG1, statusCheck); //Write in the updated value to put the device in Active Mode
if(error)
{
Serial.println("Shake mode setup error");
Serial.println("retrying...");
delay(100);
shakeMode(threshold,enableX,enableY,enableZ,enableINT2,arduinoINTPin);
}
shakeMode_ = true;
}
/***********************************************************
*
* motionMode
*
*
*
***********************************************************/
void MMA8452_ahref::motionMode(int threshold, boolean enableX, boolean enableY, boolean enableZ, boolean enableINT2,int arduinoINTPin)
{
if(arduinoINTPin == 2 || arduinoINTPin == 3)
{
arduinoINTPin = arduinoINTPin - 2;
attachInterrupt(arduinoINTPin,accelISR,FALLING);
//DataSheet pg40, When IPOL is ‘0’ (default value) any interrupt event will signaled with a logical 0
}
else
{
#ifdef PINCHANGE_INT
pinMode(arduinoINTPin, INPUT); digitalWrite(arduinoINTPin, HIGH);
PCintPort::attachInterrupt(arduinoINTPin,accelISR,FALLING);
#else
Serial.println("no INT on pin, define PINCHANGE_INT");
#endif
}
boolean error = false;
byte statusCheck;
//setup i2c
I2c.begin();
I2c.write(I2CAddr, REG_CTRL_REG1, byte(0x18)); //Set device in 100 Hz ODR, Standby
byte xyzCfg = 0x80; //latch always enabled
xyzCfg |= 0x40; //Motion not free fall
if(enableX) xyzCfg |= 0x08;
if(enableY) xyzCfg |= 0x10;
if(enableZ) xyzCfg |= 0x20;
I2c.write(I2CAddr, REG_FF_MT_CFG, xyzCfg); //XYZ + latch + motion
I2c.read(I2CAddr, REG_FF_MT_CFG, byte(1), &statusCheck);
if(statusCheck != xyzCfg) error = true;
if(threshold > 127) threshold = 127; //a range of 0-127.
I2c.write(I2CAddr, REG_FF_MT_THS, byte(threshold)); //threshold
I2c.read(I2CAddr, REG_FF_MT_THS, byte(1), &statusCheck);
if(statusCheck != byte(threshold)) error = true;
I2c.write(I2CAddr, REG_FF_MT_COUNT, byte(0x0A)); //Set the Debounce Counter for 100 ms
I2c.read(I2CAddr,REG_FF_MT_COUNT, byte(1), &statusCheck);
if(statusCheck != 0x0A) error = true;
I2c.read(I2CAddr, REG_CTRL_REG4, byte(1), &statusCheck);
statusCheck |= 0x04;
I2c.write(I2CAddr, REG_CTRL_REG4, statusCheck); //Enable Motion Interrupt in the System
byte intSelect = 0x04;
if(enableINT2) intSelect = 0x00;
I2c.read(I2CAddr, REG_CTRL_REG5, byte(1), &statusCheck);
statusCheck |= intSelect;
I2c.write(I2CAddr, REG_CTRL_REG5, statusCheck); //INT2 0x0, INT1 0x04
I2c.read(I2CAddr, REG_CTRL_REG1, byte(1), &statusCheck); //Read out the contents of the register
statusCheck |= 0x01; //Change the value in the register to Active Mode.
I2c.write(I2CAddr, REG_CTRL_REG1, statusCheck); //Write in the updated value to put the device in Active Mode
if(error)
{
Serial.println("Motion mode setup error");
Serial.println("retrying...");
delay(100);
motionMode(threshold,enableX,enableY,enableZ,enableINT2,arduinoINTPin);
}
motionMode_ = true;
}
/***********************************************************
*
* regRead
*
*
*
***********************************************************/
void MMA8452_ahref::regRead(byte reg, byte *buf, byte count)
{
I2c.read(I2CAddr, reg, count, buf);
}
/***********************************************************
*
* regWrite
*
*
*
***********************************************************/
void MMA8452_ahref::regWrite(byte reg, byte val)
{
I2c.write(I2CAddr, reg, val);
}
/***********************************************************
*
* accelISR
*
*
*
***********************************************************/
void accelISR(void){
MMA8452_ahref::pMMA8452_ahref->ISRFlag = true;
}