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