Subversion Repositories BL-Ctrl

Rev

Rev 72 | Rev 76 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 72 Rev 73
Line 37... Line 37...
37
// +     from this software without specific prior written permission.
37
// +     from this software without specific prior written permission.
38
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
38
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet 
39
// +     for non-commercial use (directly or indirectly)
39
// +     for non-commercial use (directly or indirectly)
40
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
40
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted 
41
// +     with our written permission
41
// +     with our written permission
42
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be 
42
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be 
43
// +     clearly linked as origin 
43
// +     clearly linked as origin 
44
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
44
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Line 74... Line 74...
74
unsigned int  CntKommutierungen = 0;
74
unsigned int  CntKommutierungen = 0;
75
unsigned int  SIO_Drehzahl = 0;
75
unsigned int  SIO_Drehzahl = 0;
76
unsigned char ZeitZumAdWandeln = 1;
76
unsigned char ZeitZumAdWandeln = 1;
77
unsigned char MotorAdresse = 1;
77
unsigned char MotorAdresse = 1;
78
unsigned char PPM_Betrieb = 1;
78
unsigned char PPM_Betrieb = 1;
79
 
-
 
-
 
79
unsigned char HwVersion;
-
 
80
unsigned char IntRef = 0;
-
 
81
unsigned int MinUpmPulse;
80
//############################################################################
82
//############################################################################
81
//
83
//
82
void SetPWM(void)
84
void SetPWM(void)
83
//############################################################################
85
//############################################################################
84
{
86
{
Line 89... Line 91...
89
        tmp_pwm = MaxPWM;
91
        tmp_pwm = MaxPWM;
90
        PORTC |= ROT;
92
        PORTC |= ROT;
91
        }
93
        }
92
    if(Strom > MAX_STROM)   // Strombegrenzung
94
    if(Strom > MAX_STROM)   // Strombegrenzung
93
        {
95
        {
94
        OCR1A = 0; OCR1B = 0; OCR2  = 0;
96
        OCR1A = 0; OCR1B = 0; OCR2 = 0;
-
 
97
        PORTD &= ~0x38;
95
        PORTC |= ROT;
98
        PORTC |= ROT;
-
 
99
        DebugOut.Analog[6]++;
96
        Strom--;
100
        Strom--;
97
        }
101
        }
98
    else
102
    else
99
        {
103
        {
100
        #ifdef  _32KHZ 
104
        #ifdef  _32KHZ 
Line 106... Line 110...
106
        OCR1A =  tmp_pwm; OCR1B =  tmp_pwm; OCR2  = tmp_pwm;
110
        OCR1A =  tmp_pwm; OCR1B =  tmp_pwm; OCR2  = tmp_pwm;
107
        #endif 
111
        #endif 
108
        }
112
        }
109
}
113
}
Line -... Line 114...
-
 
114
 
-
 
115
void DebugAusgaben(void)
-
 
116
{
-
 
117
    DebugOut.Analog[0] = Strom;
-
 
118
    DebugOut.Analog[1] = Mittelstrom;
-
 
119
    DebugOut.Analog[2] = SIO_Drehzahl;
-
 
120
    DebugOut.Analog[3] = PPM_Signal;
-
 
121
    DebugOut.Analog[4] = OCR2;
-
 
122
//    DebugOut.Analog[5] = PWM;
-
 
123
}
110
 
124
 
111
//############################################################################
125
//############################################################################
112
//
126
//
113
void PWM_Init(void)
127
void PWM_Init(void)
114
//############################################################################
128
//############################################################################
Line 128... Line 142...
128
{
142
{
129
    dauer = (unsigned char)TCNT0 + dauer;
143
    dauer = (unsigned char)TCNT0 + dauer;
130
    while((TCNT0 - dauer) & 0x80);
144
    while((TCNT0 - dauer) & 0x80);
131
}
145
}
Line -... Line 146...
-
 
146
 
-
 
