Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*############################################################################ |
2 | + Regler für Brushless-Motoren |
||
3 | + ATMEGA8 mit 8MHz |
||
4 | + (c) 01.2007 Holger Buss |
||
5 | + Nur für den privaten Gebrauch |
||
6 | + Keine Garantie auf Fehlerfreiheit |
||
7 | + Kommerzielle Nutzung nur mit meiner Zustimmung |
||
8 | + Der Code ist für die Hardware BL_Ctrl V1.0 entwickelt worden |
||
9 | + www.mikrocontroller.com |
||
10 | /############################################################################*/ |
||
11 | |||
12 | #include "main.h" |
||
13 | |||
14 | unsigned int PWM = 0; |
||
15 | unsigned int Strom = 0; //ca. in 0,1A |
||
16 | unsigned char Strom_max = 0; |
||
17 | unsigned char Mittelstrom = 0; |
||
18 | unsigned int Drehzahl = 0; // in 100UPM 60 = 6000 |
||
19 | unsigned int KommutierDelay = 10; |
||
20 | unsigned int I2C_Timeout = 0; |
||
23 | ingob | 21 | unsigned int SIO_Timeout = 0; |
1 | ingob | 22 | unsigned int SollDrehzahl = 0; |
23 | unsigned int IstDrehzahl = 0; |
||
24 | unsigned int DrehZahlTabelle[256];//vorberechnete Werte zur Drehzahlerfassung |
||
25 | unsigned char ZeitFuerBerechnungen = 1; |
||
26 | unsigned char MotorAnwerfen = 0; |
||
27 | unsigned char MotorGestoppt = 1; |
||
28 | unsigned char MaxPWM = MAX_PWM; |
||
29 | unsigned int CntKommutierungen = 0; |
||
30 | unsigned int SIO_Drehzahl = 0; |
||
31 | unsigned char ZeitZumAdWandeln = 1; |
||
24 | ingob | 32 | unsigned char MotorAdresse = 1; |
50 | holgerb | 33 | unsigned char PPM_Betrieb = 1; |
1 | ingob | 34 | |
35 | //############################################################################ |
||
36 | // |
||
37 | void SetPWM(void) |
||
38 | //############################################################################ |
||
39 | { |
||
40 | unsigned char tmp_pwm; |
||
41 | tmp_pwm = PWM; |
||
42 | if(tmp_pwm > MaxPWM) // Strombegrenzung |
||
43 | { |
||
44 | tmp_pwm = MaxPWM; |
||
45 | PORTC |= ROT; |
||
46 | } |
||
47 | if(Strom > MAX_STROM) // Strombegrenzung |
||
48 | { |
||
49 | OCR1A = 0; OCR1B = 0; OCR2 = 0; |
||
50 | PORTC |= ROT; |
||
51 | Strom--; |
||
52 | } |
||
53 | else |
||
54 | { |
||
55 | #ifdef _32KHZ |
||
56 | OCR1A = tmp_pwm; OCR1B = tmp_pwm; OCR2 = tmp_pwm; |
||
57 | #endif |
||
58 | |||
59 | #ifdef _16KHZ |
||
50 | holgerb | 60 | //OCR1A = 2 * (int)tmp_pwm; OCR1B = 2 * (int)tmp_pwm; OCR2 = tmp_pwm; |
61 | OCR1A = tmp_pwm; OCR1B = tmp_pwm; OCR2 = tmp_pwm; |
||
1 | ingob | 62 | #endif |
63 | } |
||
64 | } |
||
65 | |||
66 | //############################################################################ |
||
67 | // |
||
68 | void PWM_Init(void) |
||
69 | //############################################################################ |
||
70 | { |
||
71 | PWM_OFF; |
||
50 | holgerb | 72 | TCCR1B = (1 << CS10) | (0 << CS11) | (0 << CS12) | (0 << WGM12) | |
1 | ingob | 73 | (0 << WGM13) | (0<< ICES1) | (0 << ICNC1); |
50 | holgerb | 74 | /* TCCR1B = (1 << CS10) | (0 << CS11) | (0 << CS12) | (1 << WGM12) | |
75 | (0 << WGM13) | (0<< ICES1) | (0 << ICNC1); |
||
76 | */ |
||
1 | ingob | 77 | } |
78 | |||
79 | //############################################################################ |
||
80 | // |
||
81 | void Wait(unsigned char dauer) |
||
82 | //############################################################################ |
||
83 | { |
||
84 | dauer = (unsigned char)TCNT0 + dauer; |
||
85 | while((TCNT0 - dauer) & 0x80); |
||
86 | } |
||
87 | |||
88 | //############################################################################ |
||
89 | // |
||
90 | void Anwerfen(unsigned char pwm) |
||
91 | //############################################################################ |
||
92 | { |
||
93 | unsigned long timer = 300,i; |
||
94 | DISABLE_SENSE_INT; |
||
95 | PWM = 5; |
||
96 | SetPWM(); |
||
97 | Manuell(); |
||
98 | Delay_ms(200); |
||
99 | PWM = pwm; |
||
100 | while(1) |
||
101 | { |
||
102 | for(i=0;i<timer; i++) |
||
103 | { |
||
104 | if(!UebertragungAbgeschlossen) SendUart(); |
||
105 | else DatenUebertragung(); |
||
106 | Wait(100); // warten |
||
107 | } |
||
108 | timer-= timer/15+1; |
||
109 | if(timer < 25) { if(TEST_MANUELL) timer = 25; else return; } |
||
110 | |||
111 | Manuell(); |
||
112 | Phase++; |
||
113 | Phase %= 6; |
||
114 | AdConvert(); |
||
115 | PWM = pwm; |
||
116 | SetPWM(); |
||
117 | if(SENSE) |
||
118 | { |
||
119 | PORTD ^= GRUEN; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
50 | holgerb | 124 | /* |
125 | #define SENSE_A ADMUX = 0; |
||
126 | #define SENSE_B ADMUX = 1; |
||
127 | #define SENSE_C ADMUX = 2; |
||
128 | |||
129 | #define ClrSENSE ACSR |= 0x10 |
||
130 | #define SENSE ((ACSR & 0x10)) |
||
131 | #define SENSE_L (!(ACSR & 0x20)) |
||
132 | #define SENSE_H ((ACSR & 0x20)) |
||
133 | */ |
||
134 | |||
135 | void RotBlink(unsigned char anz) |
||
136 | { |
||
137 | sei(); // Interrupts ein |
||
138 | while(anz--) |
||
139 | { |
||
140 | PORTC |= ROT; |
||
141 | Delay_ms(300); |
||
142 | PORTC &= ~ROT; |
||
143 | Delay_ms(300); |
||
144 | } |
||
145 | Delay_ms(1000); |
||
146 | } |
||
147 | |||
148 | #define TEST_STROMGRENZE 100 |
||
149 | unsigned char DelayM(unsigned int timer) |
||
150 | { |
||
151 | while(timer--) |
||
152 | { |
||
153 | FastADConvert(); |
||
154 | if(Strom > TEST_STROMGRENZE) |
||
155 | { |
||
156 | STEUER_OFF; |
||
157 | return(1); |
||
158 | } |
||
159 | } |
||
160 | return(0); |
||
161 | } |
||
162 | |||
163 | unsigned char Delay(unsigned int timer) |
||
164 | { |
||
165 | while(timer--) |
||
166 | { |
||
167 | } |
||
168 | return(0); |
||
169 | } |
||
170 | |||
171 | |||
172 | void MotorTon(void) |
||
1 | ingob | 173 | //############################################################################ |
50 | holgerb | 174 | { |
175 | unsigned int timer = 300,i; |
||
176 | unsigned int t; |
||
177 | unsigned char MosfetKurzschluss = 0, MosfetOkay = 0,anz = 0; |
||
178 | DISABLE_SENSE_INT; |
||
179 | cli();//Globale Interrupts Ausschalten |
||
180 | PORTC &= ~ROT; |
||
181 | PWM = 0; |
||
182 | SetPWM(); |
||
183 | uart_putchar('\n'); |
||
184 | STEUER_OFF; |
||
185 | //#define STEUER_A_L {PORTD &= ~0x30; PORTD |= 0x08;} |
||
186 | //#define STEUER_B_L {PORTD &= ~0x28; PORTD |= 0x10;} |
||
187 | //#define STEUER_C_L {PORTD &= ~0x18; PORTD |= 0x20;} |
||
188 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
189 | //+ High-Mosfets auf Kurzschluss testen |
||
190 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
191 | Strom = 0; |
||
192 | PORTD |= 0x08; // Low A ein |
||
193 | PORTB |= 0x04; // High B ein; und A gegen B |
||
194 | //PORTB |= 0x08; // Test A |
||
195 | if(DelayM(3)) |
||
196 | { |
||
197 | MosfetKurzschluss = 0x01; |
||
198 | anz = 1; |
||
199 | uart_putchar('A'); |
||
200 | } |
||
201 | STEUER_OFF; |
||
202 | DelayM(100); |
||
203 | Strom = 0; |
||
204 | PORTD |= 0x10; // Low B ein |
||
205 | PORTB |= 0x02; // High C ein |
||
206 | //PORTB |= 0x04; // Test B |
||
207 | if(DelayM(3)) |
||
208 | { |
||
209 | anz = 2; |
||
210 | MosfetKurzschluss = 0x02; |
||
211 | uart_putchar('B'); |
||
212 | } |
||
213 | STEUER_OFF; |
||
214 | DelayM(100); |
||
215 | Strom = 0; |
||
216 | PORTD |= 0x20; // Low C ein |
||
217 | PORTB |= 0x04; // High B ein |
||
218 | //PORTB |= 0x0C; // High A+B ein |
||
219 | //PORTB |= 0x02; // Test C |
||
220 | if(DelayM(3)) |
||
221 | { |
||
222 | anz = 3; |
||
223 | MosfetKurzschluss = 0x04; |
||
224 | uart_putchar('C'); |
||
225 | } |
||
226 | STEUER_OFF; |
||
227 | DelayM(100); |
||
228 | PORTD |= 0x08; // Low A ein; und A gegen C |
||
229 | PORTB |= 0x02; // High C ein |
||
230 | //PORTB |= 0x08; // Test A |
||
231 | if(DelayM(3)) |
||
232 | { |
||
233 | anz = 3; |
||
234 | MosfetKurzschluss = 0x01; |
||
235 | uart_putchar('C'); |
||
236 | } |
||
237 | STEUER_OFF; |
||
238 | DelayM(100); |
||
239 | if(anz) while(1) RotBlink(anz); |
||
240 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
241 | //+ Low-Mosfets auf Kurzschluss testen |
||
242 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
243 | Strom = 0; |
||
244 | PORTB |= 0x08; // Test A |
||
245 | //PORTD |= 0x08; // Low A ein |
||
246 | if(DelayM(3)) |
||
247 | { |
||
248 | anz = 4; |
||
249 | MosfetKurzschluss = 0x01; |
||
250 | uart_putchar('a'); |
||
251 | } |
||
252 | STEUER_OFF; |
||
253 | Strom = 0; |
||
254 | PORTB |= 0x04; // Test B |
||
255 | //PORTD |= 0x10; // Low B ein |
||
256 | if(DelayM(3)) |
||
257 | { |
||
258 | anz = 5; |
||
259 | MosfetKurzschluss = 0x02; |
||
260 | uart_putchar('b'); |
||
261 | } |
||
262 | STEUER_OFF; |
||
263 | Strom = 0; |
||
264 | PORTB |= 0x02; // Test C |
||
265 | //PORTD |= 0x20; // Low C ein |
||
266 | if(DelayM(3)) |
||
267 | { |
||
268 | anz = 6; |
||
269 | MosfetKurzschluss = 0x04; |
||
270 | uart_putchar('c'); |
||
271 | } |
||
272 | STEUER_OFF; |
||
273 | |||
274 | if(anz) while(1) RotBlink(anz); |
||
275 | |||
276 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
277 | //+ High-Mosfets auf Schalten testen |
||
278 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
279 | SENSE_A; |
||
280 | STEUER_OFF; |
||
281 | PORTD |= 0x10; // Low B ein |
||
282 | PORTD |= 0x20; // Low C ein |
||
283 | Strom = 0; |
||
284 | #define TONDAUER 100000 |
||
285 | #define SOUND_E 5 |
||
286 | #define SOUND2_A 500 |
||
287 | for(i=0; i< (TONDAUER / SOUND2_A) ; i++) |
||
288 | { |
||
289 | PORTB |= 0x08; // Test A |
||
290 | Delay(SOUND_E); |
||
291 | if(SENSE_L) { MosfetOkay |= 0x01; } else { MosfetOkay &= ~0x01;} |
||
292 | PORTB = 0; |
||
293 | Delay(SOUND2_A); |
||
294 | } |
||
295 | STEUER_OFF; |
||
296 | SENSE_B; |
||
297 | PORTD |= 0x08; // Low A ein |
||
298 | PORTD |= 0x20; // Low C ein |
||
299 | #define SOUND1_A 600 |
||
300 | for(i=0; i<(TONDAUER / SOUND1_A); i++) |
||
301 | { |
||
302 | PORTB |= 0x04; // Test B |
||
303 | Delay(SOUND_E); |
||
304 | if(SENSE_L) { MosfetOkay |= 0x02; } else { MosfetOkay &= ~0x02;} |
||
305 | PORTB = 0; |
||
306 | Delay(SOUND1_A); |
||
307 | } |
||
308 | |||
309 | STEUER_OFF; |
||
310 | SENSE_C; |
||
311 | PORTD |= 0x08; // Low A ein |
||
312 | PORTD |= 0x10; // Low B ein |
||
313 | #define SOUND3_A 700 |
||
314 | for(i=0; i<(TONDAUER / SOUND3_A); i++) |
||
315 | { |
||
316 | PORTB |= 0x02; // Test C |
||
317 | Delay(SOUND_E); |
||
318 | if(SENSE_L) { MosfetOkay |= 0x04; } else { MosfetOkay &= ~0x04;} |
||
319 | PORTB = 0; |
||
320 | Delay(SOUND3_A); |
||
321 | } |
||
322 | STEUER_OFF; |
||
323 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
324 | //+ Low-Mosfets auf Schalten testen |
||
325 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
326 | SENSE_B; |
||
327 | PORTD |= 0x08; // Low A ein |
||
328 | #define SOUND2_A 500 |
||
329 | for(i=0; i< (TONDAUER / SOUND2_A) ; i++) |
||
330 | { |
||
331 | PORTB |= 0x04; // Test B |
||
332 | Delay(SOUND_E); |
||
333 | if(SENSE_L) { MosfetOkay |= 0x08; } else { MosfetOkay &= ~0x08;} |
||
334 | PORTB = 0; |
||
335 | Delay(SOUND2_A); |
||
336 | } |
||
337 | |||
338 | STEUER_OFF; |
||
339 | SENSE_C; |
||
340 | PORTD |= 0x10; // Low B ein |
||
341 | for(i=0; i<(TONDAUER / SOUND3_A); i++) |
||
342 | { |
||
343 | PORTB |= 0x02; // Test C |
||
344 | Delay(SOUND_E); |
||
345 | if(SENSE_L) { MosfetOkay |= 0x10; } else { MosfetOkay &= ~0x10;} |
||
346 | PORTB = 0; |
||
347 | Delay(SOUND3_A); |
||
348 | } |
||
349 | |||
350 | STEUER_OFF; |
||
351 | SENSE_B; |
||
352 | PORTD |= 0x20; // Low C ein |
||
353 | for(i=0; i<(TONDAUER / SOUND1_A); i++) |
||
354 | { |
||
355 | PORTB |= 0x04; // Test B |
||
356 | Delay(SOUND_E); |
||
357 | if(SENSE_L) { MosfetOkay |= 0x20; } else { MosfetOkay &= ~0x20;} |
||
358 | PORTB = 0; |
||
359 | Delay(SOUND1_A); |
||
360 | } |
||
361 | |||
362 | sei();//Globale Interrupts Einschalten |
||
363 | Delay_ms(250 * MotorAdresse); |
||
364 | |||
365 | #define SOUND8_E 7 |
||
366 | #define SOUND8_A 650 |
||
367 | for(i=0; i<(TONDAUER / SOUND8_A); i++) |
||
368 | { |
||
369 | for(timer = 0; timer < SOUND8_E; timer++) PORTB |= 0x04; // Test B |
||
370 | for(timer = 0; timer < SOUND8_A; timer++) PORTB = 0; |
||
371 | } |
||
372 | |||
373 | if(!(MosfetOkay & 0x01)) { anz = 1; uart_putchar('A'); } else |
||
374 | if(!(MosfetOkay & 0x02)) { anz = 2; uart_putchar('B'); } else |
||
375 | if(!(MosfetOkay & 0x04)) { anz = 3; uart_putchar('C'); } else |
||
376 | if(!(MosfetOkay & 0x08)) { anz = 4; uart_putchar('a'); } else |
||
377 | if(!(MosfetOkay & 0x10)) { anz = 5; uart_putchar('b'); } else |
||
378 | if(!(MosfetOkay & 0x20)) { anz = 6; uart_putchar('c'); } |
||
379 | Delay_ms(250 * (4-MotorAdresse)); |
||
380 | |||
381 | if(MosfetOkay != 0x3f) Delay_ms(1000); |
||
382 | |||
383 | #define TONDAUER 80000 |
||
384 | #define SOUND9_E 20 |
||
385 | #define SOUND9_A 500 |
||
386 | |||
387 | while(anz--) |
||
388 | { |
||
389 | PORTC |= ROT; |
||
390 | if(!(MosfetOkay & 0x04) || !(MosfetOkay & 0x20)) // Schalter C defekt |
||
391 | { |
||
392 | PORTD |= 0x10; // Low B ein |
||
393 | for(i=0; i< (TONDAUER / SOUND9_A) ; i++) |
||
394 | { |
||
395 | for(timer = 0; timer < SOUND9_E; timer++) PORTB |= 0x08; // Test A |
||
396 | for(timer = 0; timer < SOUND9_A; timer++) PORTB = 0; |
||
397 | } |
||
398 | } |
||
399 | else |
||
400 | if(!(MosfetOkay & 0x02) || !(MosfetOkay & 0x10)) // Schalter B defekt |
||
401 | { |
||
402 | PORTD |= 0x20; // Low C ein |
||
403 | for(i=0; i< (TONDAUER / SOUND9_A) ; i++) |
||
404 | { |
||
405 | for(timer = 0; timer < SOUND9_E; timer++) PORTB |= 0x08; // Test A |
||
406 | for(timer = 0; timer < SOUND9_A; timer++) PORTB = 0; |
||
407 | } |
||
408 | } |
||
409 | else |
||
410 | if(!(MosfetOkay & 0x01) || !(MosfetOkay & 0x08)) // Schalter A defekt |
||
411 | { |
||
412 | PORTD |= 0x20; // Low C ein |
||
413 | for(i=0; i< (TONDAUER / SOUND9_A) ; i++) |
||
414 | { |
||
415 | for(timer = 0; timer < SOUND9_E; timer++) PORTB |= 0x04; // Test B |
||
416 | for(timer = 0; timer < SOUND9_A; timer++) PORTB = 0; |
||
417 | } |
||
418 | } |
||
419 | PORTC &= ~ROT; |
||
420 | Delay_ms(300); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | //############################################################################ |
||
1 | ingob | 425 | // |
426 | unsigned char SollwertErmittlung(void) |
||
427 | //############################################################################ |
||
428 | { |
||
429 | static unsigned int sollwert = 0; |
||
430 | unsigned int ppm; |
||
431 | if(!I2C_Timeout) // bei Erreichen von 0 ist der Wert ungültig |
||
432 | { |
||
433 | if(SIO_Timeout) // es gibt gültige SIO-Daten |
||
434 | { |
||
50 | holgerb | 435 | sollwert = (MAX_PWM * (unsigned int) SIO_Sollwert) / 200; // skalieren auf 0-200 = 0-255 |
436 | PPM_Betrieb = 0; |
||
437 | ICP_INT_DISABLE; |
||
438 | PORTC &= ~ROT; |
||
1 | ingob | 439 | } |
440 | else |
||
50 | holgerb | 441 | if(anz_ppm_werte > 20) // es gibt gültige PPM-Daten |
1 | ingob | 442 | { |
50 | holgerb | 443 | PPM_Betrieb = 1; |
1 | ingob | 444 | ppm = PPM_Signal; |
445 | if(ppm > 300) ppm = 0; // ungültiges Signal |
||
446 | if(ppm > 200) ppm = 200; |
||
447 | if(ppm <= MIN_PPM) sollwert = 0; |
||
448 | else |
||
449 | { |
||
450 | sollwert = (int) MIN_PWM + ((MAX_PWM - MIN_PWM) * (ppm - MIN_PPM)) / (190 - MIN_PPM); |
||
451 | } |
||
452 | PORTC &= ~ROT; |
||
453 | } |
||
454 | else // Kein gültiger Sollwert |
||
455 | { |
||
50 | holgerb | 456 | if(!TEST_SCHUB) { if(sollwert) sollwert--; } |
457 | PORTC |= ROT; |
||
1 | ingob | 458 | } |
459 | } |
||
460 | else // I2C-Daten sind gültig |
||
461 | { |
||
462 | sollwert = I2C_RXBuffer; |
||
50 | holgerb | 463 | PPM_Betrieb = 0; |
1 | ingob | 464 | PORTC &= ~ROT; |
50 | holgerb | 465 | ICP_INT_DISABLE; |
1 | ingob | 466 | } |
467 | if(sollwert > MAX_PWM) sollwert = MAX_PWM; |
||
468 | return(sollwert); |
||
469 | } |
||
470 | |||
471 | void DebugAusgaben(void) |
||
472 | { |
||
473 | DebugOut.Analog[0] = Strom; |
||
474 | DebugOut.Analog[1] = Mittelstrom; |
||
475 | DebugOut.Analog[2] = SIO_Drehzahl; |
||
476 | DebugOut.Analog[3] = PPM_Signal; |
||
477 | } |
||
478 | |||
479 | |||
480 | //############################################################################ |
||
481 | //Hauptprogramm |
||
482 | int main (void) |
||
483 | //############################################################################ |
||
484 | { |
||
485 | char altPhase = 0; |
||
486 | int test = 0; |
||
487 | unsigned int MinUpmPulse,Blink,TestschubTimer; |
||
488 | unsigned int Blink2,MittelstromTimer,DrehzahlMessTimer,MotorGestopptTimer; |
||
489 | |||
490 | DDRC = 0x08; |
||
491 | PORTC = 0x08; |
||
492 | DDRD = 0xBA; |
||
26 | ingob | 493 | PORTD = 0x00; |
1 | ingob | 494 | DDRB = 0x0E; |
495 | PORTB = 0x31; |
||
24 | ingob | 496 | |
497 | #if (MOTORADRESSE == 0) |
||
498 | PORTB |= (ADR1 + ADR2); // Pullups für Adresswahl |
||
43 | holgerb | 499 | for(test=0;test<500;test++); |
24 | ingob | 500 | if (PINB & ADR1) |
501 | { |
||
502 | if (PINB & ADR2) MotorAdresse = 1; |
||
503 | else MotorAdresse = 2; |
||
504 | } |
||
505 | else |
||
506 | { |
||
507 | if (PINB & ADR2) MotorAdresse = 3; |
||
508 | else MotorAdresse = 4; |
||
509 | } |
||
510 | #else |
||
511 | MotorAdresse = MOTORADRESSE; |
||
512 | #endif |
||
1 | ingob | 513 | |
514 | UART_Init(); |
||
515 | Timer0_Init(); |
||
50 | holgerb | 516 | sei();//Globale Interrupts Einschalten |
1 | ingob | 517 | |
518 | // Am Blinken erkennt man die richtige Motoradresse |
||
50 | holgerb | 519 | /* |
1 | ingob | 520 | for(test=0;test<5;test++) |
521 | { |
||
24 | ingob | 522 | if(test == MotorAdresse) PORTD |= GRUEN; |
1 | ingob | 523 | Delay_ms(150); |
524 | PORTD &= ~GRUEN; |
||
525 | Delay_ms(250); |
||
526 | } |
||
527 | |||
528 | Delay_ms(500); |
||
50 | holgerb | 529 | */ |
18 | ingob | 530 | // UART_Init(); // war doppelt |
1 | ingob | 531 | PWM_Init(); |
532 | |||
533 | InitIC2_Slave(0x50); |
||
534 | InitPPM(); |
||
535 | |||
536 | Blink = SetDelay(101); |
||
537 | Blink2 = SetDelay(102); |
||
538 | MinUpmPulse = SetDelay(103); |
||
539 | MittelstromTimer = SetDelay(254); |
||
540 | DrehzahlMessTimer = SetDelay(1005); |
||
541 | TestschubTimer = SetDelay(1006); |
||
50 | holgerb | 542 | while(!CheckDelay(MinUpmPulse)) |
543 | { |
||
544 | if(SollwertErmittlung()) break; |
||
545 | } |
||
546 | ; |
||
1 | ingob | 547 | PORTD |= GRUEN; |
548 | PWM = 0; |
||
549 | |||
550 | SetPWM(); |
||
551 | |||
552 | SFIOR = 0x08; // Analog Comperator ein |
||
553 | ADMUX = 1; |
||
554 | |||
555 | MinUpmPulse = SetDelay(10); |
||
556 | DebugOut.Analog[1] = 1; |
||
557 | PPM_Signal = 0; |
||
558 | |||
50 | holgerb | 559 | if(!SollwertErmittlung()) MotorTon(); |
560 | |||
1 | ingob | 561 | // zum Test der Hardware; Motor dreht mit konstanter Drehzahl ohne Regelung |
562 | if(TEST_MANUELL) Anwerfen(TEST_MANUELL); // kommt von dort nicht wieder |
||
563 | |||
564 | while (1) |
||
565 | { |
||
566 | if(!TEST_SCHUB) PWM = SollwertErmittlung(); |
||
567 | //I2C_TXBuffer = PWM; // Antwort über I2C-Bus |
||
568 | if(MANUELL_PWM) PWM = MANUELL_PWM; |
||
569 | |||
570 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
571 | if(Phase != altPhase) // es gab eine Kommutierung im Interrupt |
||
572 | { |
||
573 | MotorGestoppt = 0; |
||
574 | ZeitFuerBerechnungen = 0; // direkt nach einer Kommutierung ist Zeit |
||
575 | MinUpmPulse = SetDelay(50); // Timeout, falls ein Motor stehen bleibt |
||
576 | altPhase = Phase; |
||
577 | } |
||
578 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
579 | if(!PWM) // Sollwert == 0 |
||
580 | { |
||
581 | MotorAnwerfen = 0; // kein Startversuch |
||
582 | ZeitFuerBerechnungen = 0; |
||
583 | // nach 1,5 Sekunden den Motor als gestoppt betrachten |
||
18 | ingob | 584 | if(CheckDelay(MotorGestopptTimer)) |
1 | ingob | 585 | { |
586 | DISABLE_SENSE_INT; |
||
587 | MotorGestoppt = 1; |
||
588 | STEUER_OFF; |
||
589 | } |
||
590 | } |
||
591 | else |
||
592 | { |
||
593 | if(MotorGestoppt) MotorAnwerfen = 1; // Startversuch |
||
594 | MotorGestopptTimer = SetDelay(1500); |
||
595 | } |
||
596 | |||
597 | if(MotorGestoppt && !TEST_SCHUB) PWM = 0; |
||
598 | SetPWM(); |
||
599 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
600 | if(!ZeitFuerBerechnungen++) |
||
601 | { |
||
602 | if(MotorGestoppt) PORTD |= GRUEN; //else PORTD &= ~GRUEN; |
||
603 | if(SIO_DEBUG) |
||
604 | { |
||
605 | DebugAusgaben(); // welche Werte sollen angezeigt werden? |
||
606 | if(!UebertragungAbgeschlossen) SendUart(); |
||
607 | else DatenUebertragung(); |
||
608 | } |
||
609 | // Berechnen des Mittleren Stroms zur (langsamen) Strombegrenzung |
||
610 | if(CheckDelay(MittelstromTimer)) |
||
611 | { |
||
612 | MittelstromTimer = SetDelay(50); // alle 50ms |
||
613 | if(Mittelstrom < Strom) Mittelstrom++;// Mittelwert des Stroms bilden |
||
614 | else if(Mittelstrom > Strom) Mittelstrom--; |
||
615 | |||
616 | if(Mittelstrom > LIMIT_STROM)// Strom am Limit? |
||
617 | { |
||
618 | MaxPWM--;// dann die Maximale PWM herunterfahren |
||
619 | PORTC |= ROT; |
||
620 | } |
||
621 | else |
||
622 | { |
||
623 | if(MaxPWM < MAX_PWM) MaxPWM++; |
||
624 | } |
||
625 | } |
||
626 | |||
627 | if(CheckDelay(DrehzahlMessTimer)) // Ist-Drehzahl bestimmen |
||
628 | { |
||
629 | DrehzahlMessTimer = SetDelay(10); |
||
630 | SIO_Drehzahl = (6 * CntKommutierungen) / (POLANZAHL / 2); |
||
631 | CntKommutierungen = 0; |
||
632 | if(PPM_Timeout == 0) // keine PPM-Signale |
||
633 | ZeitZumAdWandeln = 1; |
||
634 | } |
||
635 | |||
636 | |||
637 | if(CheckDelay(TestschubTimer)) |
||
638 | { |
||
639 | TestschubTimer = SetDelay(1500); |
||
640 | if(TEST_SCHUB) |
||
641 | { |
||
642 | switch(test) |
||
643 | { |
||
644 | case 0: PWM = 50; test++; break; |
||
645 | case 1: PWM = 130; test++; break; |
||
646 | case 2: PWM = 60; test++; break; |
||
647 | case 3: PWM = 140; test++; break; |
||
648 | case 4: PWM = 150; test = 0; break; |
||
649 | default: test = 0; |
||
650 | } |
||
651 | } |
||
652 | } |
||
653 | // Motor Stehen geblieben |
||
654 | if((CheckDelay(MinUpmPulse) && SIO_Drehzahl == 0) || MotorAnwerfen) |
||
655 | { |
||
656 | MotorGestoppt = 1; |
||
657 | DISABLE_SENSE_INT; |
||
658 | MinUpmPulse = SetDelay(100); |
||
659 | if(MotorAnwerfen) |
||
660 | { |
||
661 | PORTC &= ~ROT; |
||
662 | MotorAnwerfen = 0; |
||
663 | Anwerfen(10); |
||
664 | PORTD |= GRUEN; |
||
665 | MotorGestoppt = 0; |
||
666 | Phase--; |
||
667 | PWM = 1; |
||
668 | SetPWM(); |
||
669 | SENSE_TOGGLE_INT; |
||
670 | ENABLE_SENSE_INT; |
||
43 | holgerb | 671 | MinUpmPulse = SetDelay(20); |
1 | ingob | 672 | while(!CheckDelay(MinUpmPulse)); // kurz Synchronisieren |
43 | holgerb | 673 | PWM = 15; |
1 | ingob | 674 | SetPWM(); |
43 | holgerb | 675 | MinUpmPulse = SetDelay(300); |
1 | ingob | 676 | while(!CheckDelay(MinUpmPulse)); // kurz Durchstarten |
18 | ingob | 677 | |
678 | // Drehzahlmessung wieder aufsetzen |
||
679 | DrehzahlMessTimer = SetDelay(50); |
||
1 | ingob | 680 | altPhase = 7; |
681 | } |
||
682 | } |
||
683 | } // ZeitFuerBerechnungen |
||
684 | } // while(1) - Hauptschleife |
||
685 | } |
||
686 |