Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
684 | killagreg | 1 | #include "main.h" |
2 | |||
3 | |||
4 | int16_t ServoValue = 0; |
||
5 | |||
6 | |||
7 | /*****************************************************/ |
||
8 | /* Initialize Timer 2 */ |
||
9 | /*****************************************************/ |
||
10 | // The timer 2 is used to generate the PWM at PD7 (J7) |
||
11 | // to control a camera servo for nick compensation |
||
12 | void TIMER2_Init(void) |
||
13 | { |
||
14 | uint8_t sreg = SREG; |
||
15 | |||
16 | // disable all interrupts before reconfiguration |
||
17 | cli(); |
||
18 | |||
19 | // set PD7 as output for the PWM |
||
20 | DDRD |=(1<<DDD7); |
||
21 | PORTB |= (1<<PORTD7); |
||
22 | |||
23 | |||
24 | // Timer/Counter 2 Control Register A |
||
25 | |||
26 | // Waveform Generation Mode is Fast PWM (Bits WGM22 = 0, WGM21 = 1, WGM20 = 1) |
||
27 | // PD7: Clear OC2B on Compare Match, set OC2B at BOTTOM, noninverting PWM (Bits COM2A1 = 1, COM2A0 = 0) |
||
28 | // PD6: Normal port operation, OC2B disconnected, (Bits COM2B1 = 0, COM2B0 = 0) |
||
29 | TCCR2A &= ~((1<<COM2B1)|(1<<COM2B0)|(1<<COM2A0)); |
||
30 | TCCR2A |= (1<<COM2A1)|(1<<WGM21)|(1<<WGM20); |
||
31 | |||
32 | // Timer/Counter 2 Control Register B |
||
33 | |||
34 | // set clock devider for timer 2 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz |
||
35 | // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
||
36 | // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz |
||
37 | |||
38 | // devider 8 (Bits CS022 = 0, CS21 = 1, CS20 = 0) |
||
39 | TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<WGM22)); |
||
40 | TCCR2B = (TCCR2B & 0xF8)|(0<<CS22)|(1<<CS21)|(0<<CS20); |
||
41 | |||
42 | // initialize the Output Compare Register A used for PWM generation on port PD7 |
||
43 | OCR2A = 10; |
||
44 | |||
45 | // init Timer/Counter 2 Register |
||
46 | TCNT2 = 0; |
||
47 | |||
48 | // Timer/Counter 2 Interrupt Mask Register |
||
49 | // enable timer output compare match A Interrupt only |
||
50 | TIMSK2 &= ~((1<<OCIE2B)|(1<<TOIE2)); |
||
51 | TIMSK2 |= (1<<OCIE2A); |
||
52 | |||
53 | SREG = sreg; |
||
54 | } |
||
55 | |||
56 | |||
57 | /*****************************************************/ |
||
58 | /* Control Servo Position */ |
||
59 | /*****************************************************/ |
||
60 | ISR(TIMER2_COMPA_vect) // 9,765 kHz |
||
61 | { |
||
62 | static uint8_t timer = 10; |
||
63 | |||
64 | if(!timer--) |
||
65 | { |
||
66 | // enable PWM on PD7 in non inverting mode |
||
67 | TCCR2A = (TCCR2A & 0x3F)|(1<<COM2A1)|(0<<COM2A0); |
||
68 | |||
69 | ServoValue = Parameter_ServoNickControl; |
||
70 | // inverting movment of servo |
||
71 | if(EE_Parameter.ServoNickCompInvert & 0x01) |
||
72 | { |
||
73 | ServoValue += ((int32_t) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512; |
||
74 | } |
||
75 | else // non inverting movment of servo |
||
76 | { |
||
77 | ServoValue -= ((int32_t) EE_Parameter.ServoNickComp * (IntegralNick / 128)) / 512; |
||
78 | } |
||
79 | |||
80 | // limit servo value to its parameter range definition |
||
81 | if(ServoValue < EE_Parameter.ServoNickMin) |
||
82 | { |
||
83 | ServoValue = EE_Parameter.ServoNickMin; |
||
84 | } |
||
85 | else if(ServoValue > EE_Parameter.ServoNickMax) |
||
86 | { |
||
87 | ServoValue = EE_Parameter.ServoNickMax; |
||
88 | } |
||
89 | |||
90 | // update PWM |
||
91 | OCR2A = ServoValue; |
||
92 | timer = EE_Parameter.ServoNickRefresh; |
||
93 | } |
||
94 | else |
||
95 | { |
||
96 | // disable PWM at PD7 |
||
97 | TCCR2A &= ~((1<<COM2A1)|(1<<COM2A0)); |
||
98 | // set PD7 to GND |
||
99 | PORTD &= ~(1<<PORTD7); |
||
100 | } |
||
101 | } |