Rev 953 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 953 | Rev 960 | ||
---|---|---|---|
1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
2 | #include <avr/io.h> |
2 | #include <avr/io.h> |
3 | #include <avr/interrupt.h> |
3 | #include <avr/interrupt.h> |
4 | #include "eeprom.h" |
4 | #include "eeprom.h" |
5 | #include "analog.h" |
5 | #include "analog.h" |
6 | #include "main.h" |
6 | #include "main.h" |
7 | #include "fc.h" |
7 | #include "fc.h" |
8 | #ifdef USE_KILLAGREG |
8 | #ifdef USE_KILLAGREG |
9 | #include "mm3.h" |
9 | #include "mm3.h" |
10 | #endif |
10 | #endif |
11 | #ifdef USE_MK3MAG |
11 | #ifdef USE_MK3MAG |
12 | #include "mk3mag.h" |
12 | #include "mk3mag.h" |
13 | #endif |
13 | #endif |
14 | 14 | ||
15 | volatile uint16_t CountMilliseconds = 0; |
15 | volatile uint16_t CountMilliseconds = 0; |
16 | volatile uint8_t UpdateMotor = 0; |
16 | volatile uint8_t UpdateMotor = 0; |
17 | volatile uint16_t cntKompass = 0; |
17 | volatile uint16_t cntKompass = 0; |
18 | volatile uint16_t BeepTime = 0; |
18 | volatile uint16_t BeepTime = 0; |
19 | volatile uint16_t BeepModulation = 0xFFFF; |
19 | volatile uint16_t BeepModulation = 0xFFFF; |
20 | 20 | ||
21 | #ifdef USE_NAVICTRL |
21 | #ifdef USE_NAVICTRL |
22 | volatile uint8_t SendSPI = 0; |
22 | volatile uint8_t SendSPI = 0; |
23 | #endif |
23 | #endif |
24 | 24 | ||
25 | 25 | ||
26 | 26 | ||
27 | /*****************************************************/ |
27 | /*****************************************************/ |
28 | /* Initialize Timer 0 */ |
28 | /* Initialize Timer 0 */ |
29 | /*****************************************************/ |
29 | /*****************************************************/ |
30 | // timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor |
30 | // timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor |
31 | // Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate |
31 | // Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate |
32 | void TIMER0_Init(void) |
32 | void TIMER0_Init(void) |
33 | { |
33 | { |
34 | uint8_t sreg = SREG; |
34 | uint8_t sreg = SREG; |
35 | 35 | ||
36 | // disable all interrupts before reconfiguration |
36 | // disable all interrupts before reconfiguration |
37 | cli(); |
37 | cli(); |
38 | 38 | ||
39 | // configure speaker port as output |
39 | // configure speaker port as output |
40 | if(BoardRelease == 10) |
40 | if(BoardRelease == 10) |
41 | { // Speaker at PD2 |
41 | { // Speaker at PD2 |
42 | DDRD |= (1<<DDD2); |
42 | DDRD |= (1<<DDD2); |
43 | PORTD &= ~(1<<PORTD2); |
43 | PORTD &= ~(1<<PORTD2); |
44 | } |
44 | } |
45 | else |
45 | else |
46 | { // Speaker at PC7 |
46 | { // Speaker at PC7 |
47 | DDRC |= (1<<DDC7); |
47 | DDRC |= (1<<DDC7); |
48 | PORTC &= ~(1<<PORTC7); |
48 | PORTC &= ~(1<<PORTC7); |
49 | } |
49 | } |
50 | 50 | ||
51 | // set PB3 and PB4 as output for the PWM used as aoffset for the pressure sensor |
51 | // set PB3 and PB4 as output for the PWM used as aoffset for the pressure sensor |
52 | DDRB |= (1<<DDB4)|(1<<DDB3); |
52 | DDRB |= (1<<DDB4)|(1<<DDB3); |
53 | PORTB &= ~((1<<PORTB4)|(1<<PORTB3)); |
53 | PORTB &= ~((1<<PORTB4)|(1<<PORTB3)); |
54 | - | ||
55 | if(BoardRelease == 10) |
- | |
56 | { |
- | |
57 | DDRD |= (1<<DDD2); |
- | |
58 | PORTD &= ~(1<<PORTD2); |
- | |
59 | - | ||
60 | } |
- | |
61 | else |
- | |
62 | { |
- | |
63 | DDRC |= (1<<DDC7); |
- | |
64 | PORTC &= ~(1<<PORTC7); |
- | |
65 | } |
- | |
66 | 54 | ||
67 | // Timer/Counter 0 Control Register A |
55 | // Timer/Counter 0 Control Register A |
68 | 56 | ||
69 | // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1) |
57 | // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1) |
70 | // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0) |
58 | // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0) |
71 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
59 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
72 | TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0)); |
60 | TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0)); |
73 | TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); |
61 | TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); |
74 | 62 | ||
75 | // Timer/Counter 0 Control Register B |
63 | // Timer/Counter 0 Control Register B |
76 | 64 | ||
77 | // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
65 | // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
78 | // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
66 | // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
79 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
67 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
80 | 68 | ||
81 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
69 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
82 | TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)); |
70 | TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)); |
83 | TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00); |
71 | TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00); |
84 | 72 | ||
85 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
73 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
86 | OCR0A = 0; // for PB3 |
74 | OCR0A = 0; // for PB3 |
87 | OCR0B = 120; // for PB4 |
75 | OCR0B = 120; // for PB4 |
88 | 76 | ||
89 | // init Timer/Counter 0 Register |
77 | // init Timer/Counter 0 Register |
90 | TCNT0 = 0; |
78 | TCNT0 = 0; |
91 | 79 | ||
92 | // Timer/Counter 0 Interrupt Mask Register |
80 | // Timer/Counter 0 Interrupt Mask Register |
93 | // enable timer overflow interrupt only |
81 | // enable timer overflow interrupt only |
94 | TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A)); |
82 | TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A)); |
95 | TIMSK0 |= (1<<TOIE0); |
83 | TIMSK0 |= (1<<TOIE0); |
96 | 84 | ||
97 | SREG = sreg; |
85 | SREG = sreg; |
98 | } |
86 | } |
99 | 87 | ||
100 | 88 | ||
101 | 89 | ||
102 | /*****************************************************/ |
90 | /*****************************************************/ |
103 | /* Interrupt Routine of Timer 0 */ |
91 | /* Interrupt Routine of Timer 0 */ |
104 | /*****************************************************/ |
92 | /*****************************************************/ |
105 | ISR(TIMER0_OVF_vect) // 9.765 kHz |
93 | ISR(TIMER0_OVF_vect) // 9.765 kHz |
106 | { |
94 | { |
107 | static uint8_t cnt_1ms = 1,cnt = 0; |
95 | static uint8_t cnt_1ms = 1,cnt = 0; |
108 | uint8_t Beeper_On = 0; |
96 | uint8_t Beeper_On = 0; |
109 | 97 | ||
110 | #ifdef USE_NAVICTRL |
98 | #ifdef USE_NAVICTRL |
111 | if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done |
99 | if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done |
112 | #endif |
100 | #endif |
113 | 101 | ||
114 | if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz) |
102 | if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz) |
115 | { |
103 | { |
116 | cnt = 9; |
104 | cnt = 9; |
117 | cnt_1ms++; |
105 | cnt_1ms++; |
118 | cnt_1ms %= 2; |
106 | cnt_1ms %= 2; |
119 | if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz) |
107 | if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz) |
120 | CountMilliseconds++; // increment millisecond counter |
108 | CountMilliseconds++; // increment millisecond counter |
121 | } |
109 | } |
122 | 110 | ||
123 | 111 | ||
124 | // beeper on if duration is not over |
112 | // beeper on if duration is not over |
125 | if(BeepTime) |
113 | if(BeepTime) |
126 | { |
114 | { |
127 | BeepTime--; // decrement BeepTime |
115 | BeepTime--; // decrement BeepTime |
128 | if(BeepTime & BeepModulation) Beeper_On = 1; |
116 | if(BeepTime & BeepModulation) Beeper_On = 1; |
129 | else Beeper_On = 0; |
117 | else Beeper_On = 0; |
130 | } |
118 | } |
131 | else // beeper off if duration is over |
119 | else // beeper off if duration is over |
132 | { |
120 | { |
133 | Beeper_On = 0; |
121 | Beeper_On = 0; |
134 | BeepModulation = 0xFFFF; |
122 | BeepModulation = 0xFFFF; |
135 | } |
123 | } |
136 | 124 | ||
137 | // if beeper is on |
125 | // if beeper is on |
138 | if(Beeper_On) |
126 | if(Beeper_On) |
139 | { |
127 | { |
140 | // set speaker port to high |
128 | // set speaker port to high |
141 | if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2 |
129 | if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2 |
142 | else PORTC |= (1<<PORTC7); // Speaker at PC7 |
130 | else PORTC |= (1<<PORTC7); // Speaker at PC7 |
143 | } |
131 | } |
144 | else // beeper is off |
132 | else // beeper is off |
145 | { |
133 | { |
146 | // set speaker port to low |
134 | // set speaker port to low |
147 | if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2 |
135 | if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2 |
148 | else PORTC &= ~(1<<PORTC7);// Speaker at PC7 |
136 | else PORTC &= ~(1<<PORTC7);// Speaker at PC7 |
149 | } |
137 | } |
150 | 138 | ||
151 | #ifndef USE_NAVICTRL |
139 | #ifndef USE_NAVICTRL |
152 | // update compass value if this option is enabled in the settings |
140 | // update compass value if this option is enabled in the settings |
153 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
141 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
154 | { |
142 | { |
155 | #ifdef USE_KILLAGREG |
143 | #ifdef USE_KILLAGREG |
156 | MM3_Update(); // read out mm3 board |
144 | MM3_Update(); // read out mm3 board |
157 | #endif |
145 | #endif |
158 | #ifdef USE_MK3MAG |
146 | #ifdef USE_MK3MAG |
159 | MK3MAG_Update(); // read out mk3mag pwm |
147 | MK3MAG_Update(); // read out mk3mag pwm |
160 | #endif |
148 | #endif |
161 | } |
149 | } |
162 | #endif |
150 | #endif |
163 | } |
151 | } |
164 | 152 | ||
165 | 153 | ||
166 | 154 | ||
167 | // ----------------------------------------------------------------------- |
155 | // ----------------------------------------------------------------------- |
168 | uint16_t SetDelay (uint16_t t) |
156 | uint16_t SetDelay (uint16_t t) |
169 | { |
157 | { |
170 | return(CountMilliseconds + t - 1); |
158 | return(CountMilliseconds + t - 1); |
171 | } |
159 | } |
172 | 160 | ||
173 | // ----------------------------------------------------------------------- |
161 | // ----------------------------------------------------------------------- |
174 | int8_t CheckDelay(uint16_t t) |
162 | int8_t CheckDelay(uint16_t t) |
175 | { |
163 | { |
176 | return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit |
164 | return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit |
177 | } |
165 | } |
178 | 166 | ||
179 | // ----------------------------------------------------------------------- |
167 | // ----------------------------------------------------------------------- |
180 | void Delay_ms(uint16_t w) |
168 | void Delay_ms(uint16_t w) |
181 | { |
169 | { |
182 | unsigned int t_stop; |
170 | unsigned int t_stop; |
183 | t_stop = SetDelay(w); |
171 | t_stop = SetDelay(w); |
184 | while (!CheckDelay(t_stop)); |
172 | while (!CheckDelay(t_stop)); |
185 | } |
173 | } |
186 | 174 | ||
187 | // ----------------------------------------------------------------------- |
175 | // ----------------------------------------------------------------------- |
188 | void Delay_ms_Mess(uint16_t w) |
176 | void Delay_ms_Mess(uint16_t w) |
189 | { |
177 | { |
190 | uint16_t t_stop; |
178 | uint16_t t_stop; |
191 | t_stop = SetDelay(w); |
179 | t_stop = SetDelay(w); |
192 | while (!CheckDelay(t_stop)) ADC_Enable(); |
180 | while (!CheckDelay(t_stop)) ADC_Enable(); |
193 | } |
181 | } |
194 | 182 | ||
195 | 183 |