147
void RotBlink(unsigned char anz)
-
 
148
{
-
 
149
sei(); // Interrupts ein
-
 
150
 while(anz--)
-
 
151
  {
-
 
152
   PORTC |= ROT;
-
 
153
   Delay_ms(300);    
-
 
154
   PORTC &= ~ROT;
-
 
155
   Delay_ms(300);    
-
 
156
  }
-
 
157
   Delay_ms(1000);    
-
 
158
}
132
 
159
 
133
//############################################################################
160
//############################################################################
134
//
161
//
135
void Anwerfen(unsigned char pwm)
162
char Anwerfen(unsigned char pwm)
136
//############################################################################
163
//############################################################################
137
{
164
{
138
    unsigned long timer = 300,i;
165
    unsigned long timer = 300,i;
139
    DISABLE_SENSE_INT;
166
    DISABLE_SENSE_INT;
140
    PWM = 5;
167
    PWM = 5;
141
    SetPWM();
168
    SetPWM();
142
    Manuell();
169
    Manuell();
-
 
170
//    Delay_ms(200);
-
 
171
                    MinUpmPulse = SetDelay(300);
-
 
172
                    while(!CheckDelay(MinUpmPulse))
-
 
173
                    {
-
 
174
                     FastADConvert();
-
 
175
                      if(Strom > 120)
-
 
176
                      {
-
 
177
                        STEUER_OFF; // Abschalten wegen Kurzschluss
-
 
178
                        RotBlink(10);
-
 
179
                        return(0);
-
 
180
                      }  
143
    Delay_ms(200);
181
                    }
144
    PWM = pwm;
182
    PWM = pwm;
145
    while(1)
183
    while(1)
146
        {
184
        {
147
        for(i=0;i<timer; i++)
185
        for(i=0;i<timer; i++)
148
            {
186
            {
149
            if(!UebertragungAbgeschlossen)  SendUart();
187
            if(!UebertragungAbgeschlossen)  SendUart();
150
            else DatenUebertragung();
188
            else DatenUebertragung();
151
            Wait(100);  // warten
189
            Wait(100);  // warten
-
 
190
            }
-
 
191
        DebugAusgaben();
-
 
192
        FastADConvert();
-
 
193
        if(Strom > 60)
-
 
194
          {
-
 
195
            STEUER_OFF; // Abschalten wegen Kurzschluss
-
 
196
            RotBlink(10);
-
 
197
            return(0);
-
 
198
          }  
152
            }
199
         
153
        timer-= timer/15+1;
200
        timer-= timer/15+1;
154
        if(timer < 25) { if(TEST_MANUELL) timer = 25; else return; }
-
 
155
 
201
        if(timer < 25) { if(TEST_MANUELL) timer = 25; else return(1); }
156
        Manuell();
202
        Manuell();
157
        Phase++;
203
        Phase++;
158
        Phase %= 6;
204
        Phase %= 6;
159
        AdConvert();
205
        AdConvert();
Line 175... Line 221...
175
#define SENSE               ((ACSR & 0x10))
221
#define SENSE               ((ACSR & 0x10))
176
#define SENSE_L             (!(ACSR & 0x20))
222
#define SENSE_L             (!(ACSR & 0x20))
177
#define SENSE_H             ((ACSR & 0x20))
223
#define SENSE_H             ((ACSR & 0x20))
178
*/
224
*/
Line 179... Line -...
179
 
-
 
180
void RotBlink(unsigned char anz)
-
 
181
{
-
 
182
sei(); // Interrupts ein
-
 
183
 while(anz--)
-
 
184
  {
-
 
185
   PORTC |= ROT;
-
 
186
   Delay_ms(300);    
-
 
187
   PORTC &= ~ROT;
-
 
188
   Delay_ms(300);    
-
 
189
  }
-
 
190
   Delay_ms(1000);    
-
 
Line 191... Line 225...
191
}
225
 
192
 
226
 
193
#define TEST_STROMGRENZE 120
227
#define TEST_STROMGRENZE 120
194
unsigned char DelayM(unsigned int timer)
228
unsigned char DelayM(unsigned int timer)
Line 304... Line 338...
304
  FETS_OFF;
338
  FETS_OFF;
305
  Delay(5);
339
  Delay(5);
306
  HIGH_A_EIN;
340
  HIGH_A_EIN;
307
  DelayM(1);
341
  DelayM(1);
308
  FETS_OFF;
342
  FETS_OFF;
309
  if(Strom > grenze + RuheStrom) {anz = 4; uart_putchar('4'); break;}
343
  if(Strom > grenze + RuheStrom) {anz = 4; uart_putchar('4'); FETS_OFF; break;}
310
  Delay(5);
344
  Delay(5);
311
 }
345
 }
312
 Delay(10000);
346
 Delay(10000);
Line 313... Line 347...
313
 
347
 
Line 319... Line 353...
319
  FETS_OFF;
353
  FETS_OFF;
320
  Delay(5);
354
  Delay(5);
321
  HIGH_B_EIN;
355
  HIGH_B_EIN;
322
  DelayM(1);
356
  DelayM(1);
323
  FETS_OFF;
357
  FETS_OFF;
324
  if(Strom > grenze + RuheStrom) {anz = 5; uart_putchar('5'); break;}
358
  if(Strom > grenze + RuheStrom) {anz = 5; uart_putchar('5'); FETS_OFF;break;}
325
  Delay(5);
359
  Delay(5);
326
 }
360
 }
