Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
796 joko 1
#include "main.h"
2
 
3
volatile unsigned int CountMilliseconds = 0;
4
volatile unsigned char UpdateMotor = 0;
5
volatile unsigned int beeptime = 0;
6
volatile unsigned int cntKompass = 0;
7
unsigned int BeepMuster = 0xffff;
8
int ServoValue = 0;
9
 
10
enum {
11
  STOP             = 0,
12
  CK               = 1,
13
  CK8              = 2,
14
  CK64             = 3,
15
  CK256            = 4,
16
  CK1024           = 5,
17
  T0_FALLING_EDGE  = 6,
18
  T0_RISING_EDGE   = 7
19
};
20
 
21
 
22
SIGNAL (SIG_OVERFLOW0)    // 20MHz/8/256 = 9,765KHz
23
{
24
    static unsigned char cnt_1ms = 1,cnt = 0;
25
    unsigned char pieper_ein = 0;
26
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
27
 
28
   if(!cnt--)
29
    {
30
        //***Info aus Email von Walter
31
        //durch cnt kann die Updatefrequenz der Motoren beeinflusst werden. Bei cnt=9 sind es etwa 500hz.
32
        //mit "cnt = 9;" sind es 488,28 Hz (alle 2,048 ms); (20000000 / 8 / 256 / 10 / 2)  
33
        //mit "cnt = 10;" nur noch 443,89 Hz (alle 2,253 ms); (20000000 / 8 / 256 / 11 / 2)
34
        //***Info aus Email von Walter
35
 
36
         cnt = 9;
37
     cnt_1ms++;
38
     cnt_1ms %= 2;
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
 /* MM3 Magentometer 131207WM */
70
#ifdef MM3IS
71
 //Abfrage um GPS_AKTIV erweitert, damit auch bei nur eingeschaltetem GPS OHNE Kompass der benötigte Kompassvalue berechnet wird (200907Kr)
72
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV || EE_Parameter.GlobalConfig & CFG_GPS_AKTIV) timer0_MM3();
73
#endif
74
 
75
#ifndef MM3IS
76
  //Abfrage um GPS_AKTIV erweitert, damit auch bei nur eingeschaltetem GPS OHNE Kompass der benötigte Kompassvalue berechnet wird (200907Kr)
77
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV || EE_Parameter.GlobalConfig & CFG_GPS_AKTIV)
78
 {
79
  if(PINC & 0x10)
80
   {
81
    cntKompass++;
82
   }
83
  else
84
   {
85
    if((cntKompass) && (cntKompass < 4000))
86
    {
87
     KompassValue = cntKompass;
88
    }
89
//     if(cntKompass < 10) cntKompass = 10;
90
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
91
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
92
    cntKompass = 0;
93
   }
94
 }
95
#endif
96
 
97
}
98
 
99
 
100
void Timer_Init(void)
101
{
102
    TCCR0B = CK8;
103
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
104
    OCR0A =  0;
105
    OCR0B = 120;
106
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
107
    //OCR1  = 0x00;
108
 
109
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
110
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
111
 
112
//    TIMSK2 |= _BV(TOIE2);
113
TIMSK2 |= _BV(OCIE2A);
114
 
115
    TIMSK0 |= _BV(TOIE0);
116
    OCR2A = 10;
117
    TCNT2 = 0;
118
 
119
}
120
 
121
// -----------------------------------------------------------------------
122
 
123
unsigned int SetDelay (unsigned int t)
124
{
125
//  TIMSK0 &= ~_BV(TOIE0);
126
  return(CountMilliseconds + t + 1);                                            
127
//  TIMSK0 |= _BV(TOIE0);
128
}
129
 
130
// -----------------------------------------------------------------------
131
char CheckDelay(unsigned int t)
132
{
133
//  TIMSK0 &= ~_BV(TOIE0);
134
  return(((t - CountMilliseconds) & 0x8000) >> 9);
135
//  TIMSK0 |= _BV(TOIE0);
136
}
137
 
138
// -----------------------------------------------------------------------
139
void Delay_ms(unsigned int w)
140
{
141
 unsigned int akt;
142
 akt = SetDelay(w);
143
 while (!CheckDelay(akt));
144
}
145
 
146
void Delay_ms_Mess(unsigned int w)
147
{
148
 unsigned int akt;
149
 akt = SetDelay(w);
150
 while (!CheckDelay(akt)) ANALOG_ON;
151
}
152
 
153
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
154
//  Servo ansteuern
155
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
156
SIGNAL(SIG_OUTPUT_COMPARE2A)
157
{
158
  static unsigned char timer = 10;
159
 
160
  if(!timer--)  
161
    {
162
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
163
     ServoValue =  Parameter_ServoNickControl;
164
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
165
     else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
166
 
167
     if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
168
     else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
169
 
170
     OCR2A = ServoValue;// + 75;
171
     timer = EE_Parameter.ServoNickRefresh;
172
    }
173
    else
174
    {
175
     TCCR2A =3;
176
     PORTD&=~0x80;
177
    }
178
}