Subversion Repositories FlightCtrl

Rev

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

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