Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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