Subversion Repositories FlightCtrl

Rev

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

Rev 749 Rev 753
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 beeptime = 0;
6
volatile unsigned int beeptime = 0;
7
unsigned int BeepMuster = 0xffff;
7
unsigned int BeepMuster = 0xffff;
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)    // 9,8kHz
22
SIGNAL (SIG_OVERFLOW0)    // 9,8kHz
23
{
23
{
24
    static unsigned char cnt_1ms = 1,cnt = 0;
24
    static unsigned char cnt_1ms = 1,cnt = 0;
-
 
25
        static uint8_t led_dim, led_duty;
25
    unsigned char pieper_ein = 0;
26
    unsigned char pieper_ein = 0;
26
 
27
 
27
   if(!cnt--)
28
   if(!cnt--)           // ca. 1 kHz
28
    {
29
    {
29
     cnt = 10;
30
     cnt = 10;
30
     cnt_1ms++;
31
     cnt_1ms++;
31
     cnt_1ms %= 2;
32
     cnt_1ms %= 2;
32
     if(!cnt_1ms) UpdateMotor = 1;
33
     if(!cnt_1ms) UpdateMotor = 1;
33
     CountMilliseconds++;
34
     CountMilliseconds++;
-
 
35
         
-
 
36
         // LED-Dimmung Duty-Cycle
-
 
37
         if (!led_duty--)
-
 
38
         {
-
 
39
                led_duty = 4;
-
 
40
                led_dim = Poti2 >> 2;
-
 
41
                PORTD &= ~(1<<PD4);     // J5 auf Low
-
 
42
         }       
34
     }  
43
     }
-
 
44
       
-
 
45
        if (led_dim) led_dim--;
-
 
46
        else PORTD |= (1<<PD4);
35
 
47
 
36
     if(beeptime > 1)
48
     if(beeptime > 1)
37
        {
49
        {
38
        beeptime--;      
50
        beeptime--;      
39
        if(beeptime & BeepMuster)
51
        if(beeptime & BeepMuster)
40
         {
52
         {
41
          pieper_ein = 1;
53
          pieper_ein = 1;
42
         }
54
         }
43
         else pieper_ein = 0;
55
         else pieper_ein = 0;
44
        }
56
        }
45
     else
57
     else
46
      {
58
      {
47
       pieper_ein = 0;
59
       pieper_ein = 0;
48
       BeepMuster = 0xffff;
60
       BeepMuster = 0xffff;
49
      }
61
      }
50
 
62
 
51
 
63
 
52
     if(pieper_ein)
64
     if(pieper_ein)
53
        {
65
        {
54
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
66
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
55
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
67
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
56
        }
68
        }
57
     else  
69
     else  
58
        {
70
        {
59
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
71
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
60
         else                      PORTC &= ~(1<<7);
72
         else                      PORTC &= ~(1<<7);
61
        }
73
        }
62
 
74
 
63
        if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) timer0_MM3();         // Kompass auslesen
75
        if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) timer0_MM3();         // Kompass auslesen
64
}
76
}
65
 
77
 
66
 
78
 
67
void Timer_Init(void)
79
void Timer_Init(void)
68
{
80
{
69
    TCCR0B = CK8;
81
    TCCR0B = CK8;
70
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
82
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
71
    OCR0A =  0;
83
    OCR0A =  0;
72
    OCR0B = 120;
84
    OCR0B = 120;
73
 
85
 
74
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
86
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
75
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
87
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
76
   
88
   
77
//    TIMSK2 |= _BV(TOIE2);
89
        //    TIMSK2 |= _BV(TOIE2);
78
TIMSK2 |= _BV(OCIE2A);
90
        TIMSK2 |= _BV(OCIE2A);
79
 
91
 
80
    TIMSK0 |= _BV(TOIE0);
92
    TIMSK0 |= _BV(TOIE0);
81
    OCR2A = 10;
93
    OCR2A = 10;
82
    TCNT2 = 0;
94
    TCNT2 = 0;    
83
   
-
 
84
}
95
}
85
 
96
 
86
// -----------------------------------------------------------------------
97
// -----------------------------------------------------------------------
87
 
98
 
