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;
496 Nick666 64
    TCNT2 = 0;    
463 Nick666 65
}
66
 
67
// -----------------------------------------------------------------------
68
 
69
unsigned int SetDelay (unsigned int t)
70
{
71
//  TIMSK0 &= ~_BV(TOIE0);
72
  return(CountMilliseconds + t + 1);                                            
73
//  TIMSK0 |= _BV(TOIE0);
74
}
75
 
76
// -----------------------------------------------------------------------
77
char CheckDelay(unsigned int t)
78
{
79
//  TIMSK0 &= ~_BV(TOIE0);
80
  return(((t - CountMilliseconds) & 0x8000) >> 9);
81
//  TIMSK0 |= _BV(TOIE0);
82
}
83
 
84
// -----------------------------------------------------------------------
85
void Delay_ms(unsigned int w)
86
{
87
 unsigned int akt;
88
 akt = SetDelay(w);
89
 while (!CheckDelay(akt));
90
}
91
 
92
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
93
//  Servo ansteuern
94
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
95
SIGNAL(SIG_OUTPUT_COMPARE2A)
96
{
97
  static unsigned char timer = 10;
98
 
99
  if(!timer--)  
100
    {
101
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
102
     ServoValue =  Parameter_ServoNickControl;
103
     if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
104
     else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
105
 
106
     if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
107
     else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
108
 
109
     //DebugOut.Analog[10] = ServoValue;     
110
     OCR2A = ServoValue;// + 75;
111
     timer = EE_Parameter.ServoNickRefresh;
112
    }
113
    else
114
    {
115
     TCCR2A =3;
116
     PORTD&=~0x80;
117
    }
118
}