Subversion Repositories FlightCtrl

Rev

Rev 624 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 624 Rev 683
Line 1... Line 1...
1
#include "main.h"
1
#include "main.h"
Line 2... Line 2...
2
 
2
 
3
volatile unsigned int CountMilliseconds = 0;
-
 
4
volatile static unsigned int tim_main;
3
volatile uint16_t CountMilliseconds = 0;
5
volatile unsigned char UpdateMotor = 0;
4
volatile uint8_t UpdateMotor = 0;
6
volatile unsigned int cntKompass = 0;
5
volatile uint16_t cntKompass = 0;
7
volatile unsigned int beeptime = 0;
6
volatile uint16_t BeepTime = 0;
8
unsigned int BeepMuster = 0xffff;
-
 
9
int ServoValue = 0;
-
 
10
 
-
 
11
enum {
-
 
12
  STOP             = 0,
-
 
13
  CK               = 1,
-
 
14
  CK8              = 2,
-
 
15
  CK64             = 3,
-
 
16
  CK256            = 4,
-
 
17
  CK1024           = 5,
-
 
18
  T0_FALLING_EDGE  = 6,
-
 
19
  T0_RISING_EDGE   = 7
-
 
20
};
-
 
21
 
-
 
22
 
-
 
23
SIGNAL (SIG_OVERFLOW0)    // 8kHz
-
 
24
{
-
 
25
    static unsigned char cnt_1ms = 1,cnt = 0;
-
 
26
    unsigned char pieper_ein = 0;
-
 
Line -... Line 7...
-
 
7
uint16_t BeepModulation = 0xFFFF;
-
 
8
 
-
 
9
 
-
 
10
 
-
 
11
/*****************************************************/
-
 
12
/*              Initialize Timer 0                   */
-
 
13
/*****************************************************/
-
 
14
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
-
 
15
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
-
 
16
void TIMER0_Init(void)
-
 
17
{
-
 
18
        uint8_t sreg = SREG;
-
 
19
 
-
 
20
        // disable all interrupts before reconfiguration
-
 
21
        cli();
-
 
22
 
-
 
23
        // set PB3 and PB4 as output for the PWM
-
 
24
        DDRB |= (1<<DDB4)|(1<<DDB3);
-
 
25
        PORTB &= ~((1<<PORTB4)|(1<<PORTB3));
-
 
26
 
-
 
27
        // Timer/Counter 0 Control Register A
-
 
28
 
-
 
29
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
-
 
30
    // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
-
 
31
    // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
-
 
32
    TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
-
 
33
    TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
-
 
34
 
-
 
35
        // Timer/Counter 0 Control Register B
-
 
36
 
-
 
37
        // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
-
 
38
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
-
 
39
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
-
 
40
 
-
 
41
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
-
 
42
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
-
 
43
    TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
-
 
44
 
-
 
45
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
-
 
46
    OCR0A =  0;  // for PB3
-
 
47
    OCR0B = 120; // for PB4
27
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
48
 
-
 
49
        // init Timer/Counter 0 Register
-
 
50
    TCNT0 = 0;
-
 
51
 
-
 
52
        // Timer/Counter 0 Interrupt Mask Register
-
 
53
        // enable timer overflow interrupt only
-
 
54
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
-
 
55
        TIMSK0 |= (1<<TOIE0);
-
 
56
 
-
 
57
        SREG = sreg;
-
 
58
}
-
 
59
 
-
 
60
 
-
 
61
 
-
 
62
/*****************************************************/
-
 
63
/*          Interrupt Routine of Timer 0             */
-
 
64
/*****************************************************/
-
 
65
ISR(TIMER0_OVF_vect)    // 9.765 kHz
-
 
66
{
-
 
67
    static uint8_t cnt_1ms = 1,cnt = 0;
-
 
68
    uint8_t Beeper_On = 0;
28
 
69
 
29
   if(!cnt--)
70
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
30
    {
71
        {
31
     cnt = 9;
72
         cnt = 9;
32
     cnt_1ms++;
73
         cnt_1ms++;
33
     cnt_1ms %= 2;
74
         cnt_1ms %= 2;
34
     if(!cnt_1ms) UpdateMotor = 1;
75
         if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz)
Line 35... Line -...
35
     CountMilliseconds++;
-
 
36
     }  
76
         CountMilliseconds++; // increment millisecond counter
37
 
77
        }
38
     if(beeptime > 1)
78
 
39
        {
79
 
40
        beeptime--;      
80
        // beeper on if duration is not over
41
        if(beeptime & BeepMuster)
81
        if(BeepTime > 1)
42
         {
82
        {
43
          pieper_ein = 1;
83
           BeepTime--; // decrement BeepTime
44
         }
84
           if(BeepTime & BeepModulation) Beeper_On = 1;
45
         else pieper_ein = 0;
85
           else Beeper_On = 0;
46
        }
86
        }
47
     else
87
        else // beeper off if duration is over
48
      {
88
        {
Line 49... Line -...
49
       pieper_ein = 0;
-
 
-
 
89
           Beeper_On = 0;
50
       BeepMuster = 0xffff;
90
           BeepModulation = 0xFFFF;
51
      }
91
        }
-
 
92
 
52
 
93
        // if beeper is on
53
 
94
        if(Beeper_On)
54
     if(pieper_ein)
95
        {
55
        {
96
                // set speaker port to high
56
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
97
                if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker at PD2
-
 
98
                else                      PORTC |= (1<<7); // Speaker at PC7
57
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
99
        }
58
        }
100
        else // beeper is off
59
     else  
101
        {
Line -... Line 102...
-
 
102
                // set speaker port to low
60
        {
103
                if(PlatinenVersion == 10) PORTD &= ~(1<<2);// Speaker at PD2
61
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
104
                else                      PORTC &= ~(1<<7);// Speaker at PC7
62
         else                      PORTC &= ~(1<<7);
105
        }
63
        }
106
 
64
 
107
        // update compass value if this option is enabled in the settings
Line 72... Line 115...
72
   {
115
        {
73
    if((cntKompass) && (cntKompass < 4000))
116
                if((cntKompass) && (cntKompass < 4000))
74
    {
117
                {
75
     KompassValue = cntKompass;
118
                        KompassValue = cntKompass;
76
    }
119
                }
77
//     if(cntKompass < 10) cntKompass = 10;
-
 
78
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
-
 
79
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
120
                KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
80
    cntKompass = 0;
121
                cntKompass = 0;
81
   }
122
                }
82
 }
