Subversion Repositories FlightCtrl

Rev

Rev 935 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
886 killagreg 1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include "fc.h"
4
#include "eeprom.h"
936 killagreg 5
#include "uart.h"
886 killagreg 6
 
936 killagreg 7
volatile uint16_t ServoValue = 0;
886 killagreg 8
 
9
 
936 killagreg 10
 
886 killagreg 11
/*****************************************************/
12
/*              Initialize Timer 2                   */
13
/*****************************************************/
14
// The timer 2 is used to generate the PWM at PD7 (J7)
911 killagreg 15
// to control a camera servo for nick compensation.
886 killagreg 16
void TIMER2_Init(void)
17
{
18
        uint8_t sreg = SREG;
19
 
20
        // disable all interrupts before reconfiguration
21
        cli();
22
 
911 killagreg 23
        // set PD7 as output of the PWM for nick servo
886 killagreg 24
        DDRD  |=(1<<DDD7);
936 killagreg 25
        PORTD &= ~(1<<PORTD7);  // set PD7 to low
886 killagreg 26
 
27
 
28
        // Timer/Counter 2 Control Register A
29
 
30
        // Waveform Generation Mode is Fast PWM (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 1)
936 killagreg 31
    // PD7: Normal port operation, OC2A disconnected, (Bits: COM2A1 = 0, COM2A0 = 0)
886 killagreg 32
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
936 killagreg 33
        TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)|(1<<COM2B1)|(1<<COM2B0));
34
    TCCR2A |= (1<<WGM21)|(1<<WGM20);
886 killagreg 35
 
36
    // Timer/Counter 2 Control Register B
37
 
936 killagreg 38
        // Set clock divider for timer 2 to SYSKLOCK/64 = 20MHz / 64 = 312.5 kHz
39
        // The timer increments from 0x00 to 0xFF with an update rate of 312.5 kHz or 3.2 us
40
        // hence the timer overflow interrupt frequency is 312.5 kHz / 256 = 1220.7 Hz or 0.8192 ms
886 killagreg 41
 
936 killagreg 42
    // divider 64 (Bits: CS022 = 1, CS21 = 0, CS20 = 0)
43
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS21)|(1<<CS20)|(1<<WGM22));
44
    TCCR2B |= (1<<CS22);
886 killagreg 45
 
46
        // Initialize the Timer/Counter 2 Register
47
    TCNT2 = 0;
48
 
936 killagreg 49
        // Initialize the Output Compare Register A used for PWM generation on port PD7.
50
        OCR2A = 10;
51
 
886 killagreg 52
        // Timer/Counter 2 Interrupt Mask Register
53
        // Enable timer output compare match A Interrupt only
54
        TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2));
55
        TIMSK2 |= (1<<OCIE2A);
56
 
57
    SREG = sreg;
58
}
59
 
60
 
61
/*****************************************************/
62
/*              Control Servo Position               */
63
/*****************************************************/
936 killagreg 64
 
65
ISR(TIMER2_COMPA_vect)  // every  256 * 3.2 us = 0.819 us ( on compare match of TCNT2 and OC2A)
886 killagreg 66
{
936 killagreg 67
        static  uint8_t PostPulse = 0x80;       // value for last pwm cycle in non inverting mode (clear pin on compare match)
68
        static uint16_t FilterServo = 100;      // initial value, after some iterations it becomes the average value of 2 * FCParam.ServoNickControl
69
        static uint16_t ServoState = 40;        // cycle down counter for this ISR
886 killagreg 70
 
936 killagreg 71
        #define MULTIPLIER 4
886 killagreg 72
 
73
 
74
 
936 killagreg 75
        switch(ServoState)
76
        {
77
                case 4:
78
                        // recalculate new ServoValue
79
                        ServoValue = 0x0030; // Offset (part 1)
80
                        FilterServo = (3 * FilterServo + (uint16_t)FCParam.ServoNickControl * 2) / 4; // lowpass static offset
81
                        ServoValue += FilterServo; // add filtered static offset
82
 
83
                        if(ParamSet.ServoNickCompInvert & 0x01)
84
                        {       // inverting movement of servo
85
                                ServoValue += ((int32_t) ((int32_t)ParamSet.ServoNickComp * IntegralNick) / 128L )/ (512L/MULTIPLIER);
86
                        }
87
                        else
88
                        {       // non inverting movement of servo
89
                                ServoValue -= ((int32_t) ((int32_t)ParamSet.ServoNickComp * IntegralNick) / 128L) / (512L/MULTIPLIER);
90
                        }
91
 
92
                        // limit servo value to its parameter range definition
93
                        if(ServoValue < ((uint16_t)ParamSet.ServoNickMin * 3) )
94
                        {
95
                                ServoValue = (uint16_t)ParamSet.ServoNickMin * 3;
96
                        }
97
                        else
98
                        if(ServoValue > ((uint16_t)ParamSet.ServoNickMax * 3) )
99
                        {
100
                                ServoValue = (uint16_t)ParamSet.ServoNickMax * 3;
101
                        }
102
 
103
                        DebugOut.Analog[20] = ServoValue;
104
                        // determine prepulse width (remaining part of ServoValue/Timer Cycle)
105
                        if ((ServoValue % 255) < 45)
106
                        {       // if prepulse width is to short the execution time of thios isr is longer than the next compare match
107
                                // so balance with postpulse width
108
                                ServoValue += 77;
109
                                PostPulse = 0x60 - 77;
110
                        }
111
                        else
112
                        {
113
                                PostPulse = 0x60;
114
                        }
115
                        // set output compare register to 255 - prepulse width
116
                        OCR2A = 255 - (ServoValue % 256);
117
                        // connect OC2A in inverting mode (Clear pin on overflow, Set pin on compare match)
118
                        TCCR2A=(1<<COM2A1)|(1<<COM2A0)|(1<<WGM21)|(1<<WGM20);
119
 
120
                        break;
121
 
122
                case 3:
123
                case 2:
124
                case 1:
125
 
126
                        if(ServoValue > 255)        // is larger than a full timer 2 cycle
127
                        {
128
                                PORTD |= (1<<PORTD7);                   // set PD7 to high
129
                                TCCR2A = (1<<WGM21)|(1<<WGM20); // disconnect OC2A
130
                                ServoValue -= 255;              // substract full timer cycle
131
                        }
132
                        else // the post pule must be generated
133
                        {
134
                                TCCR2A=(1<<COM2A1)|(0<<COM2A0)|(1<<WGM21)|(1<<WGM20); // connect OC2A in non inverting mode
135
                                OCR2A = PostPulse; // Offset Part2
136
                                ServoState = 1;    // jump to ServoState 0 with next ISR call
137
                        }
138
                break;
139
 
140
                case 0:
141
                        ServoState  = (uint16_t) ParamSet.ServoNickRefresh * MULTIPLIER;        // reload ServoState
142
                        PORTD &= ~(1<<PORTD7);                                                                                          // set PD7 to low
143
                        TCCR2A = (1<<WGM21)|(1<<WGM20);                                                         // disconnect OC2A
144
                        break;
145
 
146
                default:
147
                        // do nothing
148
                        break;
149
        }
150
        ServoState--;
886 killagreg 151
}
936 killagreg 152