Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
787 Nick666 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 beeptime = 0;
7
unsigned int BeepMuster = 0xffff;
857 Nick666 8
uint8_t ServoValue = 0;
787 Nick666 9
 
857 Nick666 10
enum
11
{
787 Nick666 12
  STOP             = 0,
13
  CK               = 1,
14
  CK8              = 2,
15
  CK64             = 3,
16
  CK256            = 4,
17
  CK1024           = 5,
18
  T0_FALLING_EDGE  = 6,
19
  T0_RISING_EDGE   = 7
20
};
21
 
22
 
23
SIGNAL (SIG_OVERFLOW0)    // 9,8kHz
24
{
25
    static uint8_t cnt_1ms = 1,cnt = 0;
857 Nick666 26
    uint8_t pieper_ein;
787 Nick666 27
 
28
   if(!cnt--)
29
    {
30
     cnt = 9;
31
     cnt_1ms++;
32
     cnt_1ms %= 2;
33
         if(!cnt_1ms) UpdateMotor = 1;
34
     CountMilliseconds++;
857 Nick666 35
    }
787 Nick666 36
 
857 Nick666 37
    if(beeptime > 1)
787 Nick666 38
        {
39
        beeptime--;      
40
        if(beeptime & BeepMuster)
41
         {
42
          pieper_ein = 1;
43
         }
44
         else pieper_ein = 0;
45
        }
857 Nick666 46
    else
787 Nick666 47
      {
48
       pieper_ein = 0;
49
       BeepMuster = 0xffff;
50
      }
51
 
52
 
857 Nick666 53
    if(pieper_ein)
787 Nick666 54
        {
857 Nick666 55
          #if (PlatinenVersion == 10)
56
                   PORTD |= (1<<2); // Speaker an PORTD.2
57
          #else
58
                   PORTC |= (1<<7); // Speaker an PORTC.7
59
                  #endif
787 Nick666 60
        }
857 Nick666 61
    else  
787 Nick666 62
        {
857 Nick666 63
         #if (PlatinenVersion == 10)
64
                  PORTD &= ~(1<<2);
65
         #else
66
                  PORTC &= ~(1<<7);
67
                 #endif
787 Nick666 68
        }
69
 
70
        if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) timer0_MM3();         // Kompass auslesen
71
}
72
 
73
 
74
void Timer_Init(void)
75
{
76
    TCCR0B = CK8;
77
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
78
    OCR0A =  0;
79
    OCR0B = 120;
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
unsigned int SetDelay (unsigned int t)
95
{
96
  return(CountMilliseconds + t + 1);                                            
97
}
98
 
99
// -----------------------------------------------------------------------
100
char CheckDelay(unsigned int t)
101
{
102
  return(((t - CountMilliseconds) & 0x8000) >> 9);
103
}
104
 
105
// -----------------------------------------------------------------------
106
void Delay_ms(unsigned int w)
107
{
108
 unsigned int akt;
109
 akt = SetDelay(w);
110
 while (!CheckDelay(akt));
111
}
112
 
113
void Delay_ms_Mess(unsigned int w)
114
{
115
 unsigned int akt;
116
 akt = SetDelay(w);
117
 while (!CheckDelay(akt)) ANALOG_ON;
118
}
119
 
120
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
121
//  Servo ansteuern
122
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
123
SIGNAL(SIG_OUTPUT_COMPARE2A)
124
{
857 Nick666 125
  static uint8_t timer = 10;
787 Nick666 126
 
127
  if(!timer--)  
128
    {
129
     TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;  
857 Nick666 130
     //ServoValue =  Parameter_ServoNickControl;
131
         ServoValue = (((uint16_t)Parameter_ServoNickControl * Cam_Travel) /255) + EE_Parameter.ServoNickMin;
132
    if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((uint16_t)EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
133
    else ServoValue -= ((uint16_t)EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
134
 
135
    if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
136
    else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;
787 Nick666 137
 
138
     OCR2A = ServoValue;// + 75;
139
     timer = EE_Parameter.ServoNickRefresh;
140
    }
141
    else
142
    {
857 Nick666 143
     TCCR2A = 3;
144
     PORTD &=~ 0x80;
787 Nick666 145
    }
146
}