Subversion Repositories FlightCtrl

Rev

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

Rev 1179 Rev 1180
-
 
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
2
// + Copyright (c) 04.2007 Holger Buss
-
 
3
// + Nur für den privaten Gebrauch
-
 
4
// + www.MikroKopter.com
-
 
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
6
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
-
 
7
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
-
 
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
-
 
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
-
 
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
-
 
11
// + Verkauf von Luftbildaufnahmen, usw.
-
 
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
-
 
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
-
 
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
-
 
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
-
 
18
// + eindeutig als Ursprung verlinkt werden
-
 
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
-
 
21
// + Benutzung auf eigene Gefahr
-
 
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
-
 
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
-
 
25
// + mit unserer Zustimmung zulässig
-
 
26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
-
 
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
-
 
30
// + this list of conditions and the following disclaimer.
-
 
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
-
 
32
// +     from this software without specific prior written permission.
-
 
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
-
 
34
// +     for non-commercial use (directly or indirectly)
-
 
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
-
 
36
// +     with our written permission
-
 
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
-
 
38
// +     clearly linked as origin
-
 
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
-
 
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-
 
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-
 
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-
 
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-
 
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-
 
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-
 
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-
 
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-
 
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-
 
49
// +  POSSIBILITY OF SUCH DAMAGE.
-
 
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1
#include <inttypes.h>
51
#include <inttypes.h>
2
#include <avr/io.h>
52
#include <avr/io.h>
3
#include <avr/interrupt.h>
53
#include <avr/interrupt.h>
4
#include "eeprom.h"
54
#include "eeprom.h"
5
#include "analog.h"
55
#include "analog.h"
6
#include "main.h"
56
#include "main.h"
7
#include "fc.h"
57
#include "fc.h"
8
#ifdef USE_KILLAGREG
58
#ifdef USE_KILLAGREG
9
#include "mm3.h"
59
#include "mm3.h"
10
#endif
60
#endif
11
#ifdef USE_MK3MAG
61
#ifdef USE_MK3MAG
12
#include "mk3mag.h"
62
#include "mk3mag.h"
13
#endif
63
#endif
14
 
64
 
15
volatile uint16_t CountMilliseconds = 0;
65
volatile uint16_t CountMilliseconds = 0;
16
volatile uint8_t UpdateMotor = 0;
66
volatile uint8_t UpdateMotor = 0;
17
volatile uint16_t cntKompass = 0;
67
volatile uint16_t cntKompass = 0;
18
volatile uint16_t BeepTime = 0;
68
volatile uint16_t BeepTime = 0;
19
volatile uint16_t BeepModulation = 0xFFFF;
69
volatile uint16_t BeepModulation = 0xFFFF;
20
 
70
 
21
#ifdef USE_NAVICTRL
71
#ifdef USE_NAVICTRL
22
volatile uint8_t SendSPI = 0;
72
volatile uint8_t SendSPI = 0;
23
#endif
73
#endif
24
 
74
 
25
 
75
 
26
 
76
 
