Subversion Repositories FlightCtrl

Rev

Rev 863 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 863 Rev 876
1
#include "main.h"
1
#include "main.h"
2
 
2
 
3
volatile unsigned int CountMilliseconds = 0;
3
volatile unsigned int CountMilliseconds = 0;
4
volatile static unsigned int tim_main;
4
volatile static unsigned int tim_main;
5
volatile unsigned char UpdateMotor = 0;
5
volatile unsigned char UpdateMotor = 0;
6
volatile unsigned int cntKompass = 0;
6
volatile unsigned int cntKompass = 0;
7
volatile unsigned int beeptime = 0;
7
volatile unsigned int beeptime = 0;
8
unsigned int BeepMuster = 0xffff;
8
unsigned int BeepMuster = 0xffff;
9
int ServoValue = 0;
9
int ServoValue = 0;
-
 
10
unsigned char ucBeeperActive=1;
10
 
11
 
11
enum {
12
enum {
12
  STOP             = 0,
13
  STOP             = 0,
13
  CK               = 1,
14
  CK               = 1,
14
  CK8              = 2,
15
  CK8              = 2,
15
  CK64             = 3,
16
  CK64             = 3,
16
  CK256            = 4,
17
  CK256            = 4,
17
  CK1024           = 5,
18
  CK1024           = 5,
18
  T0_FALLING_EDGE  = 6,
19
  T0_FALLING_EDGE  = 6,
19
  T0_RISING_EDGE   = 7
20
  T0_RISING_EDGE   = 7
20
};
21
};
21
 
22
 
22
 
23
 
23
SIGNAL (SIG_OVERFLOW0)    // 8kHz
24
SIGNAL (SIG_OVERFLOW0)    // 8kHz
24
{
25
{
25
    static unsigned char cnt_1ms = 1,cnt = 0;
26
    static unsigned char cnt_1ms = 1,cnt = 0;
26
    unsigned char pieper_ein = 0;
27
    unsigned char pieper_ein = 0;
27
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
28
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
28
 
29
 
29
   if(!cnt--)
30
   if(!cnt--)
30
    {
31
    {
31
     cnt = 9;
32
     cnt = 9;
32
     cnt_1ms++;
33
     cnt_1ms++;
33
     cnt_1ms %= 2;
34
     cnt_1ms %= 2;
34
     if(!cnt_1ms) UpdateMotor = 1;
35
     if(!cnt_1ms) UpdateMotor = 1;
35
     CountMilliseconds++;
36
     CountMilliseconds++;
36
     }  
37
     }  
37
 
38
 
38
     if(beeptime > 1)
39
     if(beeptime > 1)
39
        {
40
        {
40
        beeptime--;      
41
        beeptime--;      
41
        if(beeptime & BeepMuster)
42
        if(beeptime & BeepMuster)
42
         {
43
         {
43
          pieper_ein = 1;
44
          if(ucBeeperActive) pieper_ein = 1;
44
          EXT1OUT_INV_OFF;
45
          EXT1OUT_INV_OFF;
45
         }
46
         }
46
         else
47
         else
47
         {
48
         {
48
                pieper_ein = 0;
49
                if(ucBeeperActive) pieper_ein = 0;
49
                EXT1OUT_INV_ON;
50
                EXT1OUT_INV_ON;
50
         }
51
         }
51
        }
52
        }
52
     else
53
     else
53
      {
54
      {
54
        EXT1OUT_INV_ON;
55
        EXT1OUT_INV_ON;
55
       pieper_ein = 0;
56
       pieper_ein = 0;
-
 
57
       ucBeeperActive=1;
56
       BeepMuster = 0xffff;
58
       BeepMuster = 0xffff;
57
      }
59
      }
58
 
60
 
59
 
61
 
60
     if(pieper_ein)
62
     if(pieper_ein)
61
        {
63
        {
62
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
64
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
63
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
65
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
64
        }
66
        }
65
     else  
67
     else  
66
        {
68
        {
67
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
69
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
68
         else                      PORTC &= ~(1<<7);
70
         else                      PORTC &= ~(1<<7);
69
        }
71
        }
70
 
72
 
71
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
73
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
72
 {
74
 {
73
  if(PINC & 0x10)
75
  if(PINC & 0x10)
74
   {
76
   {
75
    cntKompass++;
77
    cntKompass++;
76
   }
78
   }
77
  else
79
  else
78
   {
80
   {
79
    if((cntKompass) && (cntKompass < 4000))
81
    if((cntKompass) && (cntKompass < 4000))
80
    {
82
    {
81
     KompassValue = cntKompass;
83
     KompassValue = cntKompass;
82
    }
84
    }
83
//     if(cntKompass < 10) cntKompass = 10;
85
//     if(cntKompass < 10) cntKompass = 10;
84
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
86
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
85
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
87
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
86
    cntKompass = 0;
88
    cntKompass = 0;
87
   }
89
   }
88
 }
90
 }
89
}
91
}
-
 
92
 
-
 
93
void setBeeperActive(unsigned char ucVal)
-
 
94
{
-
 
95
        ucBeeperActive=ucVal;
90
 
96
}
91
 
97
 
92
void Timer_Init(void)
98
void Timer_Init(void)
93
{
99
{
94
    tim_main = SetDelay(10);
100
    tim_main = SetDelay(10);
95
    TCCR0B = CK8;
101
    TCCR0B = CK8;
96
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
102
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
97
    OCR0A =  0;
103
    OCR0A =  0;
98
    OCR0B = 120;
104
    OCR0B = 120;
99
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
105
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
100
    //OCR1  = 0x00;
106
    //OCR1  = 0x00;
101
 
107
 
102
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
108
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
103
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
109
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
104
   
110
   
105
//    TIMSK2 |= _BV(TOIE2);
111
//    TIMSK2 |= _BV(TOIE2);
106
TIMSK2 |= _BV(OCIE2A);
112
TIMSK2 |= _BV(OCIE2A);
107
 
113
 
108
    TIMSK0 |= _BV(TOIE0);
114
    TIMSK0 |= _BV(TOIE0);
109
    OCR2A = 10;
115
    OCR2A = 10;
110
    TCNT2 = 0;
116
    TCNT2 = 0;
111
   
117
   
112
}
118
}
113
 
119
 
114
// -----------------------------------------------------------------------
120
// -----------------------------------------------------------------------
115
 
121
 
116
unsigned int SetDelay (unsigned int t)
122
unsigned int SetDelay (unsigned int t)
117
{
123
{
118
//  TIMSK0 &= ~_BV(TOIE0);
124
//  TIMSK0 &= ~_BV(TOIE0);
119
  return(CountMilliseconds + t + 1);                                            
125
  return(CountMilliseconds + t + 1);                                            
120
//  TIMSK0 |= _BV(TOIE0);
126
//  TIMSK0 |= _BV(TOIE0);
121
}
127
}
122
 
128
 
123
// -----------------------------------------------------------------------
129
// -----------------------------------------------------------------------
124
char CheckDelay(unsigned int t)
130
char CheckDelay(unsigned int t)
125
{
131
{
126
//  TIMSK0 &= ~_BV(TOIE0);
132
//  TIMSK0 &= ~_BV(TOIE0);
127
  return(((t - CountMilliseconds) & 0x8000) >> 9);
133
  return(((t - CountMilliseconds) & 0x8000) >> 9);
128
//  TIMSK0 |= _BV(TOIE0);
134
//  TIMSK0 |= _BV(TOIE0);
129
}
135
}
130
 
136
 
131
// -----------------------------------------------------------------------
137
// -----------------------------------------------------------------------
132
void Delay_ms(unsigned int w)
138
void Delay_ms(unsigned int w)
133
{
139
{
134
 unsigned int akt;
140
 unsigned int akt;
135
 akt = SetDelay(w);
141
 akt = SetDelay(w);
136
 while (!CheckDelay(akt));
142
 while (!CheckDelay(akt));
137
}
143
}
138
 
144
 
139
void Delay_ms_Mess(unsigned int w)
145
void Delay_ms_Mess(unsigned int w)
140
{
146
{
141
 unsigned int akt;
147
 unsigned int akt;
142
 akt = SetDelay(w);
148
 akt = SetDelay(w);
143
 while (!CheckDelay(akt)) ANALOG_ON;
149
 while (!CheckDelay(akt)) ANALOG_ON;
144
}
150
}
145
 
151
 
146
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
152
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
147
//  Servo ansteuern
153
//  Servo ansteuern
148
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
154
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
149
SIGNAL(SIG_OUTPUT_COMPARE2A)
155
SIGNAL(SIG_OUTPUT_COMPARE2A)
150
{
156
{
151
  static unsigned char timer = 10;
157
  static unsigned char timer = 10;
152
 
158
 
153
  if(!timer--)  
159
  if(!timer--)  
154
    {
160
    {
155
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
161
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
156
     ServoValue =  Parameter_ServoNickControl;
162
     ServoValue =  Parameter_ServoNickControl;
157
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
163
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
158
     else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
164
     else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
159
     
165
     
160
     if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
166
     if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
161
     else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
167
     else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
162
 
168
 
163
     OCR2A = ServoValue;// + 75;
169
     OCR2A = ServoValue;// + 75;
164
     timer = EE_Parameter.ServoNickRefresh;
170
     timer = EE_Parameter.ServoNickRefresh;
165
    }
171
    }
166
    else
172
    else
167
    {
173
    {
168
     TCCR2A =3;
174
     TCCR2A =3;
169
     PORTD&=~0x80;
175
     PORTD&=~0x80;
170
    }
176
    }
171
}
177
}
172
 
178