Subversion Repositories FlightCtrl

Rev

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

#include <avr/io.h>
#include <avr/interrupt.h>
#include "fc.h"
#include "eeprom.h"

volatile int16_t ServoValue = 0;


/*****************************************************/
/*              Initialize Timer 2                   */
/*****************************************************/
// The timer 2 is used to generate the PWM at PD7 (J7)
// to control a camera servo for nick compensation.
void TIMER2_Init(void)
{
        uint8_t sreg = SREG;

        // disable all interrupts before reconfiguration
        cli();

        // set PD7 as output for the PWM
        DDRD  |=(1<<DDD7);
        PORTB |= (1<<PORTD7);


        // Timer/Counter 2 Control Register A

        // Waveform Generation Mode is Fast PWM (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 1)
    // PD7: Clear OC2B on Compare Match, set OC2B at BOTTOM, noninverting PWM (Bits: COM2A1 = 1, COM2A0 = 0)
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
        TCCR2A &= ~((1<<COM2B1)|(1<<COM2B0)|(1<<COM2A0));
    TCCR2A |= (1<<COM2A1)|(1<<WGM21)|(1<<WGM20);

    // Timer/Counter 2 Control Register B

        // Set clock divider for timer 2 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
        // 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 or 0.1024 ms

    // divider 8 (Bits: CS022 = 0, CS21 = 1, CS20 = 0)
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<WGM22));
    TCCR2B = (TCCR2B & 0xF8)|(0<<CS22)|(1<<CS21)|(0<<CS20);

        // Initialize the Output Compare Register A used for PWM generation on port PD7.
        OCR2A = 10;

        // Initialize the Timer/Counter 2 Register
    TCNT2 = 0;

        // Timer/Counter 2 Interrupt Mask Register
        // Enable timer output compare match A Interrupt only
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
        TIMSK2 |= (1<<OCIE2A);

    SREG = sreg;
}


/*****************************************************/
/*              Control Servo Position               */
/*****************************************************/
ISR(TIMER2_COMPA_vect)  // 9.765 kHz
{
  static uint8_t timer = 10;

  if(!timer--)
    {
                 // enable PWM on PD7 in non inverting mode
                 TCCR2A = (TCCR2A & 0x3F)|(1<<COM2A1)|(0<<COM2A0);

                 ServoValue =  Parameter_ServoPitchControl;
                 // inverting movment of servo
                 if(ParamSet.ServoPitchCompInvert & 0x01)
                 {
                         ServoValue += ((int32_t) ParamSet.ServoPitchComp * (IntegralPitch / 128)) / 512;
                 }
                 else // non inverting movement of servo
                 {
                         ServoValue -= ((int32_t) ParamSet.ServoPitchComp * (IntegralPitch / 128)) / 512;
                 }

                 // limit servo value to its parameter range definition
                 if(ServoValue < ParamSet.ServoPitchMin)
                 {
                         ServoValue = ParamSet.ServoPitchMin;
                 }
                 else if(ServoValue > ParamSet.ServoPitchMax)
                 {
                         ServoValue = ParamSet.ServoPitchMax;
                 }

                 // update PWM
                 OCR2A = ServoValue;
                 timer = ParamSet.ServoPitchRefresh;
    }
    else
    {
         // disable PWM at PD7
     TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0));
     // set PD7 to low
     PORTD &= ~(1<<PORTD7);
    }
}