Rev 886 |
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 of the PWM for nick 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.ServoNickControl;
// inverting movment of servo
if(ParamSet.ServoNickCompInvert & 0x01)
{
ServoValue += ((int32_t) ParamSet.ServoNickComp * (IntegralNick / 128)) / 512;
}
else // non inverting movement of servo
{
ServoValue -= ((int32_t) ParamSet.ServoNickComp * (IntegralNick / 128)) / 512;
}
// limit servo value to its parameter range definition
if(ServoValue < ParamSet.ServoNickMin)
{
ServoValue = ParamSet.ServoNickMin;
}
else if(ServoValue > ParamSet.ServoNickMax)
{
ServoValue = ParamSet.ServoNickMax;
}
// update PWM
OCR2A = ServoValue;
timer = ParamSet.ServoNickRefresh;
}
else
{
// disable PWM at PD7
TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0));
// set PD7 to low
PORTD &= ~(1<<PORTD7);
}
}