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