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