Line 327... Line 361...
327
 
361
 
328
 Strom = 0;
362
 Strom = 0;
Line 335... Line 369...
335
  FETS_OFF;
369
  FETS_OFF;
336
  Delay(5);
370
  Delay(5);
337
  HIGH_C_EIN;
371
  HIGH_C_EIN;
338
  DelayM(1);
372
  DelayM(1);
339
  FETS_OFF;
373
  FETS_OFF;
340
  if(Strom > grenze + RuheStrom) {anz = 6; uart_putchar('6'); break;}
374
  if(Strom > grenze + RuheStrom) {anz = 6; uart_putchar('6'); FETS_OFF; break;}
341
  Delay(5);
375
  Delay(5);
342
 }
376
 }
Line -... Line 377...
-
 
377
 
-
 
378
 if(anz) while(1) RotBlink(anz);  // bei Kurzschluss nicht starten
343
 
379
 
344
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
380
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
345
//+ High-Mosfets auf Schalten testen
381
//+ High-Mosfets auf Schalten testen
346
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
382
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
347
    SENSE_A;
383
    SENSE_A;
Line 447... Line 483...
447
 if(!(MosfetOkay & 0x04))  { anz = 3; UDR='C'; } else
483
 if(!(MosfetOkay & 0x04))  { anz = 3; UDR='C'; } else
448
 if(!(MosfetOkay & 0x08))  { anz = 4; UDR='a'; } else
484
 if(!(MosfetOkay & 0x08))  { anz = 4; UDR='a'; } else
449
 if(!(MosfetOkay & 0x10))  { anz = 5; UDR='b'; } else
485
 if(!(MosfetOkay & 0x10))  { anz = 5; UDR='b'; } else
450
 if(!(MosfetOkay & 0x20))  { anz = 6; UDR='c'; }  
486
 if(!(MosfetOkay & 0x20))  { anz = 6; UDR='c'; }  
Line 451... Line 487...
451
 
487
 
452
 if(anz) Delay_ms(1000);
-
 
-
 
488
// if(anz) Delay_ms(1000); 
453
 
489
 if(anz) while(1) RotBlink(anz);  // bei Kurzschluss nicht starten
454
 RotBlink(anz);
490
 RotBlink(anz);
455
 uart_putchar('.');
491
 uart_putchar('.');
Line 456... Line 492...
456
}
492
}
Line 500... Line 536...
500
        }
