Subversion Repositories FlightCtrl

Rev

Rev 891 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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