27
/*****************************************************/
77
/*****************************************************/
28
/*              Initialize Timer 0                   */
78
/*              Initialize Timer 0                   */
29
/*****************************************************/
79
/*****************************************************/
30
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
80
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
31
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
81
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
32
void TIMER0_Init(void)
82
void TIMER0_Init(void)
33
{
83
{
34
        uint8_t sreg = SREG;
84
        uint8_t sreg = SREG;
35
 
85
 
36
        // disable all interrupts before reconfiguration
86
        // disable all interrupts before reconfiguration
37
        cli();
87
        cli();
38
 
88
 
39
        // configure speaker port as output
89
        // configure speaker port as output
40
        if(BoardRelease == 10)
90
        if(BoardRelease == 10)
41
        {       // Speaker at PD2
91
        {       // Speaker at PD2
42
                DDRD |= (1<<DDD2);
92
                DDRD |= (1<<DDD2);
43
                PORTD &= ~(1<<PORTD2);
93
                PORTD &= ~(1<<PORTD2);
44
        }
94
        }
45
        else
95
        else
46
        {       // Speaker at PC7
96
        {       // Speaker at PC7
47
                DDRC |= (1<<DDC7);
97
                DDRC |= (1<<DDC7);
48
                PORTC &= ~(1<<PORTC7);
98
                PORTC &= ~(1<<PORTC7);
49
        }
99
        }
50
 
100
 
51
        // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
101
        // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
52
        DDRB |= (1<<DDB4)|(1<<DDB3);
102
        DDRB |= (1<<DDB4)|(1<<DDB3);
53
        PORTB &= ~((1<<PORTB4)|(1<<PORTB3));
103
        PORTB &= ~((1<<PORTB4)|(1<<PORTB3));
54
 
104
 
55
        // Timer/Counter 0 Control Register A
105
        // Timer/Counter 0 Control Register A
56
 
106
 
57
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
107
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
58
    // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
108
        // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
59
    // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
109
        // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
60
    TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
110
        TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
61
    TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
111
        TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
62
 
112
 
63
        // Timer/Counter 0 Control Register B
113
        // Timer/Counter 0 Control Register B
64
 
114
 
65
        // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
115
        // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
66
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
116
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
67
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
117
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
68
 
118
 
69
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
119
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
70
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
120
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
71
    TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
121
        TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
72
 
122
 
73
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
123
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
74
    OCR0A =  0;  // for PB3
124
        OCR0A =  0;  // for PB3
75
    OCR0B = 120; // for PB4
125
        OCR0B = 120; // for PB4
76
 
126
 
77
        // init Timer/Counter 0 Register
127
        // init Timer/Counter 0 Register
78
    TCNT0 = 0;
128
        TCNT0 = 0;
79
 
129
 
80
        // Timer/Counter 0 Interrupt Mask Register
130
        // Timer/Counter 0 Interrupt Mask Register
81
        // enable timer overflow interrupt only
131
        // enable timer overflow interrupt only
82
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
132
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
83
        TIMSK0 |= (1<<TOIE0);
133
        TIMSK0 |= (1<<TOIE0);
84
 
134
 
85
        SREG = sreg;
135
        SREG = sreg;
86
}
136
}
87
 
137
 
88
 
138
 
89
 
139
 
90
/*****************************************************/
140
/*****************************************************/
91
/*          Interrupt Routine of Timer 0             */
141
/*          Interrupt Routine of Timer 0             */
92
/*****************************************************/
142
/*****************************************************/
93
ISR(TIMER0_OVF_vect)    // 9.765 kHz
143
ISR(TIMER0_OVF_vect)    // 9.765 kHz
94
{
144
{
95
    static uint8_t cnt_1ms = 1,cnt = 0;
145
        static uint8_t cnt_1ms = 1,cnt = 0;
96
    uint8_t Beeper_On = 0;
146
        uint8_t Beeper_On = 0;
97
 
147
 
98
#ifdef USE_NAVICTRL
148
#ifdef USE_NAVICTRL
99
        if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
149
        if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
100
#endif
150
#endif
101
 
151
 
102
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
152
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
103
        {
153
        {
104
         cnt = 9;
154
                cnt = 9;
105
         cnt_1ms++;
155
                cnt_1ms++;
106
         cnt_1ms %= 2;
156
                cnt_1ms %= 2;
107
         if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz)
157
                if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz)
108
         CountMilliseconds++; // increment millisecond counter
158
                CountMilliseconds++; // increment millisecond counter
109
        }
159
        }
110
 
160
 
111
 
161
 
112
        // beeper on if duration is not over
162
        // beeper on if duration is not over
113
        if(BeepTime)
163
        if(BeepTime)
114
        {
164
        {
115
           BeepTime--; // decrement BeepTime
165
                BeepTime--; // decrement BeepTime
116
           if(BeepTime & BeepModulation) Beeper_On = 1;
166
                if(BeepTime & BeepModulation) Beeper_On = 1;
117
           else Beeper_On = 0;
167
                else Beeper_On = 0;
118
        }
168
        }
