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