Subversion Repositories FlightCtrl

Rev

Rev 783 | Go to most recent revision | Blame | Compare with Previous | 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 pitch compensation.
void TIMER2_Init(void)
{
        uint8_t sreg = SREG;

        // disable all interrupts before reconfiguration
        cli();

        // set PD7 as output of the PWM for pitch servo
        DDRD  |=(1<<DDD7);
        PORTD |= (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, non inverting 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/256 = 20MHz / 256 = 78.128 kHz
        // The timer increments from 0x00 to 0xFF with an update rate of 78.128 kHz or 12.8 us
        // hence the timer overflow interrupt frequency is 78.128 kHz / 256 = 305.176 Hz or 3.276 ms

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

        // Initialize the Output Compare Register A used for PWM generation on port PD7.
        OCR2A = 10; //10 * 12.8us = 1.28 ms high time

        // 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)  // every  OCR2A * 12.8 us (compare match)
{
  static uint8_t timer = 10;

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

                 ServoValue =  FCParam.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);
    }
}