Rev 942 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 942 | Rev 957 | ||
---|---|---|---|
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 | 54 | ||
55 | if(BoardRelease == 10) |
55 | if(BoardRelease == 10) |
56 | { |
56 | { |
57 | DDRD |= (1<<DDD2); |
57 | DDRD |= (1<<DDD2); |
58 | PORTD &= ~(1<<PORTD2); |
58 | PORTD &= ~(1<<PORTD2); |
59 | 59 | ||
60 | } |
60 | } |
61 | else |
61 | else |
62 | { |
62 | { |
63 | DDRC |= (1<<DDC7); |
63 | DDRC |= (1<<DDC7); |
64 | PORTC &= ~(1<<PORTC7); |
64 | PORTC &= ~(1<<PORTC7); |
65 | } |
65 | } |
66 | 66 | ||
67 | // Timer/Counter 0 Control Register A |
67 | // Timer/Counter 0 Control Register A |
68 | 68 | ||
69 | // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1) |
69 | // 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) |
70 | // 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) |
71 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
72 | TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0)); |
72 | TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0)); |
73 | TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); |
73 | TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); |
74 | 74 | ||
75 | // Timer/Counter 0 Control Register B |
75 | // Timer/Counter 0 Control Register B |
76 | 76 | ||
77 | // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
77 | // 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 |
78 | // 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 |
79 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
80 | 80 | ||
81 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
81 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
82 | TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)); |
82 | TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)); |
83 | TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00); |
83 | TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00); |
84 | 84 | ||
85 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
85 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
86 | OCR0A = 0; // for PB3 |
86 | OCR0A = 0; // for PB3 |
87 | OCR0B = 120; // for PB4 |
87 | OCR0B = 120; // for PB4 |
88 | 88 | ||
89 | // init Timer/Counter 0 Register |
89 | // init Timer/Counter 0 Register |
90 | TCNT0 = 0; |
90 | TCNT0 = 0; |
91 | 91 | ||
92 | // Timer/Counter 0 Interrupt Mask Register |
92 | // Timer/Counter 0 Interrupt Mask Register |
93 | // enable timer overflow interrupt only |
93 | // enable timer overflow interrupt only |
94 | TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A)); |
94 | TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A)); |
95 | TIMSK0 |= (1<<TOIE0); |
95 | TIMSK0 |= (1<<TOIE0); |
96 | 96 | ||
97 | SREG = sreg; |
97 | SREG = sreg; |
98 | } |
98 | } |
99 | 99 | ||
100 | 100 | ||
101 | 101 | ||
102 | /*****************************************************/ |
102 | /*****************************************************/ |
103 | /* Interrupt Routine of Timer 0 */ |
103 | /* Interrupt Routine of Timer 0 */ |
104 | /*****************************************************/ |
104 | /*****************************************************/ |
105 | ISR(TIMER0_OVF_vect) // 9.765 kHz |
105 | ISR(TIMER0_OVF_vect) // 9.765 kHz |
106 | { |
106 | { |
107 | static uint8_t cnt_1ms = 1,cnt = 0; |
107 | static uint8_t cnt_1ms = 1,cnt = 0; |
108 | uint8_t Beeper_On = 0; |
108 | uint8_t Beeper_On = 0; |
109 | 109 | ||
110 | #ifdef USE_NAVICTRL |
110 | #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 |
111 | if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done |
112 | #endif |
112 | #endif |
113 | 113 | ||
114 | if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz) |
114 | if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz) |
115 | { |
115 | { |
116 | cnt = 9; |
116 | cnt = 9; |
117 | cnt_1ms++; |
117 | cnt_1ms++; |
118 | cnt_1ms %= 2; |
118 | cnt_1ms %= 2; |
119 | if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz) |
119 | if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz) |
120 | CountMilliseconds++; // increment millisecond counter |
120 | CountMilliseconds++; // increment millisecond counter |
121 | } |
121 | } |
122 | 122 | ||
123 | 123 | ||
124 | // beeper on if duration is not over |
124 | // beeper on if duration is not over |
125 | if(BeepTime) |
125 | if(BeepTime) |
126 | { |
126 | { |
127 | BeepTime--; // decrement BeepTime |
127 | BeepTime--; // decrement BeepTime |
128 | if(BeepTime & BeepModulation) Beeper_On = 1; |
128 | if(BeepTime & BeepModulation) Beeper_On = 1; |
129 | else Beeper_On = 0; |
129 | else Beeper_On = 0; |
130 | } |
130 | } |
131 | else // beeper off if duration is over |
131 | else // beeper off if duration is over |
132 | { |
132 | { |
133 | Beeper_On = 0; |
133 | Beeper_On = 0; |
134 | BeepModulation = 0xFFFF; |
134 | BeepModulation = 0xFFFF; |
135 | } |
135 | } |
136 | 136 | ||
137 | // if beeper is on |
137 | // if beeper is on |
138 | if(Beeper_On) |
138 | if(Beeper_On) |
139 | { |
139 | { |
140 | // set speaker port to high |
140 | // set speaker port to high |
141 | if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2 |
141 | if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2 |
142 | else PORTC |= (1<<PORTC7); // Speaker at PC7 |
142 | else PORTC |= (1<<PORTC7); // Speaker at PC7 |
143 | } |
143 | } |
144 | else // beeper is off |
144 | else // beeper is off |
145 | { |
145 | { |
146 | // set speaker port to low |
146 | // set speaker port to low |
147 | if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2 |
147 | if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2 |
148 | else PORTC &= ~(1<<PORTC7);// Speaker at PC7 |
148 | else PORTC &= ~(1<<PORTC7);// Speaker at PC7 |
149 | } |
149 | } |
- | 150 | ||
150 | 151 | #ifndef USE_NAVICTRL |
|
151 | // update compass value if this option is enabled in the settings |
152 | // update compass value if this option is enabled in the settings |
152 | if(ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE) |
153 | if(ParamSet.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) |
153 | { |
154 | { |
154 | #ifdef USE_KILLAGREG |
155 | #ifdef USE_KILLAGREG |
155 | MM3_Update(); // read out mm3 board |
156 | MM3_Update(); // read out mm3 board |
156 | #endif |
157 | #endif |
157 | #ifdef USE_MK3MAG |
158 | #ifdef USE_MK3MAG |
158 | MK3MAG_Update(); // read out mk3mag pwm |
159 | MK3MAG_Update(); // read out mk3mag pwm |
159 | #endif |
160 | #endif |
160 | } |
161 | } |
- | 162 | #endif |
|
161 | } |
163 | } |
162 | 164 | ||
163 | 165 | ||
164 | 166 | ||
165 | // ----------------------------------------------------------------------- |
167 | // ----------------------------------------------------------------------- |
166 | uint16_t SetDelay (uint16_t t) |
168 | uint16_t SetDelay (uint16_t t) |
167 | { |
169 | { |
168 | return(CountMilliseconds + t - 1); |
170 | return(CountMilliseconds + t - 1); |
169 | } |
171 | } |
170 | 172 | ||
171 | // ----------------------------------------------------------------------- |
173 | // ----------------------------------------------------------------------- |
172 | int8_t CheckDelay(uint16_t t) |
174 | int8_t CheckDelay(uint16_t t) |
173 | { |
175 | { |
174 | return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit |
176 | return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit |
175 | } |
177 | } |
176 | 178 | ||
177 | // ----------------------------------------------------------------------- |
179 | // ----------------------------------------------------------------------- |
178 | void Delay_ms(uint16_t w) |
180 | void Delay_ms(uint16_t w) |
179 | { |
181 | { |
180 | unsigned int t_stop; |
182 | unsigned int t_stop; |
181 | t_stop = SetDelay(w); |
183 | t_stop = SetDelay(w); |
182 | while (!CheckDelay(t_stop)); |
184 | while (!CheckDelay(t_stop)); |
183 | } |
185 | } |
184 | 186 | ||
185 | // ----------------------------------------------------------------------- |
187 | // ----------------------------------------------------------------------- |
186 | void Delay_ms_Mess(uint16_t w) |
188 | void Delay_ms_Mess(uint16_t w) |
187 | { |
189 | { |
188 | uint16_t t_stop; |
190 | uint16_t t_stop; |
189 | t_stop = SetDelay(w); |
191 | t_stop = SetDelay(w); |
190 | while (!CheckDelay(t_stop)) ADC_Enable(); |
192 | while (!CheckDelay(t_stop)) ADC_Enable(); |
191 | } |
193 | } |
192 | 194 | ||
193 | 195 |