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