Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
834 | MikeW | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | #include "kafi.h" |
||
6 | |||
7 | unsigned char twi_state = 0; |
||
8 | unsigned char motor = 0; |
||
9 | unsigned char motorread = 0; |
||
10 | unsigned char motor_rx[8]; |
||
11 | |||
12 | unsigned int TriggerMagneticHeading = 0; |
||
13 | unsigned int TriggerSonicReading = 0; |
||
14 | |||
15 | |||
16 | //############################################################################ |
||
17 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
18 | void i2c_init(void) |
||
19 | //############################################################################ |
||
20 | { |
||
21 | TWSR = 0; |
||
22 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
23 | } |
||
24 | |||
25 | //############################################################################ |
||
26 | //Start I2C |
||
27 | char i2c_start(void) |
||
28 | //############################################################################ |
||
29 | { |
||
30 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
31 | return(0); |
||
32 | } |
||
33 | |||
34 | //############################################################################ |
||
35 | //Start I2C |
||
36 | void i2c_stop(void) |
||
37 | //############################################################################ |
||
38 | { |
||
39 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
40 | } |
||
41 | |||
42 | void i2c_reset(void) |
||
43 | //############################################################################ |
||
44 | { |
||
45 | i2c_stop(); |
||
46 | twi_state = 0; |
||
47 | motor = TWDR; |
||
48 | motor = 0; |
||
49 | TWCR = 0x80; |
||
50 | TWAMR = 0; |
||
51 | TWAR = 0; |
||
52 | TWDR = 0; |
||
53 | TWSR = 0; |
||
54 | TWBR = 0; |
||
55 | i2c_init(); |
||
56 | i2c_start(); |
||
57 | i2c_write_byte(0); |
||
58 | } |
||
59 | |||
60 | //############################################################################ |
||
61 | //Start I2C |
||
62 | char i2c_write_byte(char byte) |
||
63 | //############################################################################ |
||
64 | { |
||
65 | TWSR = 0x00; |
||
66 | TWDR = byte; |
||
67 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
68 | |||
69 | return(0); |
||
70 | } |
||
71 | |||
72 | //############################################################################ |
||
73 | //Start I2C |
||
74 | SIGNAL (TWI_vect) |
||
75 | //############################################################################ |
||
76 | { |
||
77 | #if 0 |
||
78 | int motorwert = 0; |
||
79 | int scale_p = (int) (MAX(0, (PPM_in[EE_Parameter.Kanalbelegung[K_POTI1]] + 170)) / 4.F) / 6; |
||
80 | int scale_d = (int) (MAX(0, (PPM_in[EE_Parameter.Kanalbelegung[K_POTI2]] + 170)) / 4.F) / 6; |
||
81 | #endif |
||
82 | |||
83 | switch (twi_state++) |
||
84 | { |
||
85 | case 0: |
||
86 | i2c_write_byte(0x52+(motor*2)); |
||
87 | break; |
||
88 | case 1: |
||
89 | switch(motor++) |
||
90 | { |
||
91 | case 0: |
||
92 | #if 0 |
||
93 | pd_ergebnis = (int) (scale_p* DiffNick + scale_d * (AdWertNick - AdNeutralNick - Roll_Y_Off)) / 10; |
||
94 | if(pd_ergebnis > (GasMischanteil + abs(GierMischanteil))) |
||
95 | { |
||
96 | pd_ergebnis = (GasMischanteil + abs(GierMischanteil)); |
||
97 | } |
||
98 | if(pd_ergebnis < -(GasMischanteil + abs(GierMischanteil))) |
||
99 | { |
||
100 | pd_ergebnis = -(GasMischanteil + abs(GierMischanteil)); |
||
101 | } |
||
102 | /* M o t o r V o r n */ |
||
103 | motorwert = GasMischanteil + pd_ergebnis + GierMischanteil; |
||
104 | if ((motorwert < 0)) |
||
105 | { |
||
106 | motorwert = 0; |
||
107 | } |
||
108 | else if(motorwert > MAX_GAS) |
||
109 | { |
||
110 | motorwert = MAX_GAS; |
||
111 | } |
||
112 | if (motorwert < MIN_GAS) |
||
113 | { |
||
114 | motorwert = MIN_GAS; |
||
115 | } |
||
116 | Motor_Vorne = motorwert; |
||
117 | if(!MotorenEin) |
||
118 | { |
||
119 | Motor_Vorne = 0; |
||
120 | if(MotorTest[0]) Motor_Vorne = MotorTest[0]; |
||
121 | } |
||
122 | #endif |
||
123 | i2c_write_byte(Motor_Vorne); |
||
124 | break; |
||
125 | case 1: |
||
126 | #if 0 |
||
127 | pd_ergebnis = (scale_p * DiffNick + scale_d * (AdWertNick - AdNeutralNick - Roll_Y_Off)) / 10; |
||
128 | if(pd_ergebnis > (GasMischanteil + abs(GierMischanteil))) |
||
129 | { |
||
130 | pd_ergebnis = (GasMischanteil + abs(GierMischanteil)); |
||
131 | } |
||
132 | if(pd_ergebnis < -(GasMischanteil + abs(GierMischanteil))) |
||
133 | { |
||
134 | pd_ergebnis = -(GasMischanteil + abs(GierMischanteil)); |
||
135 | } |
||
136 | /* M o t o r H e c k */ |
||
137 | motorwert = GasMischanteil - pd_ergebnis + GierMischanteil; |
||
138 | if ((motorwert < 0)) |
||
139 | { |
||
140 | motorwert = 0; |
||
141 | } |
||
142 | else if(motorwert > MAX_GAS) |
||
143 | { |
||
144 | motorwert = MAX_GAS; |
||
145 | } |
||
146 | if (motorwert < MIN_GAS) |
||
147 | { |
||
148 | motorwert = MIN_GAS; |
||
149 | } |
||
150 | Motor_Hinten = motorwert; |
||
151 | |||
152 | if(!MotorenEin) |
||
153 | { |
||
154 | Motor_Hinten = 0; |
||
155 | if(MotorTest[1]) Motor_Hinten = MotorTest[1]; |
||
156 | } |
||
157 | #endif |
||
158 | i2c_write_byte(Motor_Hinten); |
||
159 | break; |
||
160 | case 2: |
||
161 | #if 0 |
||
162 | pd_ergebnis = (scale_p * DiffRoll + scale_d * (AdWertRoll - AdNeutralRoll - Roll_X_Off)) / 10; |
||
163 | if(pd_ergebnis > (GasMischanteil + abs(GierMischanteil))) |
||
164 | { |
||
165 | pd_ergebnis = (GasMischanteil + abs(GierMischanteil)); |
||
166 | } |
||
167 | if(pd_ergebnis < -(GasMischanteil + abs(GierMischanteil))) |
||
168 | { |
||
169 | pd_ergebnis = -(GasMischanteil + abs(GierMischanteil)); |
||
170 | } |
||
171 | /* M o t o r R e c h t s */ |
||
172 | motorwert = GasMischanteil - pd_ergebnis - GierMischanteil; |
||
173 | if (motorwert < 0) |
||
174 | { |
||
175 | motorwert = 0; |
||
176 | } |
||
177 | else if(motorwert > MAX_GAS) |
||
178 | { |
||
179 | motorwert = MAX_GAS; |
||
180 | } |
||
181 | if (motorwert < MIN_GAS) |
||
182 | { |
||
183 | motorwert = MIN_GAS; |
||
184 | } |
||
185 | Motor_Rechts = motorwert; |
||
186 | if(!MotorenEin) |
||
187 | { |
||
188 | Motor_Rechts = 0; |
||
189 | if(MotorTest[3]) Motor_Rechts = MotorTest[3]; |
||
190 | } |
||
191 | #endif |
||
192 | i2c_write_byte(Motor_Rechts); |
||
193 | break; |
||
194 | case 3: |
||
195 | #if 0 |
||
196 | pd_ergebnis = (scale_p* DiffRoll + scale_d * (AdWertRoll - AdNeutralRoll - Roll_X_Off)) / 10; |
||
197 | if(pd_ergebnis > (GasMischanteil + abs(GierMischanteil))) |
||
198 | { |
||
199 | pd_ergebnis = (GasMischanteil + abs(GierMischanteil)); |
||
200 | } |
||
201 | if(pd_ergebnis < -(GasMischanteil + abs(GierMischanteil))) |
||
202 | { |
||
203 | pd_ergebnis = -(GasMischanteil + abs(GierMischanteil)); |
||
204 | } |
||
205 | /* M o t o r L i n k s */ |
||
206 | motorwert = GasMischanteil + pd_ergebnis - GierMischanteil; |
||
207 | if (motorwert < 0) |
||
208 | { |
||
209 | motorwert = 0; |
||
210 | } |
||
211 | else if(motorwert > MAX_GAS) |
||
212 | { |
||
213 | motorwert = MAX_GAS; |
||
214 | } |
||
215 | if (motorwert < MIN_GAS) |
||
216 | { |
||
217 | motorwert = MIN_GAS; |
||
218 | } |
||
219 | Motor_Links = motorwert; |
||
220 | if(!MotorenEin) |
||
221 | { |
||
222 | Motor_Links = 0; |
||
223 | if(MotorTest[2]) Motor_Links = MotorTest[2]; |
||
224 | } |
||
225 | #endif |
||
226 | i2c_write_byte(Motor_Links); |
||
227 | break; |
||
228 | } |
||
229 | break; |
||
230 | case 2: |
||
231 | i2c_stop(); |
||
232 | if (motor<4) twi_state = 0; |
||
233 | else motor = 0; |
||
234 | i2c_start(); |
||
235 | break; |
||
236 | |||
237 | //Liest Daten von Motor |
||
238 | case 3: |
||
239 | i2c_write_byte(0x53+(motorread*2)); |
||
240 | break; |
||
241 | case 4: |
||
242 | switch(motorread) |
||
243 | { |
||
244 | case 0: |
||
245 | i2c_write_byte(Motor_Vorne); |
||
246 | break; |
||
247 | case 1: |
||
248 | i2c_write_byte(Motor_Hinten); |
||
249 | break; |
||
250 | case 2: |
||
251 | i2c_write_byte(Motor_Rechts); |
||
252 | break; |
||
253 | case 3: |
||
254 | i2c_write_byte(Motor_Links); |
||
255 | break; |
||
256 | } |
||
257 | break; |
||
258 | case 5: //1 Byte vom Motor lesen |
||
259 | motor_rx[motorread] = TWDR; |
||
260 | |||
261 | case 6: |
||
262 | switch(motorread) |
||
263 | { |
||
264 | case 0: |
||
265 | i2c_write_byte(Motor_Vorne); |
||
266 | break; |
||
267 | case 1: |
||
268 | i2c_write_byte(Motor_Hinten); |
||
269 | break; |
||
270 | case 2: |
||
271 | i2c_write_byte(Motor_Rechts); |
||
272 | break; |
||
273 | case 3: |
||
274 | i2c_write_byte(Motor_Links); |
||
275 | break; |
||
276 | } |
||
277 | break; |
||
278 | case 7: //2 Byte vom Motor lesen |
||
279 | motor_rx[motorread+4] = TWDR; |
||
280 | motorread++; |
||
281 | if (motorread>3) motorread=0; |
||
282 | i2c_stop(); |
||
283 | I2CTimeout = 10; |
||
284 | twi_state = 0; |
||
285 | } |
||
286 | #if 0 |
||
287 | break; |
||
288 | TriggerMagneticHeading++; |
||
289 | TriggerMagneticHeading %= 50; |
||
290 | TriggerSonicReading++; |
||
291 | TriggerSonicReading %= 200; |
||
292 | |||
293 | if (TriggerMagneticHeading == 1) |
||
294 | { |
||
295 | // Get Magnetic Heading |
||
296 | i2c_start(); |
||
297 | twi_state = 8; |
||
298 | break; |
||
299 | } |
||
300 | |||
301 | if (TriggerSonicReading == 1) |
||
302 | { |
||
303 | // Get Ultrasonic Reading |
||
304 | i2c_start(); |
||
305 | twi_state = 20; |
||
306 | break; |
||
307 | } |
||
308 | else if (TriggerSonicReading == 190) |
||
309 | { |
||
310 | // Get Ultrasonic Reading |
||
311 | i2c_start(); |
||
312 | twi_state = 30; |
||
313 | break; |
||
314 | } |
||
315 | else |
||
316 | { |
||
317 | I2CTimeout = 10; |
||
318 | twi_state = 0; |
||
319 | break; |
||
320 | } |
||
321 | |||
322 | case 8: |
||
323 | i2c_write_byte(0xC0); |
||
324 | break; |
||
325 | case 9: |
||
326 | i2c_write_byte(0x02); |
||
327 | break; |
||
328 | case 10: |
||
329 | i2c_start(); |
||
330 | break; |
||
331 | case 11: |
||
332 | i2c_write_byte(0xC1); |
||
333 | break; |
||
334 | case 12: |
||
335 | i2c_write_byte(MagneticHeading); |
||
336 | break; |
||
337 | case 13: |
||
338 | MagneticHeading = (0x0F & TWDR) << 8; |
||
339 | i2c_stop(); |
||
340 | i2c_start(); |
||
341 | break; |
||
342 | case 14: |
||
343 | i2c_write_byte(0xC0); |
||
344 | break; |
||
345 | case 15: |
||
346 | i2c_write_byte(0x03); |
||
347 | break; |
||
348 | case 16: |
||
349 | i2c_start(); |
||
350 | break; |
||
351 | case 17: |
||
352 | i2c_write_byte(0xC1); |
||
353 | break; |
||
354 | case 18: |
||
355 | i2c_write_byte(MagneticHeading); |
||
356 | break; |
||
357 | case 19: |
||
358 | MagneticHeading += TWDR; |
||
359 | i2c_stop(); |
||
360 | I2CTimeout = 22; |
||
361 | twi_state = 0; |
||
362 | break; |
||
363 | |||
364 | /* Trigger Ultrasonic Ping */ |
||
365 | case 20: /* Ping */ |
||
366 | i2c_write_byte(0xE0); /* Send I2C Adress */ |
||
367 | break; |
||
368 | case 21: |
||
369 | i2c_write_byte(0x00); /* Send I2C Register */ |
||
370 | break; |
||
371 | case 22: |
||
372 | i2c_write_byte(82); /* Send I2C Value */ |
||
373 | break; |
||
374 | case 24: |
||
375 | VersionID = TWDR; |
||
376 | i2c_stop(); |
||
377 | I2CTimeout = 22; |
||
378 | twi_state = 0; |
||
379 | UltrasonicPingCnt++; |
||
380 | break; |
||
381 | case 30: |
||
382 | i2c_write_byte(0xE0);/* Send I2C Adress */ |
||
383 | break; |
||
384 | case 31: |
||
385 | i2c_write_byte(0x02); /* Send I2C Register */ |
||
386 | break; |
||
387 | case 32: |
||
388 | i2c_start(); |
||
389 | break; |
||
390 | case 33: |
||
391 | i2c_write_byte(0xE1); |
||
392 | break; |
||
393 | case 34: |
||
394 | i2c_write_byte(255); |
||
395 | break; |
||
396 | case 35: |
||
397 | UltrasonicRange = TWDR << 8;/* Read I2C Value */ |
||
398 | UltrasonicRangeHigh = TWDR; |
||
399 | i2c_stop(); |
||
400 | i2c_start(); |
||
401 | break; |
||
402 | case 36: |
||
403 | i2c_write_byte(0xE0); |
||
404 | break; |
||
405 | case 37: |
||
406 | i2c_write_byte(0x03); |
||
407 | break; |
||
408 | case 38: |
||
409 | i2c_start(); |
||
410 | break; |
||
411 | case 39: |
||
412 | i2c_write_byte(0xE1); |
||
413 | break; |
||
414 | case 40: |
||
415 | i2c_write_byte(UltrasonicRangeLow); |
||
416 | break; |
||
417 | case 41: |
||
418 | UltrasonicRange += TWDR; |
||
419 | UltrasonicRangeLow = TWDR; |
||
420 | i2c_stop(); |
||
421 | I2CTimeout = 22; |
||
422 | twi_state = 0; |
||
423 | break; |
||
424 | } |
||
425 | #endif |
||
426 | TWCR |= 0x80; |
||
427 | } |