88
unsigned int SetDelay (unsigned int t)
99
unsigned int SetDelay (unsigned int t)
89
{
100
{
90
   uint8_t tmp_sreg;
101
   uint8_t tmp_sreg;
91
   uint16_t CountMilliseconds_local;
102
   uint16_t CountMilliseconds_local;
92
   
103
   
93
   tmp_sreg = SREG;
104
   tmp_sreg = SREG;
94
   cli();
105
   cli();
95
   CountMilliseconds_local = CountMilliseconds;
106
   CountMilliseconds_local = CountMilliseconds;
96
   SREG = tmp_sreg;
107
   SREG = tmp_sreg;
97
   return(CountMilliseconds_local + t + 1);
108
   return(CountMilliseconds_local + t + 1);
98
}
109
}
99
 
110
 
100
// -----------------------------------------------------------------------
111
// -----------------------------------------------------------------------
101
char CheckDelay(unsigned int t)
112
char CheckDelay(unsigned int t)
102
{
113
{
103
   uint8_t tmp_sreg;
114
   uint8_t tmp_sreg;
104
   uint16_t CountMilliseconds_local;
115
   uint16_t CountMilliseconds_local;
105
   
116
   
106
   tmp_sreg = SREG;
117
   tmp_sreg = SREG;
107
   cli();
118
   cli();
108
   CountMilliseconds_local = CountMilliseconds;
119
   CountMilliseconds_local = CountMilliseconds;
109
   SREG = tmp_sreg;
120
   SREG = tmp_sreg;
110
   return (((t - CountMilliseconds_local) & 0x8000) >> 9);
121
   return (((t - CountMilliseconds_local) & 0x8000) >> 9);
111
}
122
}
112
 
123
 
113
// -----------------------------------------------------------------------
124
// -----------------------------------------------------------------------
114
void Delay_ms(unsigned int w)
125
void Delay_ms(unsigned int w)
115
{
126
{
116
 unsigned int akt;
127
 unsigned int akt;
117
 akt = SetDelay(w);
128
 akt = SetDelay(w);
118
 while (!CheckDelay(akt));
129
 while (!CheckDelay(akt));
119
}
130
}
120
 
131
 
121
void Delay_ms_Mess(unsigned int w)
132
void Delay_ms_Mess(unsigned int w)
122
{
133
{
123
 unsigned int akt;
134
 unsigned int akt;
124
 akt = SetDelay(w);
135
 akt = SetDelay(w);
125
 while (!CheckDelay(akt)) ANALOG_ON;
136
 while (!CheckDelay(akt)) ANALOG_ON;
126
}
137
}
127
 
138
 
128
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
139
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
129
//  Servo ansteuern
140
//  Servo ansteuern
130
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
141
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
131
SIGNAL(SIG_OUTPUT_COMPARE2A)
142
SIGNAL(SIG_OUTPUT_COMPARE2A)
132
{
143
{
133
  static unsigned char timer = 10;
144
  static unsigned char timer = 10;
134
 
145
 
135
  if(!timer--)  
146
  if(!timer--)  
136
    {
147
    {
137
                // enable PWM on PD7 in non inverting mode
148
                // enable PWM on PD7 in non inverting mode
138
                TCCR2A= (1<<COM2A1)|(0<<COM2A0)|3;
149
                TCCR2A= (1<<COM2A1)|(0<<COM2A0)|3;
139
                 
150
                 
140
                ServoValue =  Parameter_ServoNickControl;
151
                ServoValue =  Parameter_ServoNickControl;
141
                if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
152
                if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
142
                else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
153
                else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
143
     
154
     
144
                if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
155
                if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
145
                else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
156
                else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
146
 
157
 
147
                OCR2A = ServoValue;
158
                OCR2A = ServoValue;
148
                timer = EE_Parameter.ServoNickRefresh;
159
                timer = EE_Parameter.ServoNickRefresh;
149
    }
160
    }
150
    else
161
    else
151
    {
162
    {
152
                // disable PWM at PD7
163
                // disable PWM at PD7
153
                TCCR2A = 3;
164
                TCCR2A = 3;
154
                // set PD7 to low
165
                // set PD7 to low
155
                PORTD &= ~(1<<PORTD7);
166
                PORTD &= ~(1<<PD7);
156
    }
167
    }
157
}
168
}
158
 
169