Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1134 thjac 1
 
2
#include "main.h"
3
#include "parameter.h"
4
 
5
volatile unsigned int CountMilliseconds = 0;
6
volatile static unsigned int tim_main;
7
volatile unsigned char UpdateMotor = 0;
8
volatile unsigned int cntKompass = 0;
9
volatile unsigned int beeptime = 0;
10
volatile unsigned char SendSPI = 0;
11
volatile unsigned int ServoState = 40;
12
 
13
unsigned int BeepMuster = 0xffff;
14
unsigned int ServoValue = 0;
15
 
16
enum {
17
    STOP = 0,
18
    CK = 1,
19
    CK8 = 2,
20
    CK64 = 3,
21
    CK256 = 4,
22
    CK1024 = 5,
23
    T0_FALLING_EDGE = 6,
24
    T0_RISING_EDGE = 7
25
};
26
 
27
SIGNAL(SIG_OVERFLOW0) // 8kHz -> ist jetzt 10kHz
28
{
29
    static unsigned char cnt_1ms = 1, cnt = 0;
30
    unsigned char pieper_ein = 0;
31
    //    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
32
    if (SendSPI)
33
        SendSPI--;
34
    if (!cnt--) {
35
        cnt = 9;
36
//        cnt_1ms++;
37
//        cnt_1ms %= 2;
38
        cnt_1ms = !cnt_1ms;  // So ist das etwas einfacher
39
        if (!cnt_1ms)
40
            UpdateMotor = 1;
41
        CountMilliseconds++;
42
    }
43
 
44
    if (beeptime > 1) {
45
        beeptime--;
46
        if (beeptime & BeepMuster)
47
            pieper_ein = 1;
48
        else
49
            pieper_ein = 0;
50
    } else {
51
        pieper_ein = 0;
52
        BeepMuster = 0xffff;
53
    }
54
 
55
    if (pieper_ein) {
56
        if (PlatinenVersion == 10)
57
            PORTD |= (1 << 2); // Speaker an PORTD.2
58
        else
59
            PORTC |= (1 << 7); // Speaker an PORTC.7
60
    } else {
61
        if (PlatinenVersion == 10)
62
            PORTD &= ~(1 << 2);
63
        else
64
            PORTC &= ~(1 << 7);
65
    }
66
 
67
    if (EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) {
68
        if (PINC & 0x10)
69
            cntKompass++;
70
        else {
71
            if ((cntKompass) && (cntKompass < 362)) {
72
                cntKompass += cntKompass / 41;
73
                if (cntKompass > 10) KompassValue = cntKompass - 10;
74
                else KompassValue = 0;
75
            }
76
            //     if(cntKompass < 10) cntKompass = 10;
77
            //     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
78
            KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
79
            cntKompass = 0;
80
        }
81
    }
82
}
83
 
84
//----------------------------
85
 
86
void Timer_Init(void) {
87
    tim_main = SetDelay(10);
88
    TCCR0B = CK8;
89
    TCCR0A = (1 << COM0A1) | (1 << COM0B1) | 3; //fast PWM
90
    OCR0A = 0;
91
    OCR0B = 120;
92
    TCNT0 = (unsigned char) - TIMER_RELOAD_VALUE; // reload
93
    //OCR1  = 0x00;
94
 
95
    TCCR2A = (1 << COM2A1) | (1 << COM2A0) | 3;
96
    //    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);    // clk/256
97
    TCCR2B = (0 << CS20) | (0 << CS21) | (1 << CS22); // clk/64
98
 
99
 
100
    TIMSK2 |= _BV(OCIE2A);
101
 
102
    TIMSK0 |= _BV(TOIE0);
103
    OCR2A = 10;
104
    TCNT2 = 0;
105
 
106
}
107
 
108
// -----------------------------------------------------------------------
109
 
110
unsigned int SetDelay(unsigned int t) {
111
    //  TIMSK0 &= ~_BV(TOIE0);
112
    return (CountMilliseconds + t + 1);
113
    //  TIMSK0 |= _BV(TOIE0);
114
}
115
 