536
        }
501
    if(sollwert > MAX_PWM) sollwert = MAX_PWM;
537
    if(sollwert > MAX_PWM) sollwert = MAX_PWM;
502
    return(sollwert);
538
    return(sollwert);
503
}
539
}
Line 504... Line -...
504
 
-
 
505
void DebugAusgaben(void)
-
 
506
{
-
 
507
    DebugOut.Analog[0] = Strom;
-
 
508
    DebugOut.Analog[1] = Mittelstrom;
-
 
509
    DebugOut.Analog[2] = SIO_Drehzahl;
-
 
510
    DebugOut.Analog[3] = PPM_Signal;
-
 
511
    DebugOut.Analog[4] = OCR2;
-
 
Line 512... Line 540...
512
}
540
 
513
 
541
 
514
 
542
 
515
//############################################################################
543
//############################################################################
516
//Hauptprogramm
544
//Hauptprogramm
517
int main (void)
545
int main (void)
518
//############################################################################
546
//############################################################################
519
{
547
{
520
    char altPhase = 0;
548
    char altPhase = 0;
Line 521... Line 549...
521
    int test = 0;
549
    int test = 0;
522
    unsigned int MinUpmPulse,Blink,TestschubTimer;
550
    unsigned int Blink,TestschubTimer;
523
    unsigned int Blink2,MittelstromTimer,DrehzahlMessTimer,MotorGestopptTimer;
551
    unsigned int Blink2,MittelstromTimer,DrehzahlMessTimer,MotorGestopptTimer;
524
 
552
 
525
    DDRC  = 0x08;
553
    DDRC  = 0x08;
526
    PORTC = 0x08;
554
    PORTC = 0x08;
Line 527... Line 555...
527
    DDRD  = 0xBA;
555
    DDRD  = 0x3A;
528
    PORTD = 0x00;
556
    PORTD = 0x00;
529
    DDRB  = 0x0E;
557
    DDRB  = 0x0E;
530
    PORTB = 0x31;
558
    PORTB = 0x31;
531
       
559
       
532
#if (MOTORADRESSE == 0)
560
#if (MOTORADRESSE == 0)
533
    PORTB |= (ADR1 + ADR2);   // Pullups für Adresswahl
561
    PORTB |= (ADR1 + ADR2);   // Pullups für Adresswahl
534
    for(test=0;test<500;test++);
562
    for(test=0;test<500;test++);
535
    if (PINB & ADR1)
563
    if(PINB & ADR1)
536
         {
564
         {
537
           if (PINB & ADR2) MotorAdresse = 1;
565
           if (PINB & ADR2) MotorAdresse = 1;
538
            else MotorAdresse = 2;
566
            else MotorAdresse = 2;
539
         }
567
         }
-
 
568
         else
540
         else
569
         {
541
         {
570
           if (PINB & ADR2) MotorAdresse = 3;
-
 
571
            else MotorAdresse = 4;
542
           if (PINB & ADR2) MotorAdresse = 3;
572
         }
543
            else MotorAdresse = 4;
-
 
-
 
573
    HwVersion = 11;
-
 
574
#else
544
         }
575
    MotorAdresse  = MOTORADRESSE;
545
#else
576
    HwVersion = 10;
546
    MotorAdresse  = MOTORADRESSE;
577
#endif
Line 547... Line 578...
547
#endif
578
    if(PIND & 0x80) {HwVersion = 12; IntRef = 0xc0;}    
Line 576... Line 607...
576
    TestschubTimer    = SetDelay(1006);
607
    TestschubTimer    = SetDelay(1006);
577
    while(!CheckDelay(MinUpmPulse))
608
    while(!CheckDelay(MinUpmPulse))
578
    {
609
    {
579
     if(SollwertErmittlung()) break;
610
     if(SollwertErmittlung()) break;
580
    }
611
    }
581
    ;
612
 
582
    PORTD |= GRUEN;
613
    GRN_ON;
583
    PWM = 0;
614
    PWM = 0;
Line 584... Line 615...
584
 
615
 
Line 585... Line 616...
585
    SetPWM();
616
    SetPWM();
Line 632... Line 663...
632
            if(MotorGestoppt) MotorAnwerfen = 1;        // Startversuch
663
            if(MotorGestoppt) MotorAnwerfen = 1;        // Startversuch
633
            MotorGestopptTimer = SetDelay(1500);
664
            MotorGestopptTimer = SetDelay(1500);
634
            }
665
            }
Line 635... Line 666...
635
 
666
 
636
        if(MotorGestoppt && !TEST_SCHUB) PWM = 0;
-
 
637
if(SIO_Drehzahl > 120 && PWM > )
667
        if(MotorGestoppt && !TEST_SCHUB) PWM = 0;
638
        SetPWM();
668
        SetPWM();
639
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
669
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
640
        if(!ZeitFuerBerechnungen++)
670
        if(!ZeitFuerBerechnungen++)
641
            {
671
            {
-
 
672
            if(MotorGestoppt)
-
 
673
             {
-
 
674
              GRN_ON;
-
 
675
              FastADConvert();
642
            if(MotorGestoppt) PORTD |= GRUEN; //else PORTD &= ~GRUEN;
676
             }
643
            if(SIO_DEBUG)
677
            if(SIO_DEBUG)
644
                {
678
                {
645
                DebugAusgaben();  // welche Werte sollen angezeigt werden?
679
                DebugAusgaben();  // welche Werte sollen angezeigt werden?
646
                if(!UebertragungAbgeschlossen)  SendUart();
680
                if(!UebertragungAbgeschlossen)  SendUart();
Line 650... Line 684...
650
            if(CheckDelay(MittelstromTimer))  
684
            if(CheckDelay(MittelstromTimer))  
651
                {
685
                {
652
                MittelstromTimer = SetDelay(50); // alle 50ms
686
                MittelstromTimer = SetDelay(50); // alle 50ms
653
                if(Mittelstrom <  Strom) Mittelstrom++;// Mittelwert des Stroms bilden
687
                if(Mittelstrom <  Strom) Mittelstrom++;// Mittelwert des Stroms bilden
654
                else if(Mittelstrom >  Strom) Mittelstrom--;
688
                else if(Mittelstrom >  Strom) Mittelstrom--;
655
                if((Strom > MAX_STROM)) MaxPWM -= MaxPWM / 32;              
689
                if(Strom > MAX_STROM) MaxPWM -= MaxPWM / 32;              
656
                if((Mittelstrom > LIMIT_STROM))// Strom am Limit?
690
                if((Mittelstrom > LIMIT_STROM))// Strom am Limit?
657
                    {
691
                    {
658
                    if(MaxPWM) MaxPWM--;// dann die Maximale PWM herunterfahren
692
                    if(MaxPWM) MaxPWM--;// dann die Maximale PWM herunterfahren
659
                    PORTC |= ROT;
693
                    PORTC |= ROT;
660
                    }
694
                    }
Line 670... Line 704...
670
                SIO_Drehzahl = CntKommutierungen;//(6 * CntKommutierungen) / (POLANZAHL / 2);
704
                SIO_Drehzahl = CntKommutierungen;//(6 * CntKommutierungen) / (POLANZAHL / 2);
671
                CntKommutierungen = 0;
705
                CntKommutierungen = 0;
672
               // if(PPM_Timeout == 0) // keine PPM-Signale
706
               // if(PPM_Timeout == 0) // keine PPM-Signale
673
                ZeitZumAdWandeln = 1;
707
                ZeitZumAdWandeln = 1;
674
                }
708
                }
-
 
709
 
675
          if(TEST_SCHUB)
710
#if TEST_SCHUB == 1
676
           {
711
           {
677
            if(CheckDelay(TestschubTimer))  
712
            if(CheckDelay(TestschubTimer))  
678
                {
713
                {
679
                TestschubTimer = SetDelay(1500);
714
                TestschubTimer = SetDelay(1500);
680
                    switch(test)
715
                    switch(test)
Line 686... Line 721...
686
                        case 4: PWM = 150; test = 0; break;
721
                        case 4: PWM = 150; test = 0; break;
687
                        default: test = 0;
722
                        default: test = 0;
688
                        }
723
                        }
689
                }
724
                }
690
            }  
725
            }  
-
 
726
#endif
691
          // Motor Stehen geblieben
727
          // Motor Stehen geblieben
692
            if((CheckDelay(MinUpmPulse) && SIO_Drehzahl == 0) || MotorAnwerfen)
728
            if((CheckDelay(MinUpmPulse) && SIO_Drehzahl == 0) || MotorAnwerfen)
693
                {
729
                {
694
                MotorGestoppt = 1;    
730
                MotorGestoppt = 1;    
695
                DISABLE_SENSE_INT;
731
                DISABLE_SENSE_INT;
696
                MinUpmPulse = SetDelay(100);        
732
                MinUpmPulse = SetDelay(100);        
697
                if(MotorAnwerfen)
733
                if(MotorAnwerfen)
698
                    {
734
                  {
699
                    PORTC &= ~ROT;
735
                   PORTC &= ~ROT;
-
 
736
                   Strom_max = 0;
700
                    MotorAnwerfen = 0;
737
                   MotorAnwerfen = 0;
701
                    Anwerfen(10);
738
                   if(Anwerfen(10))
-
 
739
                   {  
702
                    PORTD |= GRUEN;
740
                    GRN_ON;
703
                    MotorGestoppt = 0;    
741
                    MotorGestoppt = 0;    
704
                    Phase--;
742
                    Phase--;
705
                    PWM = 1;
743
                    PWM = 1;
706
                    SetPWM();
744
                    SetPWM();
707
                    SENSE_TOGGLE_INT;
745
                    SENSE_TOGGLE_INT;
708
                    ENABLE_SENSE_INT;
746
                    ENABLE_SENSE_INT;
709
                    CntKommutierungen = 0;
-
 
710
                    MinUpmPulse = SetDelay(10); //20
747
                    MinUpmPulse = SetDelay(20);
711
                    while(!CheckDelay(MinUpmPulse)); // kurz Synchronisieren
748
                    while(!CheckDelay(MinUpmPulse)); // kurz Synchronisieren
712
                    PWM = 15;
749
                    PWM = 15;
713
                    SetPWM();
750
                    SetPWM();
714
                    MinUpmPulse = SetDelay(300);
751
                    MinUpmPulse = SetDelay(300);
715
                    while(!CheckDelay(MinUpmPulse)); // kurz Durchstarten
752
                    while(!CheckDelay(MinUpmPulse)) // kurz Durchstarten
716
                   
-
 
-
 
753
                    {
-
 
754
                      if(Strom > LIMIT_STROM/2)
-
 
755
                      {
-
 
756
                        STEUER_OFF; // Abschalten wegen Kurzschluss
-
 
757
                        RotBlink(10);
-
 
758
                        MotorAnwerfen = 1;
-
 
759
                      }  
-
 
760
                    }
717
                                    // Drehzahlmessung wieder aufsetzen
761
                                    // Drehzahlmessung wieder aufsetzen
718
                    DrehzahlMessTimer = SetDelay(50);
762
                    DrehzahlMessTimer = SetDelay(50);
719
                    altPhase = 7;
763
                    altPhase = 7;
720
                    }
764
                   }
-
 
765
                   else if(SollwertErmittlung()) MotorAnwerfen = 1;
-
 
766
                  }
721
                }
767
                }
722
            } // ZeitFuerBerechnungen
768
            } // ZeitFuerBerechnungen
723
        } // while(1) - Hauptschleife
769
        } // while(1) - Hauptschleife
724
}
770
}