Subversion Repositories FlightCtrl

Rev

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