-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gyroscope.spin
284 lines (210 loc) · 18.4 KB
/
Gyroscope.spin
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
{{
┌─────────────────────────────────────┬────────────────────────┬──────┬────────┐
│ L3G4200D_Example_Code.spin │ (C)2011 Parallax, Inc. │ V1.0 │ Jul 25 │
├─────────────────────────────────────┴────────────────────────┴──────┴────────┤
│ Demonstrates X,Y,Z output to a serial terminal. Uses default (I²C) interface │
│ on the Gyroscope module. │
│ This program is for example purposes only for RAW data from the Gyroscope │
│ Module. The calculations provided are for example only and should not be │
│ relied on for accuracy. For details about calculating Gyroscope values and │
│ other important information on functionality, please see the documents on our│
│ product page. Our Forums also have very useful information on this topic. │
│ │
└──────────────────────────────────────────────────────────────────────────────┘
''modified by Dave Ratcliff 'ratronic' 4/6/12 added 1s delay to beginning of calibrate method, added integrated axis variables and display, changed baud rate
┌─────────────────┐ ''also added 10ms delay to main loop
│ L INT2 ┣─── Data ready/FIFO interrupt
│ 3 INT1 ┣─── Programmable interrupt
│ G SDO ┣─── SPI serial data output -- I²C LSB of the device address (SA0)
│ 4 SDA/SDI/SDO ┣─── I²C serial data (SDA) -- SPI serial data input (SDI) -- 3-wire SPI data output (SDO)
│ 2 SCL ┣─── I²C & SPI clock
│ 0 CS ┣─── I²C/SPI mode selection (defaulted to I²C) pull low for SPI mode
│ 0 VIN ┣─── 2.7 - 6.5VDC (module is regulated to 2.5VDC)
│ D GND ┣──┐
└─────────────────┘
}}
CON
SDApin = 24
SCLpin = 25
'****Registers****
WRITE = $D2
READ = $D3
CTRL_REG1 = $20 'SUB $A0
CTRL_REG3 = $22
CTRL_REG4 = $23
STATUS_REG = $27
OUT_X_INC = $A8
x_idx = 0
y_idx = 1
z_idx = 2
VAR
long x, ix
long y, iy
long z, iz
long cx
long cy
long cz
long ff_x
long ff_y
long ff_z
long multiBYTE[3]
PUB Start
Wrt_1B(CTRL_REG3, $08) 'set up data ready signal
Wrt_1B(CTRL_REG4, $80) 'set up "block data update" mode (to avoid bad reads when the values would get updated while we are reading)
Wrt_1B(CTRL_REG1, $1F) 'write a byte to control register one (enable all axis, 100Hz update rate)
Calibrate
PUB Update
WaitForDataReady
Read_MultiB(OUT_X_INC)
x := x - cx
y := y - cy
z := z - cz
x := x / 114
y := y / 114
z := z / 114
ix += x
iy += y
iz += z
PUB GetRelativeX
return x
PUB GetRelativeY
return y
PUB GetRelativeZ
return Z
PUB GetX
return ix
PUB GetY
return iy
PUB GetZ
return iz
PUB Flush
ix := 0
iy := 0
iz := 0
PUB FlushX
ix := 0
PUB FlushY
iy := 0
PUB FlushZ
iz := 0
PUB Calibrate
waitcnt(clkfreq/2+cnt) ''''added for physical delay
cx := 0
cy := 0
cz := 0
repeat 25
WaitForDataReady
Read_MultiB(OUT_X_INC) ' read the 3 axis values and accumulate
cx += x
cy += y
cz += z
cx /= 25 ' calculate the average
cy /= 25
cz /= 25
PUB WaitForDataReady | status
repeat
status := Read_1B(STATUS_REG) ' read the ZYXZDA bit of the status register (looping until the bit is on)
if (status & $08) == $08
quit
PUB Wrt_1B(SUB1, data)
''Write single byte to Gyroscope.
start_
send(WRITE) 'device address as write command
'slave ACK
send(SUB1) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
send(data) 'data you want to send
'slave ACK
stop
PUB Wrt_MultiB(SUB2, data, data2)
''Write multiple bytes to Gyroscope.
start_
send(WRITE) 'device address as write command
'slave ACK
send(SUB2) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
send(data) 'data you want to send
'slave ACK
send(data2) 'data you want to send
'slave ACK
stop
PUB Read_1B(SUB3) | rxd
''Read single byte from Gyroscope
start_
send(WRITE) 'device address as write command
'slave ACK
send(SUB3) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
stop
start_ 'SR condition
send(READ) 'device address as read command
'slave ACK
rxd := receive(false) 'recieve the byte and put in variable rxd
stop
result := rxd
PUB Read_MultiB(SUB3)
''Read multiple bytes from Gyroscope
start_
send(WRITE) 'device address as write command
'slave ACK
send(SUB3) 'SUB address = Register MSB 1 = reg address auto increment
'slave ACK
stop
start_ 'SR condition
send(READ) 'device address as read command
'slave ACK
multiBYTE[x_idx] := (receive(true)) | (receive(true)) << 8 'Receives high and low bytes of Raw data
multiBYTE[y_idx] := (receive(true)) | (receive(true)) << 8
multiBYTE[z_idx] := (receive(true)) | (receive(false)) << 8
stop
x := ~~multiBYTE[x_idx]
y := ~~multiBYTE[y_idx]
z := ~~multiBYTE[z_idx]
PRI send(value) ' I²C Send data - 4 Stack Longs
value := ((!value) >< 8)
repeat 8
dira[SDApin] := value
dira[SCLpin] := false
dira[SCLpin] := true
value >>= 1
dira[SDApin] := false
dira[SCLpin] := false
result := not(ina[SDApin])
dira[SCLpin] := true
dira[SDApin] := true
PRI receive(aknowledge) ' I²C receive data - 4 Stack Longs
dira[SDApin] := false
repeat 8
result <<= 1
dira[SCLpin] := false
result |= ina[SDApin]
dira[SCLpin] := true
dira[SDApin] := (aknowledge)
dira[SCLpin] := false
dira[SCLpin] := true
dira[SDApin] := true
PRI start_ ' 3 Stack Longs
outa[SDApin] := false
outa[SCLpin] := false
dira[SDApin] := true
dira[SCLpin] := true
PRI stop ' 3 Stack Longs
dira[SCLpin] := false
dira[SDApin] := false
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ TERMS OF USE: MIT License │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions: │
│ │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│ │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}