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