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