Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
918 | hbuss | 6 | volatile unsigned char twi_state = 0; |
1 | ingob | 7 | unsigned char motor = 0; |
8 | unsigned char motorread = 0; |
||
1111 | hbuss | 9 | unsigned char motor_rx[16]; |
1 | ingob | 10 | |
11 | //############################################################################ |
||
12 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
13 | void i2c_init(void) |
||
14 | //############################################################################ |
||
15 | { |
||
16 | TWSR = 0; |
||
17 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
18 | } |
||
19 | |||
20 | //############################################################################ |
||
21 | //Start I2C |
||
22 | char i2c_start(void) |
||
23 | //############################################################################ |
||
24 | { |
||
25 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
26 | return(0); |
||
27 | } |
||
28 | |||
29 | //############################################################################ |
||
30 | //Start I2C |
||
31 | void i2c_stop(void) |
||
32 | //############################################################################ |
||
33 | { |
||
34 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
35 | } |
||
36 | |||
173 | holgerb | 37 | void i2c_reset(void) |
1 | ingob | 38 | //############################################################################ |
173 | holgerb | 39 | { |
40 | i2c_stop(); |
||
41 | twi_state = 0; |
||
42 | motor = TWDR; |
||
43 | motor = 0; |
||
44 | TWCR = 0x80; |
||
45 | TWAMR = 0; |
||
46 | TWAR = 0; |
||
47 | TWDR = 0; |
||
48 | TWSR = 0; |
||
49 | TWBR = 0; |
||
50 | i2c_init(); |
||
51 | i2c_start(); |
||
52 | i2c_write_byte(0); |
||
53 | } |
||
54 | |||
55 | //############################################################################ |
||
1 | ingob | 56 | //Start I2C |
57 | char i2c_write_byte(char byte) |
||
58 | //############################################################################ |
||
59 | { |
||
60 | TWSR = 0x00; |
||
61 | TWDR = byte; |
||
62 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
63 | |||
64 | return(0); |
||
65 | |||
66 | } |
||
67 | |||
1111 | hbuss | 68 | #ifdef OCTO |
1 | ingob | 69 | //############################################################################ |
70 | //Start I2C |
||
71 | SIGNAL (TWI_vect) |
||
72 | //############################################################################ |
||
73 | { |
||
74 | switch (twi_state++) |
||
75 | { |
||
76 | case 0: |
||
77 | i2c_write_byte(0x52+(motor*2)); |
||
78 | break; |
||
79 | case 1: |
||
80 | switch(motor++) |
||
81 | { |
||
82 | case 0: |
||
1111 | hbuss | 83 | i2c_write_byte(Motor1); |
84 | break; |
||
85 | case 1: |
||
86 | i2c_write_byte(Motor2); |
||
87 | break; |
||
88 | case 2: |
||
89 | i2c_write_byte(Motor3); |
||
90 | break; |
||
91 | case 3: |
||
92 | i2c_write_byte(Motor4); |
||
93 | break; |
||
94 | case 4: |
||
95 | i2c_write_byte(Motor5); |
||
96 | break; |
||
97 | case 5: |
||
98 | i2c_write_byte(Motor6); |
||
99 | break; |
||
100 | case 6: |
||
101 | i2c_write_byte(Motor7); |
||
102 | break; |
||
103 | case 7: |
||
104 | i2c_write_byte(Motor8); |
||
105 | break; |
||
106 | } |
||
107 | break; |
||
108 | case 2: |
||
109 | i2c_stop(); |
||
110 | if (motor<8) twi_state = 0; |
||
111 | else motor = 0; |
||
112 | i2c_start(); |
||
113 | break; |
||
114 | |||
115 | //Liest Daten von Motor |
||
116 | case 3: |
||
117 | i2c_write_byte(0x53+(motorread*2)); |
||
118 | break; |
||
119 | case 4: |
||
120 | switch(motorread) |
||
121 | { |
||
122 | case 0: |
||
123 | i2c_write_byte(Motor1); |
||
124 | break; |
||
125 | case 1: |
||
126 | i2c_write_byte(Motor2); |
||
127 | break; |
||
128 | case 2: |
||
129 | i2c_write_byte(Motor3); |
||
130 | break; |
||
131 | case 3: |
||
132 | i2c_write_byte(Motor4); |
||
133 | break; |
||
134 | case 4: |
||
135 | i2c_write_byte(Motor5); |
||
136 | break; |
||
137 | case 5: |
||
138 | i2c_write_byte(Motor6); |
||
139 | break; |
||
140 | case 6: |
||
141 | i2c_write_byte(Motor7); |
||
142 | break; |
||
143 | case 7: |
||
144 | i2c_write_byte(Motor8); |
||
145 | break; |
||
146 | } |
||
147 | break; |
||
148 | case 5: //1 Byte vom Motor lesen |
||
149 | motor_rx[motorread] = TWDR; |
||
150 | |||
151 | case 6: |
||
152 | switch(motorread) |
||
153 | { |
||
154 | case 0: |
||
155 | i2c_write_byte(Motor1); |
||
156 | break; |
||
157 | case 1: |
||
158 | i2c_write_byte(Motor2); |
||
159 | break; |
||
160 | case 2: |
||
161 | i2c_write_byte(Motor3); |
||
162 | break; |
||
163 | case 3: |
||
164 | i2c_write_byte(Motor4); |
||
165 | break; |
||
166 | case 4: |
||
167 | i2c_write_byte(Motor5); |
||
168 | break; |
||
169 | case 5: |
||
170 | i2c_write_byte(Motor6); |
||
171 | break; |
||
172 | case 6: |
||
173 | i2c_write_byte(Motor7); |
||
174 | break; |
||
175 | case 7: |
||
176 | i2c_write_byte(Motor8); |
||
177 | break; |
||
178 | } |
||
179 | break; |
||
180 | case 7: //2 Byte vom Motor lesen |
||
181 | motor_rx[motorread+4] = TWDR; |
||
182 | motorread++; |
||
183 | if (motorread>7) motorread=0; |
||
184 | i2c_stop(); |
||
185 | I2CTimeout = 10; |
||
186 | twi_state = 0; |
||
187 | break; |
||
188 | case 8: // Gyro-Offset |
||
189 | i2c_write_byte(0x98); // Address of the DAC |
||
190 | break; |
||
191 | case 9: |
||
192 | i2c_write_byte(0x10); // Update Channel A |
||
193 | break; |
||
194 | case 10: |
||
195 | i2c_write_byte(AnalogOffsetNick); // Value |
||
196 | break; |
||
197 | case 11: |
||
198 | i2c_write_byte(0x80); // Value |
||
199 | break; |
||
200 | case 12: |
||
201 | i2c_stop(); |
||
202 | I2CTimeout = 10; |
||
203 | i2c_start(); |
||
204 | break; |
||
205 | case 13: |
||
206 | i2c_write_byte(0x98); // Address of the DAC |
||
207 | break; |
||
208 | case 14: |
||
209 | i2c_write_byte(0x12); // Update Channel B |
||
210 | break; |
||
211 | case 15: |
||
212 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
213 | break; |
||
214 | case 16: |
||
215 | i2c_write_byte(0x80); // Value |
||
216 | break; |
||
217 | case 17: |
||
218 | i2c_stop(); |
||
219 | I2CTimeout = 10; |
||
220 | i2c_start(); |
||
221 | break; |
||
222 | case 18: |
||
223 | i2c_write_byte(0x98); // Address of the DAC |
||
224 | break; |
||
225 | case 19: |
||
226 | i2c_write_byte(0x14); // Update Channel C |
||
227 | break; |
||
228 | case 20: |
||
229 | i2c_write_byte(AnalogOffsetGier); // Value |
||
230 | break; |
||
231 | case 21: |
||
232 | i2c_write_byte(0x80); // Value |
||
233 | break; |
||
234 | case 22: |
||
235 | i2c_stop(); |
||
236 | I2CTimeout = 10; |
||
237 | twi_state = 0; |
||
238 | break; |
||
239 | } |
||
240 | TWCR |= 0x80; |
||
241 | } |
||
242 | #else |
||
243 | //############################################################################ |
||
244 | //Start I2C |
||
245 | SIGNAL (TWI_vect) |
||
246 | //############################################################################ |
||
247 | { |
||
248 | switch (twi_state++) |
||
249 | { |
||
250 | case 0: |
||
251 | i2c_write_byte(0x52+(motor*2)); |
||
252 | break; |
||
253 | case 1: |
||
254 | switch(motor++) |
||
255 | { |
||
256 | case 0: |
||
1 | ingob | 257 | i2c_write_byte(Motor_Vorne); |
258 | break; |
||
259 | case 1: |
||
260 | i2c_write_byte(Motor_Hinten); |
||
261 | break; |
||
262 | case 2: |
||
263 | i2c_write_byte(Motor_Rechts); |
||
264 | break; |
||
265 | case 3: |
||
266 | i2c_write_byte(Motor_Links); |
||
267 | break; |
||
268 | } |
||
269 | break; |
||
270 | case 2: |
||
271 | i2c_stop(); |
||
272 | if (motor<4) twi_state = 0; |
||
273 | else motor = 0; |
||
274 | i2c_start(); |
||
275 | break; |
||
276 | |||
277 | //Liest Daten von Motor |
||
278 | case 3: |
||
279 | i2c_write_byte(0x53+(motorread*2)); |
||
280 | break; |
||
281 | case 4: |
||
282 | switch(motorread) |
||
283 | { |
||
284 | case 0: |
||
285 | i2c_write_byte(Motor_Vorne); |
||
286 | break; |
||
287 | case 1: |
||
288 | i2c_write_byte(Motor_Hinten); |
||
289 | break; |
||
290 | case 2: |
||
291 | i2c_write_byte(Motor_Rechts); |
||
292 | break; |
||
293 | case 3: |
||
294 | i2c_write_byte(Motor_Links); |
||
295 | break; |
||
296 | } |
||
297 | break; |
||
298 | case 5: //1 Byte vom Motor lesen |
||
299 | motor_rx[motorread] = TWDR; |
||
173 | holgerb | 300 | |
1 | ingob | 301 | case 6: |
302 | switch(motorread) |
||
303 | { |
||
304 | case 0: |
||
305 | i2c_write_byte(Motor_Vorne); |
||
306 | break; |
||
307 | case 1: |
||
308 | i2c_write_byte(Motor_Hinten); |
||
309 | break; |
||
310 | case 2: |
||
311 | i2c_write_byte(Motor_Rechts); |
||
312 | break; |
||
313 | case 3: |
||
314 | i2c_write_byte(Motor_Links); |
||
315 | break; |
||
316 | } |
||
317 | break; |
||
318 | case 7: //2 Byte vom Motor lesen |
||
319 | motor_rx[motorread+4] = TWDR; |
||
320 | motorread++; |
||
321 | if (motorread>3) motorread=0; |
||
322 | i2c_stop(); |
||
173 | holgerb | 323 | I2CTimeout = 10; |
1 | ingob | 324 | twi_state = 0; |
918 | hbuss | 325 | break; |
326 | case 8: // Gyro-Offset |
||
327 | i2c_write_byte(0x98); // Address of the DAC |
||
328 | break; |
||
329 | case 9: |
||
330 | i2c_write_byte(0x10); // Update Channel A |
||
331 | break; |
||
332 | case 10: |
||
333 | i2c_write_byte(AnalogOffsetNick); // Value |
||
334 | break; |
||
335 | case 11: |
||
336 | i2c_write_byte(0x80); // Value |
||
337 | break; |
||
338 | case 12: |
||
339 | i2c_stop(); |
||
340 | I2CTimeout = 10; |
||
341 | i2c_start(); |
||
342 | break; |
||
343 | case 13: |
||
344 | i2c_write_byte(0x98); // Address of the DAC |
||
345 | break; |
||
346 | case 14: |
||
347 | i2c_write_byte(0x12); // Update Channel B |
||
348 | break; |
||
349 | case 15: |
||
350 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
351 | break; |
||
352 | case 16: |
||
353 | i2c_write_byte(0x80); // Value |
||
354 | break; |
||
355 | case 17: |
||
356 | i2c_stop(); |
||
357 | I2CTimeout = 10; |
||
358 | i2c_start(); |
||
359 | break; |
||
360 | case 18: |
||
361 | i2c_write_byte(0x98); // Address of the DAC |
||
362 | break; |
||
363 | case 19: |
||
364 | i2c_write_byte(0x14); // Update Channel C |
||
365 | break; |
||
366 | case 20: |
||
367 | i2c_write_byte(AnalogOffsetGier); // Value |
||
368 | break; |
||
369 | case 21: |
||
370 | i2c_write_byte(0x80); // Value |
||
371 | break; |
||
372 | case 22: |
||
373 | i2c_stop(); |
||
374 | I2CTimeout = 10; |
||
375 | twi_state = 0; |
||
376 | break; |
||
1 | ingob | 377 | } |
173 | holgerb | 378 | TWCR |= 0x80; |
1 | ingob | 379 | } |
1111 | hbuss | 380 | #endif |