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