Subversion Repositories FlightCtrl

Rev

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