Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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