119
        else // beeper off if duration is over
169
        else // beeper off if duration is over
120
        {
170
        {
121
           Beeper_On = 0;
171
                Beeper_On = 0;
122
           BeepModulation = 0xFFFF;
172
                BeepModulation = 0xFFFF;
123
        }
173
        }
124
 
174
 
125
        // if beeper is on
175
        // if beeper is on
126
        if(Beeper_On)
176
        if(Beeper_On)
127
        {
177
        {
128
                // set speaker port to high
178
                // set speaker port to high
129
                if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2
179
                if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2
130
                else                   PORTC |= (1<<PORTC7); // Speaker at PC7
180
                else                   PORTC |= (1<<PORTC7); // Speaker at PC7
131
        }
181
        }
132
        else // beeper is off
182
        else // beeper is off
133
        {
183
        {
134
                // set speaker port to low
184
                // set speaker port to low
135
                if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2
185
                if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2
136
                else                   PORTC &= ~(1<<PORTC7);// Speaker at PC7
186
                else                   PORTC &= ~(1<<PORTC7);// Speaker at PC7
137
        }
187
        }
138
 
188
 
139
        #ifndef USE_NAVICTRL
189
        #ifndef USE_NAVICTRL
140
        // update compass value if this option is enabled in the settings
190
        // update compass value if this option is enabled in the settings
141
        if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE))
191
        if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE))
142
        {
192
        {
143
        #ifdef USE_KILLAGREG
193
        #ifdef USE_KILLAGREG
144
                MM3_Update(); // read out mm3 board
194
                MM3_Update(); // read out mm3 board
145
        #endif
195
        #endif
146
        #ifdef USE_MK3MAG
196
        #ifdef USE_MK3MAG
147
                MK3MAG_Update(); // read out mk3mag pwm
197
                MK3MAG_Update(); // read out mk3mag pwm
148
        #endif
198
        #endif
149
        }
199
        }
150
        #endif
200
        #endif
151
}
201
}
152
 
202
 
153
 
203
 
154
 
204
 
155
// -----------------------------------------------------------------------
205
// -----------------------------------------------------------------------
156
uint16_t SetDelay (uint16_t t)
206
uint16_t SetDelay (uint16_t t)
157
{
207
{
158
  return(CountMilliseconds + t - 1);
208
        return(CountMilliseconds + t - 1);
159
}
209
}
160
 
210
 
161
// -----------------------------------------------------------------------
211
// -----------------------------------------------------------------------
162
int8_t CheckDelay(uint16_t t)
212
int8_t CheckDelay(uint16_t t)
163
{
213
{
164
  return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
214
        return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
165
}
215
}
166
 
216
 
167
// -----------------------------------------------------------------------
217
// -----------------------------------------------------------------------
168
void Delay_ms(uint16_t w)
218
void Delay_ms(uint16_t w)
169
{
219
{
170
 unsigned int t_stop;
220
        uint16_t t_stop;
171
 t_stop = SetDelay(w);
221
        t_stop = SetDelay(w);
172
 while (!CheckDelay(t_stop));
222
        while (!CheckDelay(t_stop));
173
}
223
}
174
 
224
 
175
// -----------------------------------------------------------------------
225
// -----------------------------------------------------------------------
176
void Delay_ms_Mess(uint16_t w)
226
void Delay_ms_Mess(uint16_t w)
177
{
227
{
178
 uint16_t t_stop;
228
        uint16_t t_stop;
179
 t_stop = SetDelay(w);
229
        t_stop = SetDelay(w);
180
 while (!CheckDelay(t_stop)) ADC_Enable();
230
        while (!CheckDelay(t_stop))
-
 
231
        {
-
 
232
                if(ADReady)
-
 
233
                {
-
 
234
                        ADReady = 0;
-
 
235
                        ADC_Enable();
-
 
236
                }
-
 
237
        }
181
}
238
}
182
 
239
 
183
 
240