Rev 1869 | Rev 1908 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1869 | Rev 1872 | ||
---|---|---|---|
Line 76... | Line 76... | ||
76 | * Initialize Timer 0 |
76 | * Initialize Timer 0 |
77 | *****************************************************/ |
77 | *****************************************************/ |
78 | // timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor |
78 | // timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor |
79 | // Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate |
79 | // Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate |
80 | void timer0_init(void) { |
80 | void timer0_init(void) { |
81 | uint8_t sreg = SREG; |
81 | uint8_t sreg = SREG; |
Line 82... | Line 82... | ||
82 | 82 | ||
83 | // disable all interrupts before reconfiguration |
83 | // disable all interrupts before reconfiguration |
Line 84... | Line 84... | ||
84 | cli(); |
84 | cli(); |
Line 85... | Line 85... | ||
85 | 85 | ||
86 | // Configure speaker port as output. |
86 | // Configure speaker port as output. |
87 | 87 | ||
88 | if (BoardRelease == 10) { // Speaker at PD2 |
88 | if (BoardRelease == 10) { // Speaker at PD2 |
89 | DDRD |= (1 << DDD2); |
89 | DDRD |= (1 << DDD2); |
90 | PORTD &= ~(1 << PORTD2); |
90 | PORTD &= ~(1 << PORTD2); |
91 | } else { // Speaker at PC7 |
91 | } else { // Speaker at PC7 |
Line 92... | Line 92... | ||
92 | DDRC |= (1 << DDC7); |
92 | DDRC |= (1 << DDC7); |
93 | PORTC &= ~(1 << PORTC7); |
93 | PORTC &= ~(1 << PORTC7); |
94 | } |
94 | } |
Line 95... | Line 95... | ||
95 | 95 | ||
Line 96... | Line 96... | ||
96 | // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor |
96 | // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor |
97 | DDRB |= (1 << DDB4) | (1 << DDB3); |
97 | DDRB |= (1 << DDB4) | (1 << DDB3); |
98 | PORTB &= ~((1 << PORTB4) | (1 << PORTB3)); |
98 | PORTB &= ~((1 << PORTB4) | (1 << PORTB3)); |
99 | 99 | ||
100 | // Timer/Counter 0 Control Register A |
100 | // Timer/Counter 0 Control Register A |
Line 101... | Line 101... | ||
101 | 101 | ||
Line 102... | Line 102... | ||
102 | // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1) |
102 | // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1) |
103 | // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0) |
103 | // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0) |
104 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
104 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
Line 105... | Line 105... | ||
105 | TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0)); |
105 | TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0)); |
106 | TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00); |
106 | TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00); |
107 | 107 | ||
Line 108... | Line 108... | ||
108 | // Timer/Counter 0 Control Register B |
108 | // Timer/Counter 0 Control Register B |
109 | 109 | ||
110 | // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
110 | // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
Line 111... | Line 111... | ||
111 | // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
111 | // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
112 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
112 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
Line 113... | Line 113... | ||
113 | 113 | ||
114 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
114 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
115 | TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02)); |
115 | TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02)); |
116 | TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00); |
116 | TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00); |
Line 117... | Line 117... | ||
117 | 117 | ||
118 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
118 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
Line 119... | Line 119... | ||
119 | OCR0A = 0; // for PB3 |
119 | OCR0A = 0; // for PB3 |
120 | OCR0B = 120; // for PB4 |
120 | OCR0B = 120; // for PB4 |
121 | 121 | ||
122 | // init Timer/Counter 0 Register |
122 | // init Timer/Counter 0 Register |
123 | TCNT0 = 0; |
123 | TCNT0 = 0; |
124 | 124 | ||
125 | // Timer/Counter 0 Interrupt Mask Register |
125 | // Timer/Counter 0 Interrupt Mask Register |
Line 126... | Line 126... | ||
126 | // enable timer overflow interrupt only |
126 | // enable timer overflow interrupt only |
127 | TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A)); |
127 | TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A)); |
128 | TIMSK0 |= (1 << TOIE0); |
128 | TIMSK0 |= (1 << TOIE0); |
Line 129... | Line 129... | ||
129 | 129 | ||
130 | SREG = sreg; |
130 | SREG = sreg; |
131 | } |
131 | } |
132 | 132 | ||
133 | /*****************************************************/ |
133 | /*****************************************************/ |
134 | /* Interrupt Routine of Timer 0 */ |
134 | /* Interrupt Routine of Timer 0 */ |
- | 135 | /*****************************************************/ |
|
- | 136 | ISR(TIMER0_OVF_vect) |
|
- | 137 | { // 9765.625 Hz |
|
135 | /*****************************************************/ |
138 | static uint8_t cnt_1ms = 1, cnt = 0; |
136 | ISR(TIMER0_OVF_vect) |
139 | uint8_t Beeper_On = 0; |
137 | { // 9765.625 Hz |
140 | |
138 | static uint8_t cnt_1ms = 1, cnt = 0; |
141 | #ifdef USE_NAVICTRL |
139 | uint8_t Beeper_On = 0; |
142 | if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done |
140 | 143 | #endif |
|
141 | #ifdef USE_NAVICTRL |
144 | |
142 | if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done |
145 | if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz) |
143 | #endif |
146 | cnt = 9; |
144 | 147 | cnt_1ms ^= 1; |
|
145 | if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz) |
148 | if (!cnt_1ms) { |
146 | cnt = 9; |
149 | if (runFlightControl == 1) |
147 | cnt_1ms ^= 1; |
150 | DebugOut.Digital[1] |= DEBUG_MAINLOOP_TIMER; |
148 | if (!cnt_1ms) { |
151 | else |
149 | runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz) |
152 | DebugOut.Digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
150 | DebugOut.Digital[0] |= DEBUG_MAINLOOP_TIMER; |
153 | runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz) |
151 | } |
154 | } |
152 | CountMilliseconds++; // increment millisecond counter |
155 | CountMilliseconds++; // increment millisecond counter |
153 | } |
156 | } |
154 | 157 | ||
155 | // beeper on if duration is not over |
158 | // beeper on if duration is not over |
156 | if (BeepTime) { |
159 | if (BeepTime) { |
157 | BeepTime--; // decrement BeepTime |
160 | BeepTime--; // decrement BeepTime |
158 | if (BeepTime & BeepModulation) |
161 | if (BeepTime & BeepModulation) |
159 | Beeper_On = 1; |
162 | Beeper_On = 1; |
160 | else |
163 | else |
161 | Beeper_On = 0; |
164 | Beeper_On = 0; |
162 | } else { // beeper off if duration is over |
165 | } else { // beeper off if duration is over |
163 | Beeper_On = 0; |
166 | Beeper_On = 0; |
164 | BeepModulation = 0xFFFF; |
167 | BeepModulation = 0xFFFF; |
Line 165... | Line 168... | ||
165 | } |
168 | } |
166 | 169 | ||
167 | // if beeper is on |
170 | // if beeper is on |
168 | if (Beeper_On) { |
171 | if (Beeper_On) { |
169 | // set speaker port to high. |
172 | // set speaker port to high. |
170 | if (BoardRelease == 10) |
173 | if (BoardRelease == 10) |
171 | PORTD |= (1 << PORTD2); // Speaker at PD2 |
174 | PORTD |= (1 << PORTD2); // Speaker at PD2 |
172 | else |
175 | else |
173 | PORTC |= (1 << PORTC7); // Speaker at PC7 |
176 | PORTC |= (1 << PORTC7); // Speaker at PC7 |
Line 174... | Line 177... | ||
174 | } else { // beeper is off |
177 | } else { // beeper is off |
175 | // set speaker port to low |
178 | // set speaker port to low |
176 | if (BoardRelease == 10) |
179 | if (BoardRelease == 10) |
177 | PORTD &= ~(1 << PORTD2);// Speaker at PD2 |
180 | PORTD &= ~(1 << PORTD2);// Speaker at PD2 |
Line 178... | Line 181... | ||
178 | else |
181 | else |
179 | PORTC &= ~(1 << PORTC7);// Speaker at PC7 |
182 | PORTC &= ~(1 << PORTC7);// Speaker at PC7 |
180 | } |
183 | } |
181 | 184 | ||
Line 182... | Line 185... | ||
182 | #ifndef USE_NAVICTRL |
185 | #ifndef USE_NAVICTRL |
183 | // update compass value if this option is enabled in the settings |
186 | // update compass value if this option is enabled in the settings |
184 | if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
187 | if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
185 | #ifdef USE_MK3MAG |
188 | #ifdef USE_MK3MAG |
186 | MK3MAG_Update(); // read out mk3mag pwm |
189 | MK3MAG_Update(); // read out mk3mag pwm |
187 | #endif |
190 | #endif |
Line 188... | Line 191... | ||
188 | } |
191 | } |
189 | #endif |
192 | #endif |
190 | } |
193 | } |
191 | 194 | ||
192 | // ----------------------------------------------------------------------- |
195 | // ----------------------------------------------------------------------- |
193 | uint16_t SetDelay(uint16_t t) { |
196 | uint16_t SetDelay(uint16_t t) { |
194 | return (CountMilliseconds + t - 1); |
197 | return (CountMilliseconds + t - 1); |
195 | } |
198 | } |
196 | 199 | ||
197 | // ----------------------------------------------------------------------- |
200 | // ----------------------------------------------------------------------- |
198 | int8_t CheckDelay(uint16_t t) { |
201 | int8_t CheckDelay(uint16_t t) { |