Rev 1400 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1568 | acid | 1 | |
1400 | acid | 2 | #include "main.h" |
3 | #include "spectrum.h" |
||
1568 | acid | 4 | #include "heli.h" |
1400 | acid | 5 | |
6 | volatile unsigned int CountMilliseconds = 0; |
||
7 | volatile static unsigned int tim_main; |
||
8 | volatile unsigned char UpdateMotor = 0; |
||
9 | volatile unsigned int cntKompass = 0; |
||
10 | volatile unsigned int beeptime = 0; |
||
1568 | acid | 11 | volatile unsigned char SendSPI = 0; |
1400 | acid | 12 | |
13 | unsigned int BeepMuster = 0xffff; |
||
14 | |||
15 | volatile int16_t ServoNickValue = 0; |
||
16 | volatile int16_t ServoRollValue = 0; |
||
17 | |||
18 | |||
19 | enum { |
||
20 | STOP = 0, |
||
21 | CK = 1, |
||
22 | CK8 = 2, |
||
23 | CK64 = 3, |
||
24 | CK256 = 4, |
||
25 | CK1024 = 5, |
||
26 | T0_FALLING_EDGE = 6, |
||
27 | T0_RISING_EDGE = 7 |
||
28 | }; |
||
29 | |||
30 | |||
31 | SIGNAL (SIG_OVERFLOW0) // 9,7kHz |
||
32 | { |
||
33 | static unsigned char cnt_1ms = 1,cnt = 0; |
||
34 | unsigned char pieper_ein = 0; |
||
35 | if(SendSPI) SendSPI--; |
||
1568 | acid | 36 | if(SpektrumTimer) SpektrumTimer--; |
1400 | acid | 37 | |
38 | if(!cnt--) |
||
39 | { |
||
40 | cnt = 9; |
||
41 | cnt_1ms++; |
||
42 | cnt_1ms %= 2; |
||
43 | if(!cnt_1ms) UpdateMotor = 1; |
||
44 | CountMilliseconds++; |
||
45 | } |
||
46 | |||
47 | if(beeptime >= 1) |
||
48 | { |
||
49 | beeptime--; |
||
50 | if(beeptime & BeepMuster) |
||
51 | { |
||
52 | pieper_ein = 1; |
||
53 | } |
||
54 | else pieper_ein = 0; |
||
55 | } |
||
56 | else |
||
57 | { |
||
58 | pieper_ein = 0; |
||
59 | BeepMuster = 0xffff; |
||
60 | } |
||
61 | |||
62 | if(pieper_ein) |
||
63 | { |
||
64 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
||
65 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
||
66 | } |
||
67 | else |
||
68 | { |
||
69 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
||
70 | else PORTC &= ~(1<<7); |
||
71 | } |
||
72 | |||
73 | if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) |
||
74 | { |
||
75 | if(PINC & 0x10) |
||
76 | { |
||
77 | cntKompass++; |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | if((cntKompass) && (cntKompass < 362)) |
||
82 | { |
||
83 | cntKompass += cntKompass / 41; |
||
84 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
||
85 | } |
||
86 | // if(cntKompass < 10) cntKompass =r 10; |
||
87 | // KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L; |
||
88 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
89 | cntKompass = 0; |
||
90 | } |
||
91 | } |
||
92 | |||
93 | } |
||
94 | |||
95 | |||
96 | // ----------------------------------------------------------------------- |
||
97 | |||
98 | unsigned int SetDelay (unsigned int t) |
||
99 | { |
||
100 | // TIMSK0 &= ~_BV(TOIE0); |
||
101 | return(CountMilliseconds + t + 1); |
||
102 | // TIMSK0 |= _BV(TOIE0); |
||
103 | } |
||
104 | |||
105 | // ----------------------------------------------------------------------- |
||
106 | char CheckDelay(unsigned int t) |
||
107 | { |
||
108 | // TIMSK0 &= ~_BV(TOIE0); |
||
109 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
||
110 | // TIMSK0 |= _BV(TOIE0); |
||
111 | } |
||
112 | |||
113 | // ----------------------------------------------------------------------- |
||
114 | void Delay_ms(unsigned int w) |
||
115 | { |
||
116 | unsigned int akt; |
||
117 | akt = SetDelay(w); |
||
118 | while (!CheckDelay(akt)); |
||
119 | } |
||
120 | |||
121 | void Delay_ms_Mess(unsigned int w) |
||
122 | { |
||
123 | unsigned int akt; |
||
124 | akt = SetDelay(w); |
||
125 | while (!CheckDelay(akt)) if(AdReady) {AdReady = 0; ANALOG_ON;} |
||
126 | } |
||
127 | |||
128 | /*****************************************************/ |
||
129 | /* Initialize Timer 2 */ |
||
130 | /*****************************************************/ |
||
131 | // The timer 2 is used to generate the PWM at PD7 (J7) |
||
132 | // to control a camera servo for nick compensation. |
||
133 | void TIMER2_Init(void) |
||
134 | { |
||
135 | uint8_t sreg = SREG; |
||
136 | |||
137 | // disable all interrupts before reconfiguration |
||
138 | cli(); |
||
1568 | acid | 139 | |
1400 | acid | 140 | PORTD &= ~(1<<PORTD7); // set PD7 to low |
141 | |||
142 | DDRC |= (1<<DDC6); // set PC6 as output (Reset for HEF4017) |
||
143 | HEF4017R_ON; |
||
144 | // Timer/Counter 2 Control Register A |
||
145 | |||
146 | // Timer Mode is FastPWM with timer reload at OCR2A (Bits: WGM22 = 1, WGM21 = 1, WGM20 = 1) |
||
147 | // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0) |
||
148 | // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0) |
||
149 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0)); |
||
150 | TCCR2A |= (1<<WGM21)|(1<<WGM20); |
||
151 | |||
152 | // Timer/Counter 2 Control Register B |
||
153 | |||
154 | // Set clock divider for timer 2 to SYSKLOCK/32 = 20MHz / 32 = 625 kHz |
||
155 | // The timer increments from 0x00 to 0xFF with an update rate of 625 kHz or 1.6 us |
||
156 | // hence the timer overflow interrupt frequency is 625 kHz / 256 = 2.44 kHz or 0.4096 ms |
||
157 | |||
158 | // divider 32 (Bits: CS022 = 0, CS21 = 1, CS20 = 1) |
||
159 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS22)); |
||
160 | TCCR2B |= (1<<CS21)|(1<<CS20)|(1<<WGM22); |
||
161 | |||
162 | // Initialize the Timer/Counter 2 Register |
||
163 | TCNT2 = 0; |
||
164 | |||
165 | // Initialize the Output Compare Register A used for PWM generation on port PD7. |
||
166 | OCR2A = 255; |
||
167 | TCCR2A |= (1<<COM2A1); // set or clear at compare match depends on value of COM2A0 |
||
168 | |||
169 | // Timer/Counter 2 Interrupt Mask Register |
||
170 | // Enable timer output compare match A Interrupt only |
||
171 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
||
172 | TIMSK2 |= (1<<OCIE2A); |
||
173 | |||
174 | SREG = sreg; |
||
175 | } |
||
176 | |||
177 | //---------------------------- |
||
178 | void Timer_Init(void) |
||
179 | { |
||
180 | tim_main = SetDelay(10); |
||
181 | TCCR0B = CK8; |
||
182 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
||
183 | OCR0A = 0; |
||
184 | OCR0B = 120; |
||
185 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
||
186 | //OCR1 = 0x00; |
||
187 | |||
188 | TIMSK0 |= _BV(TOIE0); |
||
189 | } |
||
190 | |||
191 | |||
192 | /*****************************************************/ |
||
193 | /* Control Servo Position */ |
||
194 | /*****************************************************/ |
||
195 | |||
196 | ISR(TIMER2_COMPA_vect) |
||
197 | { |
||
198 | // frame len 22.5 ms = 14063 * 1.6 us |
||
199 | // stop pulse: 0.3 ms = 188 * 1.6 us |
||
200 | // min servo pulse: 0.6 ms = 375 * 1.6 us |
||
201 | // max servo pulse: 2.4 ms = 1500 * 1.6 us |
||
202 | // resolution: 1500 - 375 = 1125 steps |
||
203 | |||
204 | #define IRS_RUNTIME 127 |
||
205 | #define PPM_STOPPULSE 188 |
||
206 | // #define PPM_FRAMELEN (14063 |
||
207 | #define PPM_FRAMELEN (1757 * EE_Parameter.ServoNickRefresh) |
||
208 | #define MINSERVOPULSE 375 |
||
209 | #define MAXSERVOPULSE 1500 |
||
210 | #define SERVORANGE (MAXSERVOPULSE - MINSERVOPULSE) |
||
211 | |||
212 | static uint8_t PulseOutput = 0; |
||
213 | static uint16_t RemainingPulse = 0; |
||
214 | static uint16_t ServoFrameTime = 0; |
||
215 | static uint8_t ServoIndex = 0; |
||
216 | |||
217 | #define MULTIPLYER 4 |
||
218 | static int16_t ServoNickOffset = (255 / 2) * MULTIPLYER; // initial value near center positon |
||
219 | static int16_t ServoRollOffset = (255 / 2) * MULTIPLYER; // initial value near center positon |
||
220 | |||
1568 | acid | 221 | //----------------------------------------------------- |
222 | // PPM state machine, onboard demultiplexed by HEF4017 |
||
223 | //----------------------------------------------------- |
||
224 | if(!PulseOutput) // pulse output complete |
||
1400 | acid | 225 | { |
1568 | acid | 226 | if(TCCR2A & (1<<COM2A0)) // we had a low pulse |
1400 | acid | 227 | { |
1568 | acid | 228 | TCCR2A &= ~(1<<COM2A0);// make a high pulse |
229 | |||
230 | if(ServoIndex == 0) // if we are at the sync gap |
||
1400 | acid | 231 | { |
1568 | acid | 232 | RemainingPulse = PPM_FRAMELEN - ServoFrameTime; // generate sync gap by filling time to full frame time |
233 | ServoFrameTime = 0; // reset servo frame time |
||
234 | HEF4017R_ON; // enable HEF4017 reset |
||
235 | } |
||
236 | else // servo channels |
||
237 | { |
||
1400 | acid | 238 | RemainingPulse = MINSERVOPULSE + SERVORANGE/2; // center position ~ 1.5ms |
1568 | acid | 239 | RemainingPulse += 2 * servoValues[ServoIndex - 1]; // add channel value, factor of 2 because timer 1 increments 3.2µs |
1400 | acid | 240 | |
241 | // range servo pulse width |
||
242 | if(RemainingPulse > MAXSERVOPULSE ) RemainingPulse = MAXSERVOPULSE; // upper servo pulse limit |
||
243 | else if(RemainingPulse < MINSERVOPULSE ) RemainingPulse = MINSERVOPULSE; // lower servo pulse limit |
||
1568 | acid | 244 | // substract stop pulse width |
245 | RemainingPulse -= PPM_STOPPULSE; |
||
1400 | acid | 246 | // accumulate time for correct sync gap |
247 | ServoFrameTime += RemainingPulse; |
||
248 | } |
||
249 | } |
||
1568 | acid | 250 | else // we had a high pulse |
251 | { |
||
252 | TCCR2A |= (1<<COM2A0); // make a low pulse |
||
253 | // set pulsewidth to stop pulse width |
||
254 | RemainingPulse = PPM_STOPPULSE; |
||
255 | // accumulate time for correct sync gap |
||
256 | ServoFrameTime += RemainingPulse; |
||
257 | HEF4017R_OFF; // disable HEF4017 reset |
||
258 | ServoIndex++; // change to next servo channel |
||
259 | if(ServoIndex > EE_Parameter.ServoNickRefresh) ServoIndex = 0; // reset to the sync gap |
||
260 | } |
||
261 | // set pulse output active |
||
262 | PulseOutput = 1; |
||
263 | } |
||
1400 | acid | 264 | |
265 | // General pulse output generator |
||
266 | if(RemainingPulse > (255 + IRS_RUNTIME)) |
||
267 | { |
||
268 | OCR2A = 255; |
||
269 | RemainingPulse -= 255; |
||
270 | } |
||
271 | else |
||
272 | { |
||
273 | if(RemainingPulse > 255) // this is the 2nd last part |
||
274 | { |
||
275 | if((RemainingPulse - 255) < IRS_RUNTIME) |
||
276 | { |
||
277 | OCR2A = 255 - IRS_RUNTIME; |
||
278 | RemainingPulse -= 255 - IRS_RUNTIME; |
||
279 | |||
280 | } |
||
281 | else // last part > ISR_RUNTIME |
||
282 | { |
||
283 | OCR2A = 255; |
||
284 | RemainingPulse -= 255; |
||
285 | } |
||
286 | } |
||
287 | else // this is the last part |
||
288 | { |
||
289 | OCR2A = RemainingPulse; |
||
290 | RemainingPulse = 0; |
||
291 | PulseOutput = 0; // trigger to stop pulse |
||
292 | } |
||
293 | } // EOF general pulse output generator |
||
294 | } |