116
// -----------------------------------------------------------------------
117
 
118
char CheckDelay(unsigned int t) {
119
    //  TIMSK0 &= ~_BV(TOIE0);
120
    return (((t - CountMilliseconds) & 0x8000) >> 9);
121
    //  TIMSK0 |= _BV(TOIE0);
122
}
123
 
124
// -----------------------------------------------------------------------
125
 
126
void Delay_ms(unsigned int w) {
127
    unsigned int akt;
128
    akt = SetDelay(w);
129
    while (!CheckDelay(akt));
130
}
131
 
132
void Delay_ms_Mess(unsigned int w) {
133
    unsigned int akt;
134
    akt = SetDelay(w);
135
    while (!CheckDelay(akt))
136
        ANALOG_ON;
137
}
138
 
139
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
140
//  Servo ansteuern
141
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
142
 
143
SIGNAL(SIG_OVERFLOW2) {
144
 
145
    if (ServoState > 0)
146
        PORTD |= 0x80;
147
    else
148
        PORTD &= ~0x80;
149
 
150
    TCCR2A = 3;
151
    TIMSK2 &= ~_BV(TOIE2);
152
}
153
 
154
SIGNAL(SIG_OUTPUT_COMPARE2A) {
155
 
156
    static unsigned char postPulse = 0x80;
157
    static int filterServo = 100;
158
 
159
    if (ServoState == 4) {
160
 
161
        ServoValue = 0x0030; // Offset Part1
162
        filterServo = (filterServo * 3 + (int) Parameter_ServoNickControl * 2) >> DIV_4;
163
        ServoValue += filterServo;
164
 
165
        // Min und Max vorverlegt, damit sich diese auf ServoNickControl beziehen und ggf. noch Nick-kompensiert werden
166
        if (ServoValue < ((int) EE_Parameter.ServoNickMin * 3))
167
            ServoValue = (int) EE_Parameter.ServoNickMin * 3;
168
        else if (ServoValue > ((int) EE_Parameter.ServoNickMax * 3))
169
            ServoValue = (int) EE_Parameter.ServoNickMax * 3;
170
 
171
        long integral;
172
 
173
        /* Über Parameter läßt sich zwischen "+" und "X" - Formations
174
         * umschalten (sh. parameter.h)
175
         */
176
        if (PARAM_X_FORMATION)
177
            integral = IntegralNick - IntegralRoll;
178
        else
179
            integral = IntegralNick;
180
 
181
        if (EE_Parameter.ServoNickCompInvert & 0x01)
182
            ServoValue += ((long) ((long) EE_Parameter.ServoNickComp * integral) >> DIV_128) / (512L >> DIV_4);
183
        else
184
            ServoValue -= ((long) ((long) EE_Parameter.ServoNickComp * integral) >> DIV_128) / (512L >> DIV_4);
185
 
186
        DebugOut.Analog[20] = ServoValue;
187
 
188
        if ((ServoValue % 255) < 45) {
189
            ServoValue += 77;
190
            postPulse = 0x60 - 77;
191
        } else
192
            postPulse = 0x60;
193
 
194
        OCR2A = 255 - (ServoValue % 256);
195
        TCCR2A = (1 << COM2A1) | (1 << COM2A0) | 3;
196
 
197
    } else if ((ServoState > 0) && (ServoState < 4)) {
198
 
199
        if (ServoValue > 255) {
200
            PORTD |= 0x80;
201
            TCCR2A = 3;
202
            ServoValue -= 255;
203
        } else {
204
            TCCR2A = (1 << COM2A1) | (0 << COM2A0) | 3;
205
            OCR2A = postPulse; // Offset Part2
206
            ServoState = 1;
207
        }
208
 
209
    } else if (ServoState == 0) {
210
        ServoState = (int) EE_Parameter.ServoNickRefresh << MUL_4;
211
        PORTD &= ~0x80;
212
        TCCR2A = 3;
213
    }
214
 
215
    ServoState--;
216
}