Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1152 grottenfli 1
#include "main.h"
2
#include "parameter.h"
3
 
4
volatile unsigned int CountMilliseconds = 0;
5
volatile static unsigned int tim_main;
6
volatile unsigned char UpdateMotor = 0;
7
volatile unsigned int cntKompass = 0;
8
volatile unsigned int beeptime = 0;
9
volatile unsigned char SendSPI = 0;
10
volatile unsigned int ServoState = 40;
11
 
12
unsigned int BeepMuster = 0xffff;
13
unsigned int ServoValue = 0;
14
 
15
enum {
16
  STOP             = 0,
17
  CK               = 1,
18
  CK8              = 2,
19
  CK64             = 3,
20
  CK256            = 4,
21
  CK1024           = 5,
22
  T0_FALLING_EDGE  = 6,
23
  T0_RISING_EDGE   = 7
24
};
25
 
26
 
27
SIGNAL (SIG_OVERFLOW0)    // 8kHz
28
{
29
    static unsigned char cnt_1ms = 1,cnt = 0;
30
    unsigned char pieper_ein = 0;
31
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
32
   if(SendSPI) SendSPI--;
33
   if(!cnt--)
34
    {
35
     cnt = 9;
36
//        cnt_1ms++;
37
//        cnt_1ms %= 2;
38
        cnt_1ms = !cnt_1ms;  // So ist das etwas einfacher
39
     if(!cnt_1ms) UpdateMotor = 1;
40
     CountMilliseconds++;
41
    }  
42
 
43
     if(beeptime > 1)
44
        {
45
        beeptime--;      
46
        if(beeptime & BeepMuster)
47
         {
48
          pieper_ein = 1;
49
         }
50
         else pieper_ein = 0;
51
        }
52
     else
53
      {
54
       pieper_ein = 0;
55
       BeepMuster = 0xffff;
56
      }
57
 
58
 
59
     if(pieper_ein)
60
        {
61
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
62
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
63
        }
64
     else  
65
        {
66
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
67
         else                      PORTC &= ~(1<<7);
68
        }
69
 
70
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
71
 {
72
  if(PINC & 0x10)
73
   {
74
    cntKompass++;
75
   }
76
  else
77
   {
78
    if((cntKompass) && (cntKompass < 362))
79
    {
80
     cntKompass += cntKompass / 41;
81
     if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0;
82
    }
83
//     if(cntKompass < 10) cntKompass = 10;
84
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
85
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
86
    cntKompass = 0;
87
   }
88
 }
89
}
90
 
91
//----------------------------
92
void Timer_Init(void)
93
{
94
    tim_main = SetDelay(10);
95
    TCCR0B = CK8;
96
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
97
    OCR0A =  0;
98
    OCR0B = 120;
99
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
100
    //OCR1  = 0x00;
101
 
102
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
103
//    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);    // clk/256
104
    TCCR2B=(0<<CS20)|(0<<CS21)|(1<<CS22);      // clk/64
105
 
106
 
107
  TIMSK2 |= _BV(OCIE2A);
108
 
109
    TIMSK0 |= _BV(TOIE0);
110
    OCR2A = 10;
111
    TCNT2 = 0;
112
 
113
}
114
 
115
// -----------------------------------------------------------------------
116
 
117
unsigned int SetDelay (unsigned int t)
118
{
119
//  TIMSK0 &= ~_BV(TOIE0);
120
  return(CountMilliseconds + t + 1);                                            
121
//  TIMSK0 |= _BV(TOIE0);
122
}
123
 
124
// -----------------------------------------------------------------------
125
char CheckDelay(unsigned int t)
126
{
127
//  TIMSK0 &= ~_BV(TOIE0);
128
  return(((t - CountMilliseconds) & 0x8000) >> 9);
129
//  TIMSK0 |= _BV(TOIE0);
130
}
131
 
132
// -----------------------------------------------------------------------
133
void Delay_ms(unsigned int w)
134
{
135
 unsigned int akt;
136
 akt = SetDelay(w);
137
 while (!CheckDelay(akt));
138
}
139
 
140
void Delay_ms_Mess(unsigned int w)
141
{
142
 unsigned int akt;
143
 akt = SetDelay(w);
144
 while (!CheckDelay(akt)) ANALOG_ON;
145
}
146
 
147
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
148
//  Servo ansteuern
149
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
150
SIGNAL(SIG_OVERFLOW2)
151
{
152
  if (ServoState > 0) PORTD |= 0x80;
153
  else PORTD &= ~0x80;
154
  TCCR2A =3;
155
  TIMSK2 &= ~_BV(TOIE2);
156
}
157
 
158
SIGNAL(SIG_OUTPUT_COMPARE2A)
159
{
160
  static unsigned char postPulse = 0x80;
161
  static int filterServo = 100;
162
  #define MULTIPLIER 4
163
  if(ServoState == 4)  
164
    {
165
     ServoValue = 0x0030;  // Offset Part1
166
     filterServo = (filterServo * 3 + (int) Parameter_ServoNickControl * 2)/4;
167
         ServoValue += filterServo;
168
 
169
 
170
        // Min und Max vorverlegt, damit sich diese auf ServoNickControl beziehen und ggf. noch Nick-kompensiert werden
171
        if (ServoValue < ((int) EE_Parameter.ServoNickMin * 3))
172
            ServoValue = (int) EE_Parameter.ServoNickMin * 3;
173
        else if (ServoValue > ((int) EE_Parameter.ServoNickMax * 3))
174
            ServoValue = (int) EE_Parameter.ServoNickMax * 3;
175
 
176
        long integral;
177
 
178
        /* Über Parameter läßt sich zwischen "+" und "X" - Formations
179
         * umschalten (s. parameter.h)
180
         */
181
        if (PARAM_X_FORMATION)
182
            integral = IntegralNick - IntegralRoll;
183
        else
184
            integral = IntegralNick;
185
 
186
        if(EE_Parameter.ServoNickCompInvert & 0x01)
187
            ServoValue += ((long) ((long)EE_Parameter.ServoNickComp * IntegralNick) / 128L )/ (512L/4);
188
        else
189
            ServoValue -= ((long) ((long)EE_Parameter.ServoNickComp * IntegralNick) / 128L) / (512L/4);
190
 
191
         DebugOut.Analog[20] = ServoValue;
192
 
193
         if ((ServoValue % 255) < 45) { ServoValue+= 77; postPulse = 0x60 - 77; } else postPulse = 0x60;
194
         OCR2A = 255-(ServoValue % 256);
195
         TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;  
196
        }
197
  else if ((ServoState > 0) && (ServoState < 4))
198
    {
199
          if(ServoValue > 255)
200
          { PORTD |= 0x80;
201
                TCCR2A =3;
202
                ServoValue -= 255;
203
          }
204
          else
205
      {  
206
             TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
207
                 OCR2A = postPulse; // Offset Part2
208
                 ServoState = 1;
209
      }
210
  }
211
  else if (ServoState == 0)
212
          {
213
             ServoState  = (int) EE_Parameter.ServoNickRefresh * MULTIPLIER;
214
         PORTD&=~0x80;
215
                 TCCR2A = 3;
216
          }
217
  ServoState--;
218
 
219
}