-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyro_l3gd20h.cpp
154 lines (109 loc) · 2.19 KB
/
gyro_l3gd20h.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
/**
* @file gyro-l3gd20h.cpp
* @brief Implementation of the L3GD20H gyro chip for ARM embed projects
* @author Jan-Willem Smaal <[email protected]>
* @date 7/9/2020
*/
#include "gyro_l3gd20h.h"
Gyro::Gyro()
{
x = 0;
y = 0;
z = 0;
temperature = 0;
i2c.start ();
i2c.write(L3G_WRITE_PATTERN_SDO);
i2c.write(CTRL_REG1);
i2c.write(0x3F);
//i2c.write()
i2c.stop();
}
uint8_t Gyro::ReadRegister(enum l3g_register reg)
{
uint8_t tmp;
i2c.start();
i2c.write(L3G_WRITE_PATTERN_SDO);
i2c.write(reg);
i2c.start();
i2c.write(L3G_READ_PATTERN_SDO);
tmp = i2c.read(0);
i2c.stop();
return tmp;
}
/**
* Read the register X and store in the class member.
* @author Jan-Willem Smaal <[email protected]>
* @param void
* @date 7/9/2020
*/
int16_t Gyro::ReadX(void)
{
uint16_t tmp;
tmp = Gyro::ReadRegister(OUT_X_L);
tmp = tmp + (Gyro::ReadRegister(OUT_X_H)<<8);
x = tmp;
return tmp;
}
/**
* Read the register Y and store in the class member.
* @author Jan-Willem Smaal <[email protected]>
* @param void
* @date 7/9/2020
*/
int16_t Gyro::ReadY(void)
{
int16_t tmp;
tmp = Gyro::ReadRegister(OUT_Y_L);
tmp = tmp + (Gyro::ReadRegister(OUT_Y_H)<<8);
y = tmp;
return tmp;
}
int16_t Gyro::ReadZ(void)
{
int16_t tmp;
tmp = Gyro::ReadRegister(OUT_Z_L);
tmp = tmp + (Gyro::ReadRegister(OUT_Z_H)<<8);
z = tmp;
return tmp;
}
inline void Gyro::PowerUp(void)
{
}
inline void Gyro::PowerDown(void)
{
}
inline void Gyro::EnableX(void)
{
}
inline void Gyro::EnableY(void)
{
}
inline void Gyro::EnableZ(void)
{
}
int8_t Gyro::ReadTemp(void)
{
int8_t temp;
i2c.start();
i2c.write(L3G_WRITE_PATTERN_SDO);
i2c.write(OUT_TEMP);
i2c.start();
i2c.write(L3G_READ_PATTERN_SDO);
temp = i2c.read(0);
i2c.stop();
temperature = temp;
return temp;
}
uint8_t Gyro::WhoAmI(void)
{
uint8_t who;
i2c.start();
i2c.write(L3G_WRITE_PATTERN_SDO);
i2c.write(WHO_AM_I);
i2c.start();
i2c.write(L3G_READ_PATTERN_SDO);
who = i2c.read(0);
i2c.stop();
return who;
}
/* EOF */