Skip to content
Rodion Gorkovenko edited this page Feb 7, 2018 · 3 revisions

Interfacing MPU-6050 with Miskatino

MPU-6050 accelerometer & gyroscope is attached with SCL on pin 6, SDA on pin 7. Two LEDs are added to to pins 0 and 2 (via resistor to GND). I2C is bit-banged by subroutines at lines 100 (read), 300 (write), 200 (byte exchange).

This program causes one or other LED to light when device is tilted along Y axis. I suspect my MPU-6050 may be not original as it has only one accelerometer axis working (while gyro works all 3) and reads data only via fifo, not directly from 59-64 registers. Temp sensor also works (also via fifo).

1 rem Setup MPU-6050, accel to fifo
5 i2=0x806b
6 gosub 300
7 delay 100
10 i2=0x06b
11 gosub 300
12 i2=0x416a
13 gosub 300
14 i2=0x823
15 gosub 300
19 rem Read Fifo in Loop
20 i2=0x72
21 gosub 100
22 v=i3*256
23 i2=0x73
24 gosub 100
25 if v+i3<6; goto 20
35 i=0
40 i2=0x74
45 gosub 100
50 v=i3*256
55 i2=0x74
60 gosub 100
61 v=v+i3
62 if v>0x7fff;v=v-0x10000
65 print v+i3
70 i=i+1
75 if i<3; goto 40
80 print ""
81 pin 0;v<-7000
82 pin 2;v>7000
85 goto 20
100 rem Read Register
104 pin 7;0
105 pin 6;0
107 ia=0
110 id = 208
115 gosub 200
116 ix = ib
120 id = i2
125 gosub 200
126 ix = ix+ib*2
130 pin 6;-1
135 pin 7;0
137 pin 6;0
140 id = 209
145 gosub 200
146 ix = ix+ib*4
150 ia = 0
155 id = 0xff
160 gosub 200
165 i3 = ir
170 pin 6;-1
175 pin 7;-1
180 return
200 rem Exchange Byte
201 ir = 0
202 ii = 0
205 pin 7;0-(id/128)
210 pin 6;-1
211 ir = ir*2+pin(7)
215 pin 6;0
220 ii = ii+1
225 id = (id%128)*2
230 if ii<8; goto 205
235 pin 7;-(ia=0)
240 pin 6;-1
245 ib = 1-pin(7)
250 pin 6;0
255 return
300 rem Write Register
304 pin 7;0
305 pin 6;0
307 ia=0
310 id = 208
315 gosub 200
316 ix = ib
320 id = i2%256
325 gosub 200
330 ix = ix+ib*2
335 id = i2/256
336 print "wr:";i3;"=>";i2
340 gosub 200
345 ix = ix+ib*4
350 pin 6;-1
355 pin 7;-1
360 return
Clone this wiki locally