123
        }
83
}
124
}
Line 84... Line -...
84
 
-
 
85
 
-
 
86
void Timer_Init(void)
-
 
87
{
-
 
88
    tim_main = SetDelay(10);
-
 
89
    TCCR0B = CK8;
-
 
90
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
-
 
91
    OCR0A =  0;
-
 
92
    OCR0B = 120;
-
 
93
    TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
-
 
94
    //OCR1  = 0x00;
-
 
95
 
-
 
96
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
-
 
97
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
-
 
98
   
-
 
99
//    TIMSK2 |= _BV(TOIE2);
-
 
100
TIMSK2 |= _BV(OCIE2A);
-
 
101
 
-
 
102
    TIMSK0 |= _BV(TOIE0);
-
 
103
    OCR2A = 10;
-
 
104
    TCNT2 = 0;
-
 
Line 105... Line 125...
105
   
125
 
106
}
-
 
107
 
126
 
108
// -----------------------------------------------------------------------
127
 
109
 
128
// -----------------------------------------------------------------------
110
unsigned int SetDelay (unsigned int t)
129
uint16_t SetDelay (uint16_t t)
111
{
130
{
112
//  TIMSK0 &= ~_BV(TOIE0);
131
//  TIMSK0 &= ~(1<<TOIE0);
Line 113... Line 132...
113
  return(CountMilliseconds + t + 1);                                            
132
  return(CountMilliseconds + t + 1);
114
//  TIMSK0 |= _BV(TOIE0);
133
//  TIMSK0 |= (1<<TOIE0);
115
}
134
}
116
 
135
 
117
// -----------------------------------------------------------------------
136
// -----------------------------------------------------------------------
118
char CheckDelay(unsigned int t)
137
int8_t CheckDelay(uint16_t t)
119
{
138
{
Line 120... Line 139...
120
//  TIMSK0 &= ~_BV(TOIE0);
139
//  TIMSK0 &= ~(1<<TOIE0);
121
  return(((t - CountMilliseconds) & 0x8000) >> 9);
140
  return(((t - CountMilliseconds) & 0x8000) >> 9);
122
//  TIMSK0 |= _BV(TOIE0);
141
//  TIMSK0 |= (1<<TOIE0);
123
}
142
}
124
 
143
 
125
// -----------------------------------------------------------------------
144
// -----------------------------------------------------------------------
126
void Delay_ms(unsigned int w)
145
void Delay_ms(uint16_t w)
Line -... Line 146...
-
 
146
{
127
{
147
 unsigned int t_stop;
128
 unsigned int akt;
148
 t_stop = SetDelay(w);
129
 akt = SetDelay(w);
149
 while (!CheckDelay(t_stop));
130
 while (!CheckDelay(akt));
150
}
131
}
151
 
132
 
152
// -----------------------------------------------------------------------
Line 133... Line -...
133
void Delay_ms_Mess(unsigned int w)
-
 
134
{
-
 
135
 unsigned int akt;
-
 
136
 akt = SetDelay(w);
-
 
137
 while (!CheckDelay(akt)) ANALOG_ON;
-
 
138
}
-
 
139
 
-
 
140
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
141
//  Servo ansteuern
-
 
142
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
-
 
143
SIGNAL(SIG_OUTPUT_COMPARE2A)
-
 
144
{
-
 
145
  static unsigned char timer = 10;
-
 
146
 
-
 
147
  if(!timer--)  
-
 
148
    {
-
 
149
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
-
 
150
     ServoValue =  Parameter_ServoNickControl;
-
 
151
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
-
 
152
     else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
-
 
153
     
-
 
154
     if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
-
 
155
     else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
-
 
156
 
-
 
157
     OCR2A = ServoValue;// + 75;
-
 
158
     timer = EE_Parameter.ServoNickRefresh;
-