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