Subversion Repositories FlightCtrl

Rev

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

Rev 690 Rev 694
1
#include <inttypes.h>
1
#include <inttypes.h>
2
#include <avr/io.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
3
#include <avr/interrupt.h>
4
#include "eeprom.h"
4
#include "eeprom.h"
5
#include "analog.h"
5
#include "analog.h"
6
#include "main.h"
6
#include "main.h"
7
#include "fc.h"
7
#include "fc.h"
-
 
8
#include "mm3.h"
8
 
9
 
9
volatile uint16_t CountMilliseconds = 0;
10
volatile uint16_t CountMilliseconds = 0;
10
volatile uint8_t UpdateMotor = 0;
11
volatile uint8_t UpdateMotor = 0;
11
volatile uint16_t cntKompass = 0;
12
volatile uint16_t cntKompass = 0;
12
volatile uint16_t BeepTime = 0;
13
volatile uint16_t BeepTime = 0;
13
volatile uint16_t BeepModulation = 0xFFFF;
14
volatile uint16_t BeepModulation = 0xFFFF;
14
 
15
 
15
 
16
 
16
 
17
 
17
/*****************************************************/
18
/*****************************************************/
18
/*              Initialize Timer 0                   */
19
/*              Initialize Timer 0                   */
19
/*****************************************************/
20
/*****************************************************/
20
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
21
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
21
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
22
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
22
void TIMER0_Init(void)
23
void TIMER0_Init(void)
23
{
24
{
24
        uint8_t sreg = SREG;
25
        uint8_t sreg = SREG;
25
 
26
 
26
        // disable all interrupts before reconfiguration
27
        // disable all interrupts before reconfiguration
27
        cli();
28
        cli();
28
 
29
 
29
        // set PB3 and PB4 as output for the PWM
30
        // set PB3 and PB4 as output for the PWM
30
        DDRB |= (1<<DDB4)|(1<<DDB3);
31
        DDRB |= (1<<DDB4)|(1<<DDB3);
31
        PORTB &= ~((1<<PORTB4)|(1<<PORTB3));
32
        PORTB &= ~((1<<PORTB4)|(1<<PORTB3));
-
 
33
 
-
 
34
        if(BoardRelease == 10)
-
 
35
        {
-
 
36
                DDRD |= (1<<DDD2);
-
 
37
                PORTD &= ~(1<<PORTD2);
-
 
38
 
-
 
39
        }
-
 
40
        else
-
 
41
        {
-
 
42
                DDRC |= (1<<DDC7);
-
 
43
                PORTC &= ~(1<<PORTC7);
-
 
44
        }
32
 
45
 
33
        // Timer/Counter 0 Control Register A
46
        // Timer/Counter 0 Control Register A
34
 
47
 
35
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
48
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
36
    // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
49
    // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
37
    // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
50
    // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
38
    TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
51
    TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
39
    TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
52
    TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
40
 
53
 
41
        // Timer/Counter 0 Control Register B
54
        // Timer/Counter 0 Control Register B
42
 
55
 
43
        // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
56
        // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
44
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
57
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
45
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
58
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
46
 
59
 
47
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
60
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
48
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
61
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
49
    TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
62
    TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
50
 
63
 
51
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
64
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
52
    OCR0A =  0;  // for PB3
65
    OCR0A =  0;  // for PB3
53
    OCR0B = 120; // for PB4
66
    OCR0B = 120; // for PB4
54
 
67
 
55
        // init Timer/Counter 0 Register
68
        // init Timer/Counter 0 Register
56
    TCNT0 = 0;
69
    TCNT0 = 0;
57
 
70
 
58
        // Timer/Counter 0 Interrupt Mask Register
71
        // Timer/Counter 0 Interrupt Mask Register
59
        // enable timer overflow interrupt only
72
        // enable timer overflow interrupt only
60
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
73
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
61
        TIMSK0 |= (1<<TOIE0);
74
        TIMSK0 |= (1<<TOIE0);
62
 
75
 
63
        SREG = sreg;
76
        SREG = sreg;
64
}
77
}
65
 
78
 
66
 
79
 
67
 
80
 
68
/*****************************************************/
81
/*****************************************************/
69
/*          Interrupt Routine of Timer 0             */
82
/*          Interrupt Routine of Timer 0             */
70
/*****************************************************/
83
/*****************************************************/
71
ISR(TIMER0_OVF_vect)    // 9.765 kHz
84
ISR(TIMER0_OVF_vect)    // 9.765 kHz
72
{
85
{
73
    static uint8_t cnt_1ms = 1,cnt = 0;
86
    static uint8_t cnt_1ms = 1,cnt = 0;
74
    uint8_t Beeper_On = 0;
87
    uint8_t Beeper_On = 0;
75
 
88
 
76
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
89
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
77
        {
90
        {
78
         cnt = 9;
91
         cnt = 9;
79
         cnt_1ms++;
92
         cnt_1ms++;
80
         cnt_1ms %= 2;
93
         cnt_1ms %= 2;
81
         if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz)
94
         if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz)
82
         CountMilliseconds++; // increment millisecond counter
95
         CountMilliseconds++; // increment millisecond counter
83
        }
96
        }
