Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1 ingob 1
#include "main.h"
2
 
3
volatile unsigned int CountMilliseconds = 0;
4
volatile static unsigned int tim_main;
5
volatile unsigned char UpdateMotor = 0;
6
volatile unsigned int cntKompass = 0;
7
volatile unsigned int beeptime = 0;
8
int ServoValue = 0;
9
 
29 4ndreas 10
 
11
 
12
 
13
 
1 ingob 14
enum {
15
  STOP             = 0,
16
  CK               = 1,
17
  CK8              = 2,
18
  CK64             = 3,
19
  CK256            = 4,
20
  CK1024           = 5,
21
  T0_FALLING_EDGE  = 6,
22
  T0_RISING_EDGE   = 7
23
};
24
 
25
 
26
SIGNAL (SIG_OVERFLOW0)    // 8kHz
27
{
29 4ndreas 28
Blinki++;
1 ingob 29
    static unsigned char cnt_1ms = 1,cnt = 0;
30
//    TCNT0 -= 250;//TIMER_RELOAD_VALUE;
31
 
32
   if(!cnt--)
33
    {
34
     cnt = 9;
35
     cnt_1ms++;
36
     cnt_1ms %= 2;
37
     if(!cnt_1ms) UpdateMotor = 1;
38
     CountMilliseconds++;
39
     if(Timeout) Timeout--;
40
     }  
41
 
42
     if(beeptime > 1)
43
        {
44
        beeptime--;
45
        PORTD |= (1<<2);
46
        }
47
     else  
48
        PORTD &= ~(1<<2);
49
 
50
 if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV)
51
 {
52
  if(PINC & 0x10)
53
   {
54
    cntKompass++;
55
   }
56
  else
57
   {
58
    if((cntKompass) && (cntKompass < 4000))
59
    {
60
     KompassValue = cntKompass;
61
    }
62
//     if(cntKompass < 10) cntKompass = 10;
63
//     KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L;
64
     KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180;
65
    cntKompass = 0;
66
   }
67
 }
68
}
69
 
70
 
71
void Timer_Init(void)
72
{
73
    tim_main = SetDelay(10);
74
    TCCR0B = CK8;
75
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
76
    OCR0A =  0;
77
    OCR0B = 120;
78
    TCNT0 = -TIMER_RELOAD_VALUE;  // reload
79
    //OCR1  = 0x00;
80
 
81
    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
82
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
83
 
84
//    TIMSK2 |= _BV(TOIE2);
85
TIMSK2 |= _BV(OCIE2A);
86
 
87
    TIMSK0 |= _BV(TOIE0);
88
    OCR2A = 10;
89
    TCNT2 = 0;
90
 
91
}
92
 
93
// -----------------------------------------------------------------------
94
 
95
unsigned int SetDelay (unsigned int t)
96
{
97
//  TIMSK0 &= ~_BV(TOIE0);
98
  return(CountMilliseconds + t + 1);                                            
99
//  TIMSK0 |= _BV(TOIE0);
100
}
101
 
102
// -----------------------------------------------------------------------
103
char CheckDelay(unsigned int t)
104
{
105
//  TIMSK0 &= ~_BV(TOIE0);
106
  return(((t - CountMilliseconds) & 0x8000) >> 9);
107
//  TIMSK0 |= _BV(TOIE0);
108
}
109
 
110
// -----------------------------------------------------------------------
111
void Delay_ms(unsigned int w)
112
{
113
 unsigned int akt;
114
 akt = SetDelay(w);
115
 while (!CheckDelay(akt));
116
}
117
 
118
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
119
//  Servo ansteuern
120
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
121
SIGNAL(SIG_OUTPUT_COMPARE2A)
122
{
123
  static unsigned char timer = 10;
124
 
125
  if(!timer--)  
126
    {
127
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
128
     ServoValue =  Parameter_ServoNickControl;
129
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
130
     else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
131
 
132
     if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
133
     else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
134
 
135
     DebugOut.Analog[10] = ServoValue;    
136
     OCR2A = ServoValue;// + 75;
137
     timer = EE_Parameter.ServoNickRefresh;
138
    }
139
    else
140
    {
141
     TCCR2A =3;
142
     PORTD&=~0x80;
143
    }
144
}