Subversion Repositories FlightCtrl

Rev

Rev 309 | Rev 316 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
308 osiair 1
#include "main.h"
2
 
3
volatile unsigned int CountMilliseconds = 0;
4
volatile static unsigned int tim_main;
5
volatile unsigned char UpdateMotor = 0;
309 osiair 6
volatile unsigned int beeptime = 0;
308 osiair 7
volatile unsigned int cntKompass = 0;
8
int ServoValue = 0;
309 osiair 9
/*Salvo 8.9.2007
10
volatile uint8_t Kompass_Neuer_Wert= 0;
11
volatile unsigned int Kompass_Value_Old = 0;
12
// Salvo End
13
//Salvo 21.9.2007
14
short unsigned int Kompass_present= 0; //>0 bedeutet dass der Kompass vorhanden ist
15
// Salvo End */
308 osiair 16
enum {
17
  STOP             = 0,
18
  CK               = 1,
19
  CK8              = 2,
20
  CK64             = 3,
21
  CK256            = 4,
22
  CK1024           = 5,
23
  T0_FALLING_EDGE  = 6,
24
  T0_RISING_EDGE   = 7
25
};
26
 
27
 
28
SIGNAL (SIG_OVERFLOW0)    // 8kHz
29
{
30
    static unsigned char cnt_1ms = 1,cnt = 0;
31
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
32
 
33
   if(!cnt--)
34
    {
309 osiair 35
//       if (Kompass_present > 0) Kompass_present--; //Runterzaehlen. Wenn 0 ist der Kompass nicht vorhanden
308 osiair 36
     cnt = 9;
37
     cnt_1ms++;
38
     cnt_1ms %= 2;
39
     if(!cnt_1ms) UpdateMotor = 1;
40
     CountMilliseconds++;
309 osiair 41
     if(Timeout) Timeout--;
308 osiair 42
     }  
43
 
44
     if(beeptime > 1)
45
        {
46
        beeptime--;      
47
        if(beeptime & BeepMuster)
48
         {
49
          pieper_ein = 1;
50
         }
51
         else pieper_ein = 0;
52
        }
53
     else
54
      {
55
       pieper_ein = 0;
56
       BeepMuster = 0xffff;
57
      }
58
 
59
 
60
     if(pieper_ein)
61
        {
62
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
63
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
64
        }
65
     else  
66
        {
67
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
68
         else                      PORTC &= ~(1<<7);
69
        }
70
 
71
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV || EE_Parameter.GlobalConfig & CFG_GPS_AKTIV) //Abfrage um GPS_AKTIV erweitert, damit auch bei nur eingeschaltetem GPS OHNE Kompass der benötigte Kompassvalue berechnet wird (200907Kr)
72
 {
73
//  if(PINC & 0x10)
74
//   {
75
//    cntKompass++; 
76
//   }
77
//  else
78
//   {
79
//    if((cntKompass) && (cntKompass < 4000)) 
80
//    { 
81
//     KompassValue = cntKompass;
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
//      if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
91
//      {
92
                MM3_timer0();           // Kompass auslesen
93
 
94
                if (!cntKompass--)              // Aufruf mit 25 Hz
95
                {
313 osiair 96
 
309 osiair 97
 
313 osiair 98
                if (MM3_heading() > 0) { KompassValue = 360-MM3_heading();}
99
                if (MM3_heading() < 0 ) { KompassValue = MM3_heading()*-1;}
100
 
309 osiair 101
 
102
 
103
                KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360);
104
 
105
 
308 osiair 106
                        cntKompass = 320;
107
                }
108
        }
309 osiair 109
 
308 osiair 110
 
309 osiair 111
}
308 osiair 112
 
113
 
114
void Timer_Init(void)
115
{
116
    tim_main = SetDelay(10);
117
    TCCR0B = CK8;
118
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
119
    OCR0A =  0;
120
    OCR0B = 120;
121
    TCNT0 = -TIMER_RELOAD_VALUE;  // reload
122
    //OCR1  = 0x00;
123
 
124
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
125
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
126
 
127
//    TIMSK2 |= _BV(TOIE2);
128
TIMSK2 |= _BV(OCIE2A);
129
 
130
    TIMSK0 |= _BV(TOIE0);
131
    OCR2A = 10;
132
    TCNT2 = 0;
133
 
134
}
135
 
136
// -----------------------------------------------------------------------
137
 
138
unsigned int SetDelay (unsigned int t)
139
{
140
//  TIMSK0 &= ~_BV(TOIE0);
141
  return(CountMilliseconds + t + 1);                                            
142
//  TIMSK0 |= _BV(TOIE0);
143
}
144
 
145
// -----------------------------------------------------------------------
146
char CheckDelay(unsigned int t)
147
{
148
//  TIMSK0 &= ~_BV(TOIE0);
149
  return(((t - CountMilliseconds) & 0x8000) >> 9);
150
//  TIMSK0 |= _BV(TOIE0);
151
}
152
 
153
// -----------------------------------------------------------------------
154
void Delay_ms(unsigned int w)
155
{
156
 unsigned int akt;
157
 akt = SetDelay(w);
158
 while (!CheckDelay(akt));
159
}
160
 
161
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
162
//  Servo ansteuern
163
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
164
SIGNAL(SIG_OUTPUT_COMPARE2A)
165
{
166
  static unsigned char timer = 10;
167
 
168
  if(!timer--)  
169
    {
170
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
171
     ServoValue =  Parameter_ServoNickControl;
172
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
173
     else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
174
 
175
     if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
176
     else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
177
 
309 osiair 178
     //DebugOut.Analog[10] = ServoValue;     
308 osiair 179
     OCR2A = ServoValue;// + 75;
180
     timer = EE_Parameter.ServoNickRefresh;
181
    }
182
    else
183
    {
184
     TCCR2A =3;
185
     PORTD&=~0x80;
186
    }
187
}