Subversion Repositories FlightCtrl

Rev

Rev 1112 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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