Subversion Repositories FlightCtrl

Rev

Rev 1805 | Rev 1869 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1805 Rev 1821
Line 75... Line 75...
75
 * Initialize Timer 0                  
75
 * Initialize Timer 0                  
76
 *****************************************************/
76
 *****************************************************/
77
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
77
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
78
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
78
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
79
void timer0_init(void) {
79
void timer0_init(void) {
80
  uint8_t sreg = SREG;
80
        uint8_t sreg = SREG;
Line 81... Line 81...
81
 
81
 
82
  // disable all interrupts before reconfiguration
82
        // disable all interrupts before reconfiguration
Line 83... Line 83...
83
  cli();
83
        cli();
84
 
-
 
85
  // Configure speaker port as output.
-
 
86
       
-
 
87
  if(BoardRelease == 10) {      // Speaker at PD2
-
 
88
    DDRD |= (1<<DDD2);
-
 
89
    PORTD &= ~(1<<PORTD2);
-
 
90
  } else {      // Speaker at PC7
-
 
91
    DDRC |= (1<<DDC7);
-
 
92
    PORTC &= ~(1<<PORTC7);
-
 
93
  }
-
 
94
 
-
 
95
  // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
-
 
96
  DDRB |= (1<<DDB4)|(1<<DDB3);
-
 
97
  PORTB &= ~((1<<PORTB4)|(1<<PORTB3));
-
 
98
 
-
 
99
  // Timer/Counter 0 Control Register A
-
 
100
 
-
 
101
  // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
-
 
102
  // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
-
 
103
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
-
 
104
  TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
-
 
105
  TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
-
 
106
 
-
 
107
  // Timer/Counter 0 Control Register B
-
 
108
 
-
 
109
  // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
-
 
110
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
-
 
111
  // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
-
 
112
 
-
 
113
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
-
 
114
  TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
-
 
115
  TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
-
 
116
 
-
 
117
  // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
-
 
118
  OCR0A =  0;  // for PB3
-
 
119
  OCR0B = 120; // for PB4
-
 
120
 
-
 
121
  // init Timer/Counter 0 Register
-
 
122
  TCNT0 = 0;
-
 
123
 
-
 
124
  // Timer/Counter 0 Interrupt Mask Register
-
 
125
  // enable timer overflow interrupt only
-
 
Line -... Line 84...
-
 
84
 
-
 
85
        // Configure speaker port as output.
-
 
86
 
-
 
87
        if (BoardRelease == 10) { // Speaker at PD2
-
 
88
                DDRD |= (1 << DDD2);
-
 
89
                PORTD &= ~(1 << PORTD2);
-
 
90
        } else { // Speaker at PC7
-
 
91
                DDRC |= (1 << DDC7);
-
 
92
                PORTC &= ~(1 << PORTC7);
-
 
93
        }
-
 
94
 
-
 
95
        // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
-
 
96
        DDRB |= (1 << DDB4) | (1 << DDB3);
-
 
97
        PORTB &= ~((1 << PORTB4) | (1 << PORTB3));
-
 
98
 
-
 
99
        // Timer/Counter 0 Control Register A
-
 
100
 
-
 
101
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
-
 
102
        // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
-
 
103
        // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
-
 
104
        TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
-
 
105
        TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
-
 
106
 
-
 
107
        // Timer/Counter 0 Control Register B
-
 
108
 
-
 
109
        // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
-
 
110
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
-
 
111
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
-
 
112
 
-
 
113
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
-
 
114
        TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02));
-
 
115
        TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00);
-
 
116
 
-
 
117
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
-
 
118
        OCR0A = 0; // for PB3
-
 
119
        OCR0B = 120; // for PB4
-
 
120
 
-
 
121
        // init Timer/Counter 0 Register
-
 
122
        TCNT0 = 0;
-
 
123
 
-
 
124
        // Timer/Counter 0 Interrupt Mask Register
-
 
125
        // enable timer overflow interrupt only
126
  TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
126
        TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A));
127
  TIMSK0 |= (1<<TOIE0);
127
        TIMSK0 |= (1 << TOIE0);
Line 128... Line 128...
128
 
128
 
129
  SREG = sreg;
129
        SREG = sreg;
130
}
130
}
131
 
131
 
-
 
132
/*****************************************************/
132
/*****************************************************/
133
/*          Interrupt Routine of Timer 0             */
133
/*          Interrupt Routine of Timer 0             */
134
/*****************************************************/
134
/*****************************************************/
135
ISR(TIMER0_OVF_vect)
135
ISR(TIMER0_OVF_vect) {    // 9765.625 Hz
136
{ // 9765.625 Hz
136
  static uint8_t cnt_1ms = 1,cnt = 0;
137
        static uint8_t cnt_1ms = 1, cnt = 0;
137
  uint8_t Beeper_On = 0;
138
        uint8_t Beeper_On = 0;
138
 
139
 
139
#ifdef USE_NAVICTRL
140
#ifdef USE_NAVICTRL
140
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
141
        if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
141
#endif
142
#endif
142
 
143
 
143
  if(!cnt--) { // every 10th run (9.765kHz/10 = 976Hz) 
144
        if (!cnt--) { // every 10th run (9.765kHz/10 = 976Hz)
144
    cnt = 9;
145
                cnt = 9;
145
    cnt_1ms^=1;
146
                cnt_1ms ^= 1;
146
    if(!cnt_1ms) {
147
                if (!cnt_1ms) {
147
        runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
148
                        runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
148
    }
149
                }
149
    CountMilliseconds++; // increment millisecond counter
150
                CountMilliseconds++; // increment millisecond counter
150
  }
151
        }
151
 
152
 
152
  // beeper on if duration is not over
153
        // beeper on if duration is not over
153
  if(BeepTime) {
154
        if (BeepTime) {
-
 
155
                BeepTime--; // decrement BeepTime
154
    BeepTime--; // decrement BeepTime
156
                if (BeepTime & BeepModulation)
155
    if(BeepTime & BeepModulation) Beeper_On = 1;
157
                        Beeper_On = 1;
156
    else Beeper_On = 0;
158
                else
157
  }
159
                        Beeper_On = 0;
158
  else { // beeper off if duration is over
160
        } else { // beeper off if duration is over
159
    Beeper_On = 0;
161
                Beeper_On = 0;
160
    BeepModulation = 0xFFFF;
162
                BeepModulation = 0xFFFF;
161
  }
163
        }
-
 
164
 
162
 
165
        // if beeper is on
-
 
166
        if (Beeper_On) {
163
  // if beeper is on
167
                // set speaker port to high.
164
  if(Beeper_On) {
168
                if (BoardRelease == 10)
165
    // set speaker port to high.
169
                        PORTD |= (1 << PORTD2); // Speaker at PD2
-
 
170
                else
166
    if(BoardRelease == 10) PORTD |= (1<<PORTD2); // Speaker at PD2
171
                        PORTC |= (1 << PORTC7); // Speaker at PC7
-
 
172
        } else { // beeper is off
167
    else PORTC |= (1<<PORTC7); // Speaker at PC7
173
                // set speaker port to low
168
  } else { // beeper is off
174
                if (BoardRelease == 10)
169
    // set speaker port to low
175
                        PORTD &= ~(1 << PORTD2);// Speaker at PD2
170
    if(BoardRelease == 10) PORTD &= ~(1<<PORTD2);// Speaker at PD2
176
                else
171
    else PORTC &= ~(1<<PORTC7);// Speaker at PC7
177
                        PORTC &= ~(1 << PORTC7);// Speaker at PC7
172
  }
178
        }
173
 
179
 
174
#ifndef USE_NAVICTRL
180
#ifndef USE_NAVICTRL
175
  // update compass value if this option is enabled in the settings
181
        // update compass value if this option is enabled in the settings
176
  if(staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE|CFG_GPS_ACTIVE)) {
182
        if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
177
#ifdef USE_MK3MAG
183
#ifdef USE_MK3MAG
178
    MK3MAG_Update(); // read out mk3mag pwm
184
                MK3MAG_Update(); // read out mk3mag pwm
Line 179... Line 185...
179
#endif
185
#endif
180
  }
186
        }
181
#endif
187
#endif
182
}
188
}
Line 183... Line 189...
183
 
189
 
184
// -----------------------------------------------------------------------
190
// -----------------------------------------------------------------------
185
uint16_t SetDelay (uint16_t t) {
191
uint16_t SetDelay(uint16_t t) {
186
  return(CountMilliseconds + t - 1);
192
        return (CountMilliseconds + t - 1);
Line 187... Line 193...
187
}
193
}
188
 
194
 
189
// -----------------------------------------------------------------------
195
// -----------------------------------------------------------------------
190
int8_t CheckDelay(uint16_t t) {
196
int8_t CheckDelay(uint16_t t) {
-
 
197
        return (((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
191
  return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
198
}
Line 192... Line 199...
192
}
199
 
193
 
200
// -----------------------------------------------------------------------
194
// -----------------------------------------------------------------------
201
void Delay_ms(uint16_t w) {
195
void Delay_ms(uint16_t w) {
202
        uint16_t t_stop = SetDelay(w);
196
  uint16_t t_stop = SetDelay(w);
203
        while (!CheckDelay(t_stop))
197
  while (!CheckDelay(t_stop));
204
                ;
198
}
205
}
199
 
206
 
200
// -----------------------------------------------------------------------
207
// -----------------------------------------------------------------------
201
void Delay_ms_Mess(uint16_t w) {
208
void Delay_ms_Mess(uint16_t w) {
202
  uint16_t t_stop;
209
        uint16_t t_stop;