1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
25
26
29
30
33
34
37
38
41
42
45
46
49
50
53
54
57
58
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
81
82
85
86
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
112
115
116
121
122
123
124
125
126
127
128
129
130
131
132
133
134
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
228
235
236
237
238
253
254
255
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
546
547
550
551
554
555
/* ... */
#include "lsm303dlhc.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
ACCELERO_DrvTypeDef Lsm303dlhcDrv =
{
LSM303DLHC_AccInit,
LSM303DLHC_AccDeInit,
LSM303DLHC_AccReadID,
LSM303DLHC_AccRebootCmd,
0,
LSM303DLHC_AccZClickITConfig,
0,
0,
0,
0,
LSM303DLHC_AccFilterConfig,
LSM303DLHC_AccFilterCmd,
LSM303DLHC_AccReadXYZ
...};
/* ... */
/* ... */
/* ... */
void LSM303DLHC_AccInit(uint16_t InitStruct)
{
uint8_t ctrl = 0x00;
COMPASSACCELERO_IO_Init();
ctrl = (uint8_t) InitStruct;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG1_A, ctrl);
ctrl = (uint8_t) (InitStruct >> 8);
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A, ctrl);
}{ ... }
/* ... */
void LSM303DLHC_AccDeInit(void)
{
}{ ... }
/* ... */
uint8_t LSM303DLHC_AccReadID(void)
{
uint8_t ctrl = 0x00;
COMPASSACCELERO_IO_Init();
ctrl = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_WHO_AM_I_ADDR);
return ctrl;
}{ ... }
/* ... */
void LSM303DLHC_AccRebootCmd(void)
{
uint8_t tmpreg;
tmpreg = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG5_A);
tmpreg |= LSM303DLHC_BOOT_REBOOTMEMORY;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG5_A, tmpreg);
}{ ... }
/* ... */
void LSM303DLHC_AccFilterConfig(uint8_t FilterStruct)
{
uint8_t tmpreg;
tmpreg = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A);
tmpreg &= 0x0C;
tmpreg |= FilterStruct;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, tmpreg);
}{ ... }
/* ... */
void LSM303DLHC_AccFilterCmd(uint8_t HighPassFilterState)
{
uint8_t tmpreg;
tmpreg = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A);
tmpreg &= 0xF7;
tmpreg |= HighPassFilterState;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, tmpreg);
}{ ... }
/* ... */
void LSM303DLHC_AccReadXYZ(int16_t* pData)
{
int16_t pnRawData[3];
uint8_t ctrlx[2]={0,0};
int8_t buffer[6];
uint8_t i = 0;
uint8_t sensitivity = LSM303DLHC_ACC_SENSITIVITY_2G;
ctrlx[0] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG4_A);
ctrlx[1] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG5_A);
buffer[0] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_L_A);
buffer[1] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_X_H_A);
buffer[2] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_Y_L_A);
buffer[3] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_Y_H_A);
buffer[4] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_Z_L_A);
buffer[5] = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_OUT_Z_H_A);
if(!(ctrlx[0] & LSM303DLHC_BLE_MSB))
{
for(i=0; i<3; i++)
{
pnRawData[i]=((int16_t)((uint16_t)buffer[2*i+1] << 8) + buffer[2*i]);
}for (i=0; i<3; i++) { ... }
}if (!(ctrlx[0] & LSM303DLHC_BLE_MSB)) { ... }
else
{
for(i=0; i<3; i++)
{
pnRawData[i]=((int16_t)((uint16_t)buffer[2*i] << 8) + buffer[2*i+1]);
}for (i=0; i<3; i++) { ... }
}else { ... }
switch(ctrlx[0] & LSM303DLHC_FULLSCALE_16G)
{
case LSM303DLHC_FULLSCALE_2G:
sensitivity = LSM303DLHC_ACC_SENSITIVITY_2G;
break;case LSM303DLHC_FULLSCALE_2G:
case LSM303DLHC_FULLSCALE_4G:
sensitivity = LSM303DLHC_ACC_SENSITIVITY_4G;
break;case LSM303DLHC_FULLSCALE_4G:
case LSM303DLHC_FULLSCALE_8G:
sensitivity = LSM303DLHC_ACC_SENSITIVITY_8G;
break;case LSM303DLHC_FULLSCALE_8G:
case LSM303DLHC_FULLSCALE_16G:
sensitivity = LSM303DLHC_ACC_SENSITIVITY_16G;
break;case LSM303DLHC_FULLSCALE_16G:
}switch (ctrlx[0] & LSM303DLHC_FULLSCALE_16G) { ... }
for(i=0; i<3; i++)
{
pData[i]=(pnRawData[i] * sensitivity);
}for (i=0; i<3; i++) { ... }
}{ ... }
/* ... */
void LSM303DLHC_AccFilterClickCmd(uint8_t HighPassFilterClickState)
{
uint8_t tmpreg = 0x00;
tmpreg = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A);
tmpreg &= ~(LSM303DLHC_HPF_CLICK_ENABLE);
tmpreg |= HighPassFilterClickState;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG2_A, tmpreg);
}{ ... }
/* ... */
void LSM303DLHC_AccIT1Enable(uint8_t LSM303DLHC_IT)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG3_A);
tmpval |= LSM303DLHC_IT;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG3_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccIT1Disable(uint8_t LSM303DLHC_IT)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG3_A);
tmpval &= ~LSM303DLHC_IT;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG3_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccIT2Enable(uint8_t LSM303DLHC_IT)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG6_A);
tmpval |= LSM303DLHC_IT;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG6_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccIT2Disable(uint8_t LSM303DLHC_IT)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG6_A);
tmpval &= ~LSM303DLHC_IT;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CTRL_REG6_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccINT1InterruptEnable(uint8_t ITCombination, uint8_t ITAxes)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_INT1_CFG_A);
tmpval |= (ITAxes | ITCombination);
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_INT1_CFG_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccINT1InterruptDisable(uint8_t ITCombination, uint8_t ITAxes)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_INT1_CFG_A);
tmpval &= ~(ITAxes | ITCombination);
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_INT1_CFG_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccINT2InterruptEnable(uint8_t ITCombination, uint8_t ITAxes)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_INT2_CFG_A);
tmpval |= (ITAxes | ITCombination);
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_INT2_CFG_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccINT2InterruptDisable(uint8_t ITCombination, uint8_t ITAxes)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_INT2_CFG_A);
tmpval &= ~(ITAxes | ITCombination);
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_INT2_CFG_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccClickITEnable(uint8_t ITClick)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CLICK_CFG_A);
tmpval |= ITClick;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CLICK_CFG_A, tmpval);
tmpval = 0x0A;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CLICK_THS_A, tmpval);
tmpval = 0x05;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_TIME_LIMIT_A, tmpval);
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_TIME_LATENCY_A, tmpval);
tmpval = 0x32;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_TIME_WINDOW_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccClickITDisable(uint8_t ITClick)
{
uint8_t tmpval = 0x00;
tmpval = COMPASSACCELERO_IO_Read(ACC_I2C_ADDRESS, LSM303DLHC_CLICK_CFG_A);
tmpval &= ~ITClick;
COMPASSACCELERO_IO_Write(ACC_I2C_ADDRESS, LSM303DLHC_CLICK_CFG_A, tmpval);
}{ ... }
/* ... */
void LSM303DLHC_AccZClickITConfig(void)
{
COMPASSACCELERO_IO_ITConfig();
LSM303DLHC_AccIT1Enable(LSM303DLHC_IT1_CLICK);
LSM303DLHC_AccFilterClickCmd(LSM303DLHC_HPF_CLICK_ENABLE);
LSM303DLHC_AccClickITEnable(LSM303DLHC_Z_SINGLE_CLICK);
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */