Subversion Repositories FlightCtrl

Rev

Rev 1002 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
#include "main.h"
2
 
3
volatile unsigned int CountMilliseconds = 0;
4
volatile static unsigned int tim_main;
5
volatile unsigned char UpdateMotor = 0;
6
volatile unsigned int cntKompass = 0;
7
volatile unsigned int beeptime = 0;
723 hbuss 8
volatile unsigned char SendSPI = 0;
910 hbuss 9
volatile unsigned int ServoState = 40;
723 hbuss 10
 
173 holgerb 11
unsigned int BeepMuster = 0xffff;
910 hbuss 12
unsigned int ServoValue = 0;
1003 salvo 13
//Salvo 8.9.2007
14
volatile uint8_t Kompass_Neuer_Wert= 0;
15
volatile unsigned int Kompass_Value_Old = 0;
16
// Salvo End
17
//Salvo 21.9.2007
18
short unsigned int Kompass_present= 0; //>0 bedeutet dass der Kompass vorhanden ist
19
// Salvo End
1 ingob 20
enum {
21
  STOP             = 0,
22
  CK               = 1,
23
  CK8              = 2,
24
  CK64             = 3,
25
  CK256            = 4,
26
  CK1024           = 5,
27
  T0_FALLING_EDGE  = 6,
28
  T0_RISING_EDGE   = 7
29
};
30
 
1003 salvo 31
// Aenderungen von Peter Muehlenbrock ("Salvo") Stand 24.12.2007
32
/*
33
Ersatzkompass abgeleitet aus Magnetkompass und Giergyro fuer nahezu neigungsubhaengige Kompassfunktion
34
*/
1 ingob 35
SIGNAL (SIG_OVERFLOW0)    // 8kHz
36
{
37
    static unsigned char cnt_1ms = 1,cnt = 0;
173 holgerb 38
    unsigned char pieper_ein = 0;
1 ingob 39
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
723 hbuss 40
   if(SendSPI) SendSPI--;
1 ingob 41
   if(!cnt--)
42
    {
1003 salvo 43
         if (Kompass_present > 0) Kompass_present--; //Runterzaehlen. Wenn 0 ist der Kompass nicht vorhanden
1 ingob 44
     cnt = 9;
45
     cnt_1ms++;
46
     cnt_1ms %= 2;
47
     if(!cnt_1ms) UpdateMotor = 1;
48
     CountMilliseconds++;
723 hbuss 49
    }  
1 ingob 50
 
51
     if(beeptime > 1)
52
        {
173 holgerb 53
        beeptime--;      
54
        if(beeptime & BeepMuster)
55
         {
56
          pieper_ein = 1;
57
         }
58
         else pieper_ein = 0;
1 ingob 59
        }
173 holgerb 60
     else
61
      {
62
       pieper_ein = 0;
63
       BeepMuster = 0xffff;
64
      }
65
 
66
 
67
     if(pieper_ein)
68
        {
69
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
70
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
71
        }
1 ingob 72
     else  
173 holgerb 73
        {
74
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
75
         else                      PORTC &= ~(1<<7);
76
        }
77
 
1003 salvo 78
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) //Salvo 25.10.2008
1 ingob 79
 {
80
  if(PINC & 0x10)
81
   {
82
    cntKompass++;
83
   }
84
  else
85
   {
849 hbuss 86
    if((cntKompass) && (cntKompass < 362))
1 ingob 87
    {
693 hbuss 88
     cntKompass += cntKompass / 41;
89
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
1003 salvo 90
// Salvo Kompassoffset 23.12.2007 ***********
91
         Kompass_present = 255;
92
//       Kompass_Value_Old      =       KompassValue;
93
         if (KOMPASS_OFFSET > 0)  KompassValue = cntKompass -KOMPASS_OFFSET;
94
         else KompassValue = cntKompass - ((int) (Parameter_UserParam4*2));
95
 
96
         if (KompassValue < 0)
97
         {
98
                KompassValue += 360;
99
         }
100
         if (KompassValue >= 360)
101
         {
102
                KompassValue -= 360;
103
         }     
104
 // Salvo End  
1 ingob 105
    }
106
//     if(cntKompass < 10) cntKompass = 10;
107
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
108
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
1003 salvo 109
//Salvo 13.9.2007 Ok Erkennung des Magnetkompasses 
110
         Kompass_Neuer_Wert     = 1;
111
 // Salvo End
112
     cntKompass = 0;
1 ingob 113
   }
114
 }
115
}
116
 
910 hbuss 117
//----------------------------
1 ingob 118
void Timer_Init(void)
119
{
120
    tim_main = SetDelay(10);
121
    TCCR0B = CK8;
122
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
123
    OCR0A =  0;
124
    OCR0B = 120;
304 ingob 125
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
1 ingob 126
    //OCR1  = 0x00;
127
 
128
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
910 hbuss 129
//    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);    // clk/256
130
    TCCR2B=(0<<CS20)|(0<<CS21)|(1<<CS22);      // clk/64
1 ingob 131
 
910 hbuss 132
 
133
  TIMSK2 |= _BV(OCIE2A);
1 ingob 134
 
135
    TIMSK0 |= _BV(TOIE0);
136
    OCR2A = 10;
137
    TCNT2 = 0;
138
 
139
}
140
 
141
// -----------------------------------------------------------------------
142
 
143
unsigned int SetDelay (unsigned int t)
144
{
145
//  TIMSK0 &= ~_BV(TOIE0);
146
  return(CountMilliseconds + t + 1);                                            
147
//  TIMSK0 |= _BV(TOIE0);
148
}
149
 
150
// -----------------------------------------------------------------------
151
char CheckDelay(unsigned int t)
152
{
153
//  TIMSK0 &= ~_BV(TOIE0);
154
  return(((t - CountMilliseconds) & 0x8000) >> 9);
155
//  TIMSK0 |= _BV(TOIE0);
156
}
157
 
158
// -----------------------------------------------------------------------
159
void Delay_ms(unsigned int w)
160
{
161
 unsigned int akt;
162
 akt = SetDelay(w);
163
 while (!CheckDelay(akt));
164
}
165
 
395 hbuss 166
void Delay_ms_Mess(unsigned int w)
167
{
168
 unsigned int akt;
169
 akt = SetDelay(w);
170
 while (!CheckDelay(akt)) ANALOG_ON;
171
}
172
 
1 ingob 173
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
174
//  Servo ansteuern
175
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
910 hbuss 176
SIGNAL(SIG_OVERFLOW2)
177
{
178
  if (ServoState > 0) PORTD |= 0x80;
179
  else PORTD &= ~0x80;
180
  TCCR2A =3;
181
  TIMSK2 &= ~_BV(TOIE2);
182
}
183
 
1 ingob 184
SIGNAL(SIG_OUTPUT_COMPARE2A)
185
{
910 hbuss 186
  static unsigned char postPulse = 0x80;
187
  static int filterServo = 100;
188
  #define MULTIPLIER 4
189
  if(ServoState == 4)  
1 ingob 190
    {
910 hbuss 191
     ServoValue = 0x0030;  // Offset Part1
192
     filterServo = (filterServo * 3 + (int) Parameter_ServoNickControl * 2)/4;
193
         ServoValue += filterServo;
194
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) ((long)EE_Parameter.ServoNickComp * IntegralNick) / 128L )/ (512L/MULTIPLIER);
195
     else ServoValue -= ((long) ((long)EE_Parameter.ServoNickComp * IntegralNick) / 128L) / (512L/MULTIPLIER);
196
     if((ServoValue) < ((int)EE_Parameter.ServoNickMin*3)) ServoValue = (int)EE_Parameter.ServoNickMin*3;
197
     else if((ServoValue) > ((int)EE_Parameter.ServoNickMax*3)) ServoValue = (int)EE_Parameter.ServoNickMax*3;
198
 
199
         DebugOut.Analog[20] = ServoValue;
200
         if ((ServoValue % 255) < 45) { ServoValue+= 77; postPulse = 0x60 - 77; } else postPulse = 0x60;
201
         OCR2A = 255-(ServoValue % 256);
202
         TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;  
203
        }
204
  else if ((ServoState > 0) && (ServoState < 4))
205
    {
206
          if(ServoValue > 255)
207
          { PORTD |= 0x80;
208
                TCCR2A =3;
209
                ServoValue -= 255;
210
          }
211
          else
212
      {  
213
             TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
214
                 OCR2A = postPulse; // Offset Part2
215
                 ServoState = 1;
216
      }
217
  }
218
  else if (ServoState == 0)
219
          {
220
             ServoState  = (int) EE_Parameter.ServoNickRefresh * MULTIPLIER;
221
         PORTD&=~0x80;
222
                 TCCR2A = 3;
223
          }
224
  ServoState--;
1 ingob 225
 
226
}