Subversion Repositories FlightCtrl

Rev

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

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