Rev 1068 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1068 | thjac | 1 | |
2 | #include "main.h" |
||
3 | |||
4 | volatile unsigned int CountMilliseconds = 0; |
||
5 | volatile static unsigned int tim_main; |
||
6 | volatile unsigned char UpdateMotor = 0; |
||
7 | volatile unsigned int cntKompass = 0; |
||
8 | volatile unsigned int beeptime = 0; |
||
9 | volatile unsigned char SendSPI = 0; |
||
10 | volatile unsigned int ServoState = 40; |
||
11 | |||
12 | unsigned int BeepMuster = 0xffff; |
||
13 | unsigned int ServoValue = 0; |
||
14 | |||
15 | enum { |
||
16 | STOP = 0, |
||
17 | CK = 1, |
||
18 | CK8 = 2, |
||
19 | CK64 = 3, |
||
20 | CK256 = 4, |
||
21 | CK1024 = 5, |
||
22 | T0_FALLING_EDGE = 6, |
||
23 | T0_RISING_EDGE = 7 |
||
24 | }; |
||
25 | |||
26 | |||
27 | SIGNAL (SIG_OVERFLOW0) // 8kHz |
||
28 | { |
||
29 | static unsigned char cnt_1ms = 1,cnt = 0; |
||
30 | unsigned char pieper_ein = 0; |
||
31 | // TCNT0 -= 250;//TIMER_RELOAD_VALUE; |
||
32 | if(SendSPI) SendSPI--; |
||
33 | if(!cnt--) |
||
34 | { |
||
35 | cnt = 9; |
||
36 | cnt_1ms++; |
||
37 | cnt_1ms %= 2; |
||
38 | if(!cnt_1ms) UpdateMotor = 1; |
||
39 | CountMilliseconds++; |
||
40 | } |
||
41 | |||
42 | if(beeptime > 1) |
||
43 | { |
||
44 | beeptime--; |
||
45 | if(beeptime & BeepMuster) |
||
46 | { |
||
47 | pieper_ein = 1; |
||
48 | } |
||
49 | else pieper_ein = 0; |
||
50 | } |
||
51 | else |
||
52 | { |
||
53 | pieper_ein = 0; |
||
54 | BeepMuster = 0xffff; |
||
55 | } |
||
56 | |||
57 | |||
58 | if(pieper_ein) |
||
59 | { |
||
60 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
||
61 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
||
62 | } |
||
63 | else |
||
64 | { |
||
65 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
||
66 | else PORTC &= ~(1<<7); |
||
67 | } |
||
68 | |||
69 | if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) |
||
70 | { |
||
71 | if(PINC & 0x10) |
||
72 | { |
||
73 | cntKompass++; |
||
74 | } |
||
75 | else |
||
76 | { |
||
77 | if((cntKompass) && (cntKompass < 362)) |
||
78 | { |
||
79 | cntKompass += cntKompass / 41; |
||
80 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
||
81 | } |
||
82 | // if(cntKompass < 10) cntKompass = 10; |
||
83 | // KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L; |
||
84 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
||
85 | cntKompass = 0; |
||
86 | } |
||
87 | } |
||
88 | } |
||
89 | |||
90 | //---------------------------- |
||
91 | void Timer_Init(void) |
||
92 | { |
||
93 | tim_main = SetDelay(10); |
||
94 | TCCR0B = CK8; |
||
95 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
||
96 | OCR0A = 0; |
||
97 | OCR0B = 120; |
||
98 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
||
99 | //OCR1 = 0x00; |
||
100 | |||
101 | TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; |
||
102 | // TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22); // clk/256 |
||
103 | TCCR2B=(0<<CS20)|(0<<CS21)|(1<<CS22); // clk/64 |
||
104 | |||
105 | |||
106 | TIMSK2 |= _BV(OCIE2A); |
||
107 | |||
108 | TIMSK0 |= _BV(TOIE0); |
||
109 | OCR2A = 10; |
||
110 | TCNT2 = 0; |
||
111 | |||
112 | } |
||
113 | |||
114 | // ----------------------------------------------------------------------- |
||
115 | |||
116 | unsigned int SetDelay (unsigned int t) |
||
117 | { |
||
118 | // TIMSK0 &= ~_BV(TOIE0); |
||
119 | return(CountMilliseconds + t + 1); |
||
120 | // TIMSK0 |= _BV(TOIE0); |
||
121 | } |
||
122 | |||
123 | // ----------------------------------------------------------------------- |
||
124 | char CheckDelay(unsigned int t) |
||
125 | { |
||
126 | // TIMSK0 &= ~_BV(TOIE0); |
||
127 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
||
128 | // TIMSK0 |= _BV(TOIE0); |
||
129 | } |
||
130 | |||
131 | // ----------------------------------------------------------------------- |
||
132 | void Delay_ms(unsigned int w) |
||
133 | { |
||
134 | unsigned int akt; |
||
135 | akt = SetDelay(w); |
||
136 | while (!CheckDelay(akt)); |
||
137 | } |
||
138 | |||
139 | void Delay_ms_Mess(unsigned int w) |
||
140 | { |
||
141 | unsigned int akt; |
||
142 | akt = SetDelay(w); |
||
143 | while (!CheckDelay(akt)) ANALOG_ON; |
||
144 | } |
||
145 | |||
146 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
147 | // Servo ansteuern |
||
148 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
149 | SIGNAL(SIG_OVERFLOW2) { |
||
150 | |||
151 | if (ServoState > 0) |
||
152 | PORTD |= 0x80; |
||
153 | else |
||
154 | PORTD &= ~0x80; |
||
155 | |||
156 | TCCR2A = 3; |
||
157 | TIMSK2 &= ~_BV( TOIE2 ); |
||
158 | } |
||
159 | |||
160 | SIGNAL(SIG_OUTPUT_COMPARE2A) { |
||
161 | |||
162 | static unsigned char postPulse = 0x80; |
||
163 | static int filterServo = 100; |
||
164 | |||
165 | if( ServoState == 4 ) { |
||
166 | |||
167 | ServoValue = 0x0030; // Offset Part1 |
||
168 | filterServo = ( filterServo * 3 + (int) Parameter_ServoNickControl * 2 ) >> DIV_4; |
||
169 | ServoValue += filterServo; |
||
170 | |||
171 | // Min und Max vorverlegt, damit sich diese auf ServoNickControl beziehen und ggf. noch Nick-kompensiert werden |
||
172 | if( ServoValue < ( (int) EE_Parameter.ServoNickMin * 3 ) ) |
||
173 | ServoValue = (int) EE_Parameter.ServoNickMin * 3; |
||
174 | else if( ServoValue > ( (int) EE_Parameter.ServoNickMax * 3 ) ) |
||
175 | ServoValue = (int) EE_Parameter.ServoNickMax * 3; |
||
176 | |||
1076 | thjac | 177 | long integral; |
178 | |||
179 | /* Über Parameter läßt sich zwischen "+" und "X" - Formations |
||
180 | * umschalten (sh. parameter.h) |
||
181 | */ |
||
182 | if( PARAM_X_FORMATION ) { |
||
183 | integral = IntegralNick - IntegralRoll; |
||
184 | } else { |
||
185 | integral = IntegralNick; |
||
186 | } |
||
187 | |||
1068 | thjac | 188 | if( EE_Parameter.ServoNickCompInvert & 0x01 ) |
1076 | thjac | 189 | ServoValue += ( (long) ( (long) EE_Parameter.ServoNickComp * integral ) >> DIV_128 ) / ( 512L >> DIV_4 ); |
1068 | thjac | 190 | else |
1076 | thjac | 191 | ServoValue -= ( (long) ( (long) EE_Parameter.ServoNickComp * integral ) >> DIV_128 ) / ( 512L >> DIV_4 ); |
1068 | thjac | 192 | |
193 | DebugOut.Analog[20] = ServoValue; |
||
194 | |||
195 | if ( ( ServoValue % 255 ) < 45 ) { |
||
196 | ServoValue += 77; |
||
197 | postPulse = 0x60 - 77; |
||
198 | } else { |
||
199 | postPulse = 0x60; |
||
200 | } |
||
201 | |||
202 | OCR2A = 255 - ( ServoValue % 256 ); |
||
203 | TCCR2A = (1 << COM2A1 ) | ( 1 << COM2A0 ) | 3; |
||
204 | |||
205 | } else if( ( ServoState > 0 ) && ( ServoState < 4 ) ) { |
||
206 | |||
207 | if( ServoValue > 255 ) { |
||
208 | PORTD |= 0x80; |
||
209 | TCCR2A = 3; |
||
210 | ServoValue -= 255; |
||
211 | } else { |
||
212 | TCCR2A = ( 1 << COM2A1 ) | ( 0 << COM2A0 ) | 3; |
||
213 | OCR2A = postPulse; // Offset Part2 |
||
214 | ServoState = 1; |
||
215 | } |
||
216 | |||
217 | } else if( ServoState == 0 ) { |
||
218 | ServoState = (int) EE_Parameter.ServoNickRefresh << MUL_4; |
||
219 | PORTD &= ~0x80; |
||
220 | TCCR2A = 3; |
||
221 | } |
||
222 | |||
223 | ServoState--; |
||
224 | } |