Subversion Repositories FlightCtrl

Rev

Rev 1539 | Details | Compare with Previous | Last modification | View Log | RSS feed

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