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