-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacc.c
71 lines (55 loc) · 1.05 KB
/
acc.c
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
#include "acc.h"
volatile ACC_Achse_TypeDef achse[3];
void initAcc(void)
{
writeSpi(0x20, 0x27);
for(uint8_t i=0; i<3; i++)
{
achse[i].offset = 0;
}
}
void updateAcc(void)
{
uint8_t addr = 0x28;
uint8_t tmp;
tmp = readSpi(0x27);
//update data if there are new
if((tmp & 0x08) != 0)
{
for(uint8_t i=0; i<3; i++)
{
achse[i].low = readSpi(addr);
addr++;
achse[i].high = readSpi(addr);
addr++;
}
}
}
int16_t getAchseAcc(uint8_t pos)
{
int16_t out;
out = ((int16_t)(achse[pos].high<<8 | achse[pos].low))>>4;
out += achse[pos].offset;
return out;
}
//no real calibration only set the actual position as default
void calibrateAcc(void)
{
int16_t tmp;
updateAcc();
for(uint8_t i=0; i<3; i++)
{
achse[i].offset = 0;
}
tmp = getAchseAcc(X_ACHSE);
achse[X_ACHSE].offset = tmp * (-1);
tmp = getAchseAcc(Y_ACHSE);
achse[Y_ACHSE].offset = tmp * (-1);
tmp = getAchseAcc(Z_ACHSE);
achse[Z_ACHSE].offset = (tmp - 341) * (-1);
}
//returns the offset of the given axis
int16_t getOffset(uint8_t pos)
{
return achse[pos].offset;
}