84
 
97
 
85
 
98
 
86
        // beeper on if duration is not over
99
        // beeper on if duration is not over
87
        if(BeepTime > 1)
100
        if(BeepTime > 1)
88
        {
101
        {
89
           BeepTime--; // decrement BeepTime
102
           BeepTime--; // decrement BeepTime
90
           if(BeepTime & BeepModulation) Beeper_On = 1;
103
           if(BeepTime & BeepModulation) Beeper_On = 1;
91
           else Beeper_On = 0;
104
           else Beeper_On = 0;
92
        }
105
        }
93
        else // beeper off if duration is over
106
        else // beeper off if duration is over
94
        {
107
        {
95
           Beeper_On = 0;
108
           Beeper_On = 0;
96
           BeepModulation = 0xFFFF;
109
           BeepModulation = 0xFFFF;
97
        }
110
        }
98
 
111
 
99
        // if beeper is on
112
        // if beeper is on
100
        if(Beeper_On)
113
        if(Beeper_On)
101
        {
114
        {
102
                // set speaker port to high
115
                // set speaker port to high
103
                if(BoardRelease == 10) PORTD |= (1<<2); // Speaker at PD2
116
                if(BoardRelease == 10) PORTD |= (1<<2); // Speaker at PD2
104
                else                      PORTC |= (1<<7); // Speaker at PC7
117
                else                      PORTC |= (1<<7); // Speaker at PC7
105
        }
118
        }
106
        else // beeper is off
119
        else // beeper is off
107
        {
120
        {
108
                // set speaker port to low
121
                // set speaker port to low
109
                if(BoardRelease == 10) PORTD &= ~(1<<2);// Speaker at PD2
122
                if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2
110
                else                      PORTC &= ~(1<<7);// Speaker at PC7
123
                else                      PORTC &= ~(1<<PORTC7);// Speaker at PC7
111
        }
124
        }
112
 
125
 
113
        // update compass value if this option is enabled in the settings
126
        // update compass value if this option is enabled in the settings
114
        if(ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)
127
        if(ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE)
115
        {
128
        {
116
                if(PINC & 0x10)
-
 
117
                {
-
 
118
                        cntKompass++;
129
                MM3_timer0();
119
                }
-
 
120
        else
-
 
121
        {
-
 
122
                if((cntKompass) && (cntKompass < 4000))
-
 
123
                {
-
 
124
                        KompassValue = cntKompass;
-
 
125
                }
-
 
126
                KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
-
 
127
                cntKompass = 0;
-
 
128
                }
-
 
129
        }
130
        }
130
}
131
}
131
 
132
 
132
 
133
 
133
 
134
 
134
// -----------------------------------------------------------------------
135
// -----------------------------------------------------------------------
135
uint16_t SetDelay (uint16_t t)
136
uint16_t SetDelay (uint16_t t)
136
{
137
{
137
//  TIMSK0 &= ~(1<<TOIE0);
-
 
138
  return(CountMilliseconds + t + 1);
138
  return(CountMilliseconds + t + 1);
139
//  TIMSK0 |= (1<<TOIE0);
-
 
140
}
139
}
141
 
140
 
142
// -----------------------------------------------------------------------
141
// -----------------------------------------------------------------------
143
int8_t CheckDelay(uint16_t t)
142
int8_t CheckDelay(uint16_t t)
144
{
143
{
145
//  TIMSK0 &= ~(1<<TOIE0);
-
 
146
  return(((t - CountMilliseconds) & 0x8000) >> 9);
144
  return(((t - CountMilliseconds) & 0x8000) >> 9); // check sign bit
147
//  TIMSK0 |= (1<<TOIE0);
-
 
148
}
145
}
149
 
146
 
150
// -----------------------------------------------------------------------
147
// -----------------------------------------------------------------------
151
void Delay_ms(uint16_t w)
148
void Delay_ms(uint16_t w)
152
{
149
{
153
 unsigned int t_stop;
150
 unsigned int t_stop;
154
 t_stop = SetDelay(w);
151
 t_stop = SetDelay(w);
155
 while (!CheckDelay(t_stop));
152
 while (!CheckDelay(t_stop));
156
}
153
}
157
 
154
 
158
// -----------------------------------------------------------------------
155
// -----------------------------------------------------------------------
159
void Delay_ms_Mess(uint16_t w)
156
void Delay_ms_Mess(uint16_t w)
160
{
157
{
161
 uint16_t t_stop;
158
 uint16_t t_stop;
162
 t_stop = SetDelay(w);
159
 t_stop = SetDelay(w);
163
 while (!CheckDelay(t_stop)) ADC_Enable();
160
 while (!CheckDelay(t_stop)) ADC_Enable();
164
}
161
}
165
 
162
 
166
 
163