3,9 → 3,9 |
#include <avr/interrupt.h> |
#include "eeprom.h" |
#include "analog.h" |
#include "controlMixer.h" |
|
// for debugging! |
#include "uart0.h" |
#include "timer0.h" |
#include "output.h" |
|
#ifdef USE_MK3MAG |
12,11 → 12,10 |
#include "mk3mag.h" |
#endif |
|
volatile uint16_t millisecondsCount = 0; |
volatile uint32_t globalMillisClock = 0; |
volatile uint8_t runFlightControl = 0; |
volatile uint16_t cntKompass = 0; |
volatile uint16_t beepTime = 0; |
volatile uint16_t beepModulation = 0xFFFF; |
volatile uint16_t beepModulation = BEEP_MODULATION_NONE; |
|
#ifdef USE_NAVICTRL |
volatile uint8_t SendSPI = 0; |
34,8 → 33,7 |
cli(); |
|
// Configure speaker port as output. |
|
if (BoardRelease == 10) { // Speaker at PD2 |
if (boardRelease == 10) { // Speaker at PD2 |
DDRD |= (1 << DDD2); |
PORTD &= ~(1 << PORTD2); |
} else { // Speaker at PC7 |
56,8 → 54,7 |
TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00); |
|
// Timer/Counter 0 Control Register B |
|
// set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
// set clock divider for timer 0 to SYSCLOCK/8 = 20MHz/8 = 2.5MHz |
// i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
// hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
|
83,10 → 80,9 |
/*****************************************************/ |
/* Interrupt Routine of Timer 0 */ |
/*****************************************************/ |
ISR(TIMER0_OVF_vect) |
{ // 9765.625 Hz |
ISR(TIMER0_OVF_vect) { // 9765.625 Hz |
static uint8_t cnt_1ms = 1, cnt = 0; |
uint8_t beeper_On = 0; |
uint8_t beeperOn = 0; |
|
#ifdef USE_NAVICTRL |
if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done |
97,12 → 93,12 |
cnt_1ms ^= 1; |
if (!cnt_1ms) { |
if (runFlightControl == 1) |
DebugOut.Digital[1] |= DEBUG_MAINLOOP_TIMER; |
debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER; |
else |
DebugOut.Digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER; |
runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz) |
} |
millisecondsCount++; // increment millisecond counter |
globalMillisClock++; // increment millisecond counter |
} |
|
// beeper on if duration is not over |
109,35 → 105,32 |
if (beepTime) { |
beepTime--; // decrement BeepTime |
if (beepTime & beepModulation) |
beeper_On = 1; |
beeperOn = 1; |
else |
beeper_On = 0; |
beeperOn = 0; |
} else { // beeper off if duration is over |
beeper_On = 0; |
beepModulation = 0xFFFF; |
beeperOn = 0; |
beepModulation = BEEP_MODULATION_NONE; |
} |
|
// if beeper is on |
if (beeper_On) { |
if (beeperOn) { |
// set speaker port to high. |
if (BoardRelease == 10) |
if (boardRelease == 10) |
PORTD |= (1 << PORTD2); // Speaker at PD2 |
else |
PORTC |= (1 << PORTC7); // Speaker at PC7 |
} else { // beeper is off |
// set speaker port to low |
if (BoardRelease == 10) |
if (boardRelease == 10) |
PORTD &= ~(1 << PORTD2);// Speaker at PD2 |
else |
PORTC &= ~(1 << PORTC7);// Speaker at PC7 |
} |
|
#ifndef USE_NAVICTRL |
#ifdef USE_MK3MAG |
// update compass value if this option is enabled in the settings |
if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) { |
#ifdef USE_MK3MAG |
MK3MAG_Update(); // read out mk3mag pwm |
#endif |
if (staticParams.bitConfig & CFG_COMPASS_ENABLED) { |
MK3MAG_periodicTask(); // read out mk3mag pwm |
} |
#endif |
} |
144,12 → 137,12 |
|
// ----------------------------------------------------------------------- |
uint16_t setDelay(uint16_t t) { |
return (millisecondsCount + t - 1); |
return (globalMillisClock + t - 1); |
} |
|
// ----------------------------------------------------------------------- |
int8_t checkDelay(uint16_t t) { |
return (((t - millisecondsCount) & 0x8000) >> 8); // check sign bit |
return (((t - globalMillisClock) & 0x8000) >> 8); // check sign bit |
} |
|
// ----------------------------------------------------------------------- |
160,13 → 153,18 |
} |
|
// ----------------------------------------------------------------------- |
void delay_ms_Mess(uint16_t w) { |
void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) { |
uint16_t t_stop; |
t_stop = setDelay(w); |
while (!checkDelay(t_stop)) { |
if (analogDataReady) { |
analogDataReady = 0; |
analog_start(); |
analog_update(); |
startAnalogConversionCycle(); |
} |
} |
if (stop) { |
// Wait for new samples to get prepared but do not restart AD conversion after that! |
// Caller MUST to that. |
while (!analogDataReady); |
} |
} |