#include "L3G4200D.h"
L3G4200D::L3G4200D():I2C(p28,p27) // sda, scl
{
init();
}
void L3G4200D::init()
{
address = 0xD2;
write(0x22,8); // CTRL_REG3 Set up data ready signal
write(0x23,0x80); // CTRL_REG4 Set up "block data update" mode
write(0x20,0x1f); // CTRL_REG1 Send the get continuous output command
int average[3];
for(int i=0; i<3; i++) average[i]=mean[i]=0;
short data[3];
for(int i=0; i<30; i++)
{
readData(data);
for(int j=0; j<3; j++) average[j] += data[j];
}
for(int i=0; i<3; i++)
{
mean[i] = average[i]/30;
}
}
void L3G4200D::write(unsigned char control, unsigned char dt)
{
start();
((I2C*)this)->write(address);
((I2C*)this)->write(control);
((I2C*)this)->write(dt);
stop();
}
unsigned char L3G4200D::read(unsigned char control)
{
start();
((I2C*)this)->write(address);
((I2C*)this)->write(control);
start();
((I2C*)this)->write(address|1);
unsigned char dt=((I2C*)this)->read(0);
stop();
return dt;
}
void L3G4200D::readData(short* data)
{
unsigned char high, low;
waitForDataReady();
start();
((I2C*)this)->write(address);
((I2C*)this)->write(0xA8); //OUT_X_INC Read the data starting at pointer register
stop();
start();
((I2C*)this)->write(address|1);
for(int i=0; i<3; i++) {
low = ((I2C*)this)->read(1);
high = ((I2C*)this)->read((i==2)?0:1);
data[i] = (short)(((high) << 8) | low)-mean[i];
}
stop();
}
void L3G4200D::waitForDataReady()
{
unsigned char data;
do {
data = read(0x27);
} while(!(data & 0x08));
}
|
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
|