Subversion Repositories FlightCtrl

Rev

Rev 979 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
838 MikeW 1
#include "main.h"
1042 MikeW 2
#include "FlightControl.h"
3
 
838 MikeW 4
volatile unsigned long cnt_ms = 0;
5
volatile unsigned int CountMilliseconds = 0;
6
volatile unsigned int Count8Khz = 0;
7
 
8
volatile static unsigned int tim_main;
9
volatile unsigned char UpdateMotor = 0;
10
volatile unsigned int cntKompass = 0;
11
volatile unsigned int beeptime = 0;
12
unsigned int BeepMuster = 0xffff;
13
int ServoValue = 0;
14
extern void MM3_Update(void);
966 MikeW 15
 
838 MikeW 16
enum {
17
  STOP             = 0,
966 MikeW 18
    CK               = 1,
19
    CK8              = 2,
20
    CK64             = 3,
21
    CK256            = 4,
22
    CK1024           = 5,
23
    T0_FALLING_EDGE  = 6,
24
    T0_RISING_EDGE   = 7
838 MikeW 25
};
26
 
27
 
28
SIGNAL (SIG_OVERFLOW0)    // 8kHz
29
{
966 MikeW 30
  static unsigned char cnt_1ms = 1,cnt = 0;
31
  unsigned char pieper_ein = 0;
32
 
33
  Count8Khz++;
34
 
35
  if(!cnt--)
36
  {
37
    cnt = 9;
38
    cnt_1ms++;
39
    cnt_1ms %= 2;
40
    if(!cnt_1ms)
838 MikeW 41
    {
966 MikeW 42
      UpdateMotor = 1;
43
    }
44
    CountMilliseconds++;
45
    cnt_ms++;
46
    // update compass value if this option is enabled in the settings
979 MikeW 47
#ifdef USE_COMPASS
966 MikeW 48
    MM3_Update(); // read out mm3 board
979 MikeW 49
#endif
966 MikeW 50
  }  
51
 
52
  if(beeptime > 1)
53
  {
54
    beeptime--;      
55
    if(beeptime & BeepMuster)
838 MikeW 56
    {
966 MikeW 57
      pieper_ein = 1;
838 MikeW 58
    }
966 MikeW 59
    else pieper_ein = 0;
60
  }
61
  else
62
  {
63
    pieper_ein = 0;
64
    BeepMuster = 0xffff;
65
  }
66
 
67
  if(pieper_ein)
68
  {
69
    PORTC |= (1<<7); // Speaker an PORTC.7
70
  }
71
  else  
72
  {
73
    PORTC &= ~(1<<7);
74
  }
75
 
76
#if 0
77
  if(PINC & 0x10)
78
  {
79
    cntKompass++;
80
  }
81
  else
82
  {
83
    if((cntKompass) && (cntKompass < 4000))
84
    {
85
      if(cntKompass < 10)
86
      {
87
        cntKompass = 10;
88
      }
89
      KompassValue = (((int) cntKompass-10) * 36) / 35;
838 MikeW 90
    }
966 MikeW 91
    cntKompass = 0;
92
  }
838 MikeW 93
#endif
94
}
95
 
96
 
97
void Timer_Init(void)
98
{
966 MikeW 99
  tim_main = SetDelay(10);
100
  TCCR0B = CK8;
101
  TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
102
  OCR0A =  0;
103
  OCR0B = 120;
104
  TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE;  // reload
105
 
106
  TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
107
  TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
108
  TIMSK2 |= _BV(OCIE2A);
109
 
110
  TIMSK0 |= _BV(TOIE0);
111
  OCR2A = 10;
112
  TCNT2 = 0;
838 MikeW 113
}
114
 
115
// -----------------------------------------------------------------------
116
 
117
unsigned int SetDelay (unsigned int t)
118
{
966 MikeW 119
  //  TIMSK0 &= ~_BV(TOIE0);
838 MikeW 120
  return(CountMilliseconds + t + 1);                                            
966 MikeW 121
  //  TIMSK0 |= _BV(TOIE0);
838 MikeW 122
}
123
 
124
// -----------------------------------------------------------------------
125
char CheckDelay(unsigned int t)
126
{
966 MikeW 127
  //  TIMSK0 &= ~_BV(TOIE0);
838 MikeW 128
  return(((t - CountMilliseconds) & 0x8000) >> 9);
966 MikeW 129
  //  TIMSK0 |= _BV(TOIE0);
838 MikeW 130
}
131
 
132
// -----------------------------------------------------------------------
133
void Delay_ms(unsigned int w)
134
{
966 MikeW 135
  unsigned int akt;
136
  akt = SetDelay(w);
137
  while (!CheckDelay(akt));
838 MikeW 138
}
139
 
140
void Delay_ms_Mess(unsigned int w)
141
{
966 MikeW 142
  unsigned int akt;
143
  akt = SetDelay(w);
144
  while (!CheckDelay(akt)) ANALOG_ON;
838 MikeW 145
}
146
 
147
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
148
//  Servo ansteuern
149
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++                
150
SIGNAL(SIG_OUTPUT_COMPARE2A)
151
{
152
  static unsigned char timer = 10;
153
  if(!timer--)  
966 MikeW 154
  {
979 MikeW 155
 
156
    TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3;
157
    if(ServoValue < EE_Parameter.ServoNickMin)
158
    {
159
      ServoValue = EE_Parameter.ServoNickMin;
160
    }
161
    if(ServoValue > EE_Parameter.ServoNickMax)
162
    {
163
      ServoValue = EE_Parameter.ServoNickMax;
164
    }  
966 MikeW 165
 
979 MikeW 166
    OCR2A = ServoValue;
966 MikeW 167
    timer = EE_Parameter.ServoNickRefresh;
168
  }
169
  else
170
  {
171
    TCCR2A =3;
172
    PORTD&=~0x80;
979 MikeW 173
  }
838 MikeW 174
}