Subversion Repositories FlightCtrl

Rev

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