Rev 2099 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1910 | - | 1 | #include <inttypes.h> |
2 | #include <avr/io.h> |
||
3 | #include <avr/interrupt.h> |
||
4 | #include "eeprom.h" |
||
5 | #include "analog.h" |
||
6 | |||
7 | // for debugging! |
||
8 | #include "uart0.h" |
||
9 | #include "output.h" |
||
10 | |||
11 | #ifdef USE_MK3MAG |
||
12 | #include "mk3mag.h" |
||
13 | #endif |
||
14 | |||
15 | volatile uint16_t millisecondsCount = 0; |
||
16 | volatile uint8_t runFlightControl = 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 |
||
24 | |||
25 | /***************************************************** |
||
26 | * Initialize Timer 0 |
||
27 | *****************************************************/ |
||
28 | // timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor |
||
29 | // Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate |
||
30 | void timer0_init(void) { |
||
31 | uint8_t sreg = SREG; |
||
32 | |||
33 | // disable all interrupts before reconfiguration |
||
34 | cli(); |
||
35 | |||
36 | // Configure speaker port as output. |
||
37 | |||
38 | if (BoardRelease == 10) { // Speaker at PD2 |
||
39 | DDRD |= (1 << DDD2); |
||
40 | PORTD &= ~(1 << PORTD2); |
||
41 | } else { // Speaker at PC7 |
||
42 | DDRC |= (1 << DDC7); |
||
43 | PORTC &= ~(1 << PORTC7); |
||
44 | } |
||
45 | |||
46 | // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor |
||
47 | DDRB |= (1 << DDB4) | (1 << DDB3); |
||
48 | PORTB &= ~((1 << PORTB4) | (1 << PORTB3)); |
||
49 | |||
50 | // Timer/Counter 0 Control Register A |
||
51 | |||
52 | // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1) |
||
53 | // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0) |
||
54 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
||
55 | TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0)); |
||
56 | TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00); |
||
57 | |||
58 | // Timer/Counter 0 Control Register B |
||
59 | |||
60 | // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
||
61 | // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
||
62 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
||
63 | |||
64 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
||
65 | TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02)); |
||
66 | TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00); |
||
67 | |||
68 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
||
69 | OCR0A = 0; // for PB3 |
||
70 | OCR0B = 120; // for PB4 |
||
71 | |||
72 | // init Timer/Counter 0 Register |
||
73 | TCNT0 = 0; |
||
74 | |||
75 | // Timer/Counter 0 Interrupt Mask Register |
||
76 | // enable timer overflow interrupt only |
||
77 | TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A)); |
||
78 | TIMSK0 |= (1 << TOIE0); |
||
79 | |||
80 | SREG = sreg; |
||
81 | } |
||
82 | |||
83 | /*****************************************************/ |
||
84 | /* Interrupt Routine of Timer 0 */ |
||
85 | /*****************************************************/ |
||
86 | ISR(TIMER0_OVF_vect) |
||
87 | { // 9765.625 Hz |
||
88 | static uint8_t cnt_1ms = 1, cnt = 0; |
||
89 | uint8_t beeper_On = 0; |
||
90 | |||
91 | #ifdef USE_NAVICTRL |
||
92 | if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done |
||
93 | #endif |
||
94 | |||
95 | if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz) |
||
96 | cnt = 9; |
||
97 | cnt_1ms ^= 1; |
||
98 | if (!cnt_1ms) { |
||
99 | if (runFlightControl == 1) |
||
100 | DebugOut.Digital[1] |= DEBUG_MAINLOOP_TIMER; |
||
101 | else |
||
102 | DebugOut.Digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
||
103 | runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz) |
||
104 | } |
||
105 | millisecondsCount++; // increment millisecond counter |
||
106 | } |
||
107 | |||
108 | // beeper on if duration is not over |
||
109 | if (beepTime) { |
||
110 | beepTime--; // decrement BeepTime |
||
111 | if (beepTime & beepModulation) |
||
112 | beeper_On = 1; |
||
113 | else |
||
114 | beeper_On = 0; |
||
115 | } else { // beeper off if duration is over |
||
116 | beeper_On = 0; |
||
117 | beepModulation = 0xFFFF; |
||
118 | } |
||
119 | |||
120 | // if beeper is on |
||
121 | if (beeper_On) { |
||
122 | // set speaker port to high. |
||
123 | if (BoardRelease == 10) |
||
124 | PORTD |= (1 << PORTD2); // Speaker at PD2 |
||
125 | else |
||
126 | PORTC |= (1 << PORTC7); // Speaker at PC7 |
||
127 | } else { // beeper is off |
||
128 | // set speaker port to low |
||
129 | if (BoardRelease == 10) |
||
130 | PORTD &= ~(1 << PORTD2);// Speaker at PD2 |
||
131 | else |
||
132 | PORTC &= ~(1 << PORTC7);// Speaker at PC7 |
||
133 | } |
||
134 | |||
135 | #ifndef USE_NAVICTRL |
||
136 | // update compass value if this option is enabled in the settings |
||
137 | if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
||
138 | #ifdef USE_MK3MAG |
||
139 | MK3MAG_Update(); // read out mk3mag pwm |
||
140 | #endif |
||
141 | } |
||
142 | #endif |
||
143 | } |
||
144 | |||
145 | // ----------------------------------------------------------------------- |
||
146 | uint16_t setDelay(uint16_t t) { |
||
147 | return (millisecondsCount + t - 1); |
||
148 | } |
||
149 | |||
150 | // ----------------------------------------------------------------------- |
||
151 | int8_t checkDelay(uint16_t t) { |
||
152 | return (((t - millisecondsCount) & 0x8000) >> 8); // check sign bit |
||
153 | } |
||
154 | |||
155 | // ----------------------------------------------------------------------- |
||
156 | void delay_ms(uint16_t w) { |
||
157 | uint16_t t_stop = setDelay(w); |
||
158 | while (!checkDelay(t_stop)) |
||
159 | ; |
||
160 | } |
||
161 | |||
162 | // ----------------------------------------------------------------------- |
||
163 | void delay_ms_Mess(uint16_t w) { |
||
164 | uint16_t t_stop; |
||
165 | t_stop = setDelay(w); |
||
166 | while (!checkDelay(t_stop)) { |
||
167 | if (analogDataReady) { |
||
168 | analogDataReady = 0; |
||
169 | analog_start(); |
||
170 | } |
||
171 | } |
||
172 | } |