Subversion Repositories FlightCtrl

Rev

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