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