Subversion Repositories FlightCtrl

Rev

Rev 1 | Rev 174 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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