Subversion Repositories FlightCtrl

Rev

Rev 749 | Blame | Last modification | View Log | RSS feed

#include "main.h"

volatile unsigned int CountMilliseconds = 0;
volatile static unsigned int tim_main;
volatile unsigned char UpdateMotor = 0;
volatile unsigned int beeptime = 0;
unsigned int BeepMuster = 0xffff;
int ServoValue = 0;

enum {
  STOP             = 0,
  CK               = 1,
  CK8              = 2,
  CK64             = 3,
  CK256            = 4,
  CK1024           = 5,
  T0_FALLING_EDGE  = 6,
  T0_RISING_EDGE   = 7
};


SIGNAL (SIG_OVERFLOW0)    // 9,8kHz
{
    static unsigned char cnt_1ms = 1,cnt = 0;
        static uint8_t led_dim, led_duty;
    unsigned char pieper_ein = 0;

   if(!cnt--)           // ca. 1 kHz
    {
     cnt = 10;
     cnt_1ms++;
     cnt_1ms %= 2;
     if(!cnt_1ms) UpdateMotor = 1;
     CountMilliseconds++;
         
         // LED-Dimmung Duty-Cycle
         if (!led_duty--)
         {
                led_duty = 4;
                led_dim = Poti2 >> 2;
                PORTD &= ~(1<<PD4);     // J5 auf Low
         }       
     }
       
        if (led_dim) led_dim--;
        else PORTD |= (1<<PD4);

     if(beeptime > 1)
        {
        beeptime--;      
        if(beeptime & BeepMuster)
         {
          pieper_ein = 1;
         }
         else pieper_ein = 0;
        }
     else
      {
       pieper_ein = 0;
       BeepMuster = 0xffff;
      }


     if(pieper_ein)
        {
          if(PlatinenVersion == 10) PORTD |= (1<<2); // Speaker an PORTD.2
          else                      PORTC |= (1<<7); // Speaker an PORTC.7
        }
     else  
        {
         if(PlatinenVersion == 10) PORTD &= ~(1<<2);
         else                      PORTC &= ~(1<<7);
        }
 
        if(EE_Parameter.GlobalConfig & CFG_KOMPASS_AKTIV) timer0_MM3();         // Kompass auslesen
}


void Timer_Init(void)
{
    TCCR0B = CK8;
    TCCR0A = (1<<COM0A1)|(1<<COM0B1)|3;//fast PWM
    OCR0A =  0;
    OCR0B = 120;

    TCCR2A=(1<<COM2A1)|(1<<COM2A0)|3;
    TCCR2B=(0<<CS20)|(1<<CS21)|(1<<CS22);
   
        //    TIMSK2 |= _BV(TOIE2);
        TIMSK2 |= _BV(OCIE2A);

    TIMSK0 |= _BV(TOIE0);
    OCR2A = 10;
    TCNT2 = 0;    
}

// -----------------------------------------------------------------------

unsigned int SetDelay (unsigned int t)
{
   uint8_t tmp_sreg;
   uint16_t CountMilliseconds_local;
   
   tmp_sreg = SREG;
   cli();
   CountMilliseconds_local = CountMilliseconds;
   SREG = tmp_sreg;
   return(CountMilliseconds_local + t + 1);
}

// -----------------------------------------------------------------------
char CheckDelay(unsigned int t)
{
   uint8_t tmp_sreg;
   uint16_t CountMilliseconds_local;
   
   tmp_sreg = SREG;
   cli();
   CountMilliseconds_local = CountMilliseconds;
   SREG = tmp_sreg;
   return (((t - CountMilliseconds_local) & 0x8000) >> 9);
}

// -----------------------------------------------------------------------
void Delay_ms(unsigned int w)
{
 unsigned int akt;
 akt = SetDelay(w);
 while (!CheckDelay(akt));
}

void Delay_ms_Mess(unsigned int w)
{
 unsigned int akt;
 akt = SetDelay(w);
 while (!CheckDelay(akt)) ANALOG_ON;
}

// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++               
//  Servo ansteuern
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++               
SIGNAL(SIG_OUTPUT_COMPARE2A)
{
  static unsigned char timer = 10;
 
  if(!timer--)  
    {
                // enable PWM on PD7 in non inverting mode
                TCCR2A= (1<<COM2A1)|(0<<COM2A0)|3;
                 
                ServoValue =  Parameter_ServoNickControl;
                if(EE_Parameter.ServoNickCompInvert & 0x01) ServoValue += ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
                else ServoValue -= ((long) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512;
     
                if(ServoValue < EE_Parameter.ServoNickMin) ServoValue = EE_Parameter.ServoNickMin;
                else if(ServoValue > EE_Parameter.ServoNickMax) ServoValue = EE_Parameter.ServoNickMax;

                OCR2A = ServoValue;
                timer = EE_Parameter.ServoNickRefresh;
    }
    else
    {
                // disable PWM at PD7
                TCCR2A = 3;
                // set PD7 to low
                PORTD &= ~(1<<PD7);
    }
}