Rev 885 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 885 | Rev 886 | ||
---|---|---|---|
Line -... | Line 1... | ||
- | 1 | #include <inttypes.h> |
|
- | 2 | #include <avr/io.h> |
|
- | 3 | #include <avr/interrupt.h> |
|
- | 4 | #include "eeprom.h" |
|
- | 5 | #include "analog.h" |
|
1 | #include "main.h" |
6 | #include "main.h" |
- | 7 | #include "fc.h" |
|
- | 8 | #ifdef USE_KILLAGREG |
|
- | 9 | #include "mm3.h" |
|
- | 10 | #endif |
|
- | 11 | #if !defined (USE_KILLAGREG) && !defined (USE_NAVICTRL) |
|
- | 12 | #include "mk3mag.h" |
|
- | 13 | #endif |
|
- | 14 | ||
- | 15 | volatile uint16_t CountMilliseconds = 0; |
|
- | 16 | volatile uint8_t UpdateMotor = 0; |
|
- | 17 | volatile uint16_t cntKompass = 0; |
|
- | 18 | volatile uint16_t BeepTime = 0; |
|
- | 19 | volatile uint16_t BeepModulation = 0xFFFF; |
|
- | 20 | ||
- | 21 | #ifdef USE_NAVICTRL |
|
- | 22 | volatile uint8_t SendSPI = 0; |
|
- | 23 | #endif |
|
Line 2... | Line -... | ||
2 | - | ||
3 | volatile unsigned int CountMilliseconds = 0; |
- | |
4 | volatile static unsigned int tim_main; |
- | |
5 | volatile unsigned char UpdateMotor = 0; |
- | |
6 | volatile unsigned int cntKompass = 0; |
- | |
7 | volatile unsigned int beeptime = 0; |
- | |
8 | volatile unsigned char SendSPI = 0; |
- | |
9 | - | ||
10 | unsigned int BeepMuster = 0xffff; |
- | |
11 | int ServoValue = 0; |
- | |
12 | - | ||
13 | enum { |
- | |
14 | STOP = 0, |
- | |
15 | CK = 1, |
- | |
16 | CK8 = 2, |
- | |
17 | CK64 = 3, |
- | |
18 | CK256 = 4, |
- | |
19 | CK1024 = 5, |
- | |
20 | T0_FALLING_EDGE = 6, |
- | |
21 | T0_RISING_EDGE = 7 |
- | |
22 | }; |
- | |
23 | - | ||
24 | - | ||
25 | SIGNAL (SIG_OVERFLOW0) // 8kHz |
- | |
26 | { |
- | |
27 | static unsigned char cnt_1ms = 1,cnt = 0; |
- | |
28 | unsigned char pieper_ein = 0; |
- | |
29 | // TCNT0 -= 250;//TIMER_RELOAD_VALUE; |
- | |
30 | if(SendSPI) SendSPI--; |
- | |
31 | if(!cnt--) |
- | |
32 | { |
- | |
33 | cnt = 9; |
- | |
34 | cnt_1ms++; |
- | |
35 | cnt_1ms %= 2; |
- | |
36 | if(!cnt_1ms) UpdateMotor = 1; |
- | |
37 | CountMilliseconds++; |
- | |
Line -... | Line 24... | ||
- | 24 | ||
- | 25 | ||
- | 26 | ||
- | 27 | /*****************************************************/ |
|
- | 28 | /* Initialize Timer 0 */ |
|
- | 29 | /*****************************************************/ |
|
38 | } |
30 | // timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor |
39 | 31 | // Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate |
|
40 | if(beeptime > 1) |
32 | void TIMER0_Init(void) |
- | 33 | { |
|
41 | { |
34 | uint8_t sreg = SREG; |
42 | beeptime--; |
35 | |
- | 36 | // disable all interrupts before reconfiguration |
|
- | 37 | cli(); |
|
43 | if(beeptime & BeepMuster) |
38 | |
44 | { |
39 | // configure speaker port as output |
- | 40 | if(BoardRelease == 10) |
|
45 | pieper_ein = 1; |
41 | { // Speaker at PD2 |
46 | } |
42 | DDRD |= (1<<DDD2); |
47 | else pieper_ein = 0; |
43 | PORTD &= ~(1<<PORTD2); |
48 | } |
44 | } |
49 | else |
45 | else |
50 | { |
46 | { // Speaker at PC7 |
51 | pieper_ein = 0; |
47 | DDRC |= (1<<DDC7); |
Line -... | Line 48... | ||
- | 48 | PORTC &= ~(1<<PORTC7); |
|
- | 49 | } |
|
- | 50 | ||
Line 52... | Line 51... | ||
52 | BeepMuster = 0xffff; |
51 | // set PB3 and PB4 as output for the PWM used as aoffset for the pressure sensor |
53 | } |
52 | DDRB |= (1<<DDB4)|(1<<DDB3); |
54 | 53 | PORTB &= ~((1<<PORTB4)|(1<<PORTB3)); |
|
55 | 54 | ||
- | 55 | if(BoardRelease == 10) |
|
56 | if(pieper_ein) |
56 | { |
57 | { |
57 | DDRD |= (1<<DDD2); |
58 | if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2 |
58 | PORTD &= ~(1<<PORTD2); |
59 | else PORTC |= (1<<7); // Speaker an PORTC.7 |
59 | |
60 | } |
60 | } |
61 | else |
61 | else |
Line -... | Line 62... | ||
- | 62 | { |
|
- | 63 | DDRC |= (1<<DDC7); |
|
- | 64 | PORTC &= ~(1<<PORTC7); |
|
- | 65 | } |
|
- | 66 | ||
- | 67 | // Timer/Counter 0 Control Register A |
|
- | 68 | ||
- | 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) |
|
- | 71 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
|
- | 72 | TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0)); |
|
- | 73 | TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00); |
|
- | 74 | ||
- | 75 | // Timer/Counter 0 Control Register B |
|
62 | { |
76 | |
- | 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 |
|
- | 79 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
|
- | 80 | ||
- | 81 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
|
- | 82 | TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02)); |
|
- | 83 | TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00); |
|
- | 84 | ||
- | 85 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
|
- | 86 | OCR0A = 0; // for PB3 |
|
- | 87 | OCR0B = 120; // for PB4 |
|
- | 88 | ||
- | 89 | // init Timer/Counter 0 Register |
|
- | 90 | TCNT0 = 0; |
|
- | 91 | ||
- | 92 | // Timer/Counter 0 Interrupt Mask Register |
|
- | 93 | // enable timer overflow interrupt only |
|
- | 94 | TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A)); |
|
- | 95 | TIMSK0 |= (1<<TOIE0); |
|
- | 96 | ||
- | 97 | SREG = sreg; |
|
- | 98 | } |
|
- | 99 | ||
- | 100 | ||
63 | if(PlatinenVersion == 10) PORTD &= ~(1<<2); |
101 | |
- | 102 | /*****************************************************/ |
|
- | 103 | /* Interrupt Routine of Timer 0 */ |
|
- | 104 | /*****************************************************/ |
|
64 | else PORTC &= ~(1<<7); |
105 | ISR(TIMER0_OVF_vect) // 9.765 kHz |
- | 106 | { |
|
- | 107 | static uint8_t cnt_1ms = 1,cnt = 0; |
|
- | 108 | uint8_t Beeper_On = 0; |
|
- | 109 | ||
65 | } |
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 |
|
66 | 112 | #endif |
|
- | 113 | ||
- | 114 | if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz) |
|
- | 115 | { |
|
67 | if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) |
116 | cnt = 9; |
68 | { |
117 | cnt_1ms++; |
69 | if(PINC & 0x10) |
118 | cnt_1ms %= 2; |
70 | { |
119 | if(!cnt_1ms) UpdateMotor = 1; // every 2nd run (976Hz/2 = 488 Hz) |
- | 120 | CountMilliseconds++; // increment millisecond counter |
|
71 | cntKompass++; |
121 | } |
72 | } |
122 | |
73 | else |
123 | |
- | 124 | // beeper on if duration is not over |
|
74 | { |
125 | if(BeepTime) |
75 | if((cntKompass) && (cntKompass < 362)) |
126 | { |
76 | { |
- | |
- | 127 | BeepTime--; // decrement BeepTime |
|
77 | cntKompass += cntKompass / 41; |
128 | if(BeepTime & BeepModulation) Beeper_On = 1; |
78 | if(cntKompass > 10) KompassValue = cntKompass - 10; else KompassValue = 0; |
129 | else Beeper_On = 0; |
79 | } |
130 | } |
- | 131 | else // beeper off if duration is over |
|
- | 132 | { |
|
- | 133 | Beeper_On = 0; |
|
- | 134 | BeepModulation = 0xFFFF; |
|
- | 135 | } |
|
- | 136 | ||
- | 137 | // if beeper is on |
|
80 | // if(cntKompass < 10) cntKompass = 10; |
138 | if(Beeper_On) |
- | 139 | { |
|
- | 140 | // set speaker port to high |
|
- | 141 | if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2 |
|
- | 142 | else PORTC |= (1<<PORTC7); // Speaker at PC7 |
|
- | 143 | } |
|
81 | // KompassValue = (unsigned long)((unsigned long)(cntKompass-10)*720L + 1L) / 703L; |
144 | else // beeper is off |
Line 82... | Line -... | ||
82 | KompassRichtung = ((540 + KompassValue - KompassStartwert) % 360) - 180; |
- | |
- | 145 | { |
|
83 | cntKompass = 0; |
146 | // set speaker port to low |
84 | } |
147 | if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2 |
85 | } |
148 | else PORTC &= ~(1<<PORTC7);// Speaker at PC7 |
86 | } |
- | |
87 | 149 | } |
|
88 | 150 | ||
89 | void Timer_Init(void) |
- | |
90 | { |
151 | // update compass value if this option is enabled in the settings |
91 | tim_main = SetDelay(10); |
152 | if(ParamSet.GlobalConfig & CFG_COMPASS_ACTIVE) |
- | 153 | { |
|
- | 154 | #ifdef USE_KILLAGREG |
|
92 | TCCR0B = CK8; |
155 | MM3_Update(); // read out mm3 board |
93 | TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM |
- | |
94 | OCR0A = 0; |
- | |
Line 95... | Line -... | ||
95 | OCR0B = 120; |
- | |
96 | TCNT0 = (unsigned char)-TIMER_RELOAD_VALUE; // reload |
- | |
Line 97... | Line -... | ||
97 | //OCR1 = 0x00; |
- | |
98 | - | ||
99 | TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3; |
- | |
100 | TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22); |
- | |
101 | - | ||
Line 102... | Line 156... | ||
102 | // TIMSK2 |= _BV(TOIE2); |
156 | #endif |
103 | TIMSK2 |= _BV(OCIE2A); |
- | |
104 | 157 | #if !defined (USE_KILLAGREG) && !defined (USE_NAVICTRL) |
|
105 | TIMSK0 |= _BV(TOIE0); |
158 | MK3MAG_Update(); // read out mk3mag pwm |
106 | OCR2A = 10; |
- | |
107 | TCNT2 = 0; |
159 | #endif |
108 | - | ||
109 | } |
160 | } |
Line 110... | Line 161... | ||
110 | 161 | } |
|
111 | // ----------------------------------------------------------------------- |
162 | |
112 | 163 | ||
113 | unsigned int SetDelay (unsigned int t) |
- | |
114 | { |
164 | |
115 | // TIMSK0 &= ~_BV(TOIE0); |
- | |
116 | return(CountMilliseconds + t + 1); |
165 | // ----------------------------------------------------------------------- |
Line 117... | Line 166... | ||
117 | // TIMSK0 |= _BV(TOIE0); |
166 | uint16_t SetDelay (uint16_t t) |
118 | } |
167 | { |
119 | 168 | return(CountMilliseconds + t + 1); |
|
120 | // ----------------------------------------------------------------------- |
169 | } |
121 | char CheckDelay(unsigned int t) |
170 | |
122 | { |
171 | // ----------------------------------------------------------------------- |
123 | // TIMSK0 &= ~_BV(TOIE0); |
172 | int8_t CheckDelay(uint16_t t) |
Line -... | Line 173... | ||
- | 173 | { |
|
124 | return(((t - CountMilliseconds) & 0x8000) >> 9); |
174 | return(((t - CountMilliseconds) & 0x8000) >> 9); // check sign bit |
125 | // TIMSK0 |= _BV(TOIE0); |
175 | } |
126 | } |
176 | |
127 | 177 | // ----------------------------------------------------------------------- |
|
128 | // ----------------------------------------------------------------------- |
178 | void Delay_ms(uint16_t w) |
129 | void Delay_ms(unsigned int w) |
179 | { |
Line 130... | Line -... | ||
130 | { |
- | |
131 | unsigned int akt; |
- | |
132 | akt = SetDelay(w); |
- | |
133 | while (!CheckDelay(akt)); |
- | |
134 | } |
- | |
135 | - | ||
136 | void Delay_ms_Mess(unsigned int w) |
- | |
137 | { |
- | |
138 | unsigned int akt; |
- | |
139 | akt = SetDelay(w); |
- | |
140 | while (!CheckDelay(akt)) ANALOG_ON; |
- | |
141 | } |
- | |
142 | - | ||
143 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
144 | // Servo ansteuern |
- | |
145 | // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
146 | SIGNAL(SIG_OUTPUT_COMPARE2A) |
- | |
147 | { |
- | |
148 | static unsigned char timer = 10; |
- | |
149 | - | ||
150 | if(!timer--) |
- | |
151 | { |
- | |
152 | TCCR2A=(1<<COM2A1)|(0<<COM2A0)|3; |
- | |
153 | ServoValue = Parameter_ServoNickControl; |
- | |
154 | if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512; |
- | |
155 | else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512; |
- |