Subversion Repositories FlightCtrl

Rev

Rev 766 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 766 Rev 783
Line 18... Line 18...
18
        // disable all interrupts before reconfiguration
18
        // disable all interrupts before reconfiguration
19
        cli();
19
        cli();
Line 20... Line 20...
20
 
20
 
21
        // set PD7 as output of the PWM for pitch servo
21
        // set PD7 as output of the PWM for pitch servo
22
        DDRD  |=(1<<DDD7);
22
        DDRD  |=(1<<DDD7);
Line 23... Line 23...
23
        PORTB |= (1<<PORTD7);
23
        PORTD |= (1<<PORTD7);
Line 24... Line 24...
24
 
24
 
25
 
25
 
26
        // Timer/Counter 2 Control Register A
26
        // Timer/Counter 2 Control Register A
27
 
27
 
28
        // Waveform Generation Mode is Fast PWM (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 1)
28
        // Waveform Generation Mode is Fast PWM (Bits: WGM22 = 0, WGM21 = 1, WGM20 = 1)
Line 29... Line 29...
29
    // PD7: Clear OC2B on Compare Match, set OC2B at BOTTOM, noninverting PWM (Bits: COM2A1 = 1, COM2A0 = 0)
29
    // PD7: Clear OC2B on Compare Match, set OC2B at BOTTOM, non inverting PWM (Bits: COM2A1 = 1, COM2A0 = 0)
Line 30... Line 30...
30
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
30
    // PD6: Normal port operation, OC2B disconnected, (Bits: COM2B1 = 0, COM2B0 = 0)
31
        TCCR2A &= ~((1<<COM2B1)|(1<<COM2B0)|(1<<COM2A0));
31
        TCCR2A &= ~((1<<COM2B1)|(1<<COM2B0)|(1<<COM2A0));
32
    TCCR2A |= (1<<COM2A1)|(1<<WGM21)|(1<<WGM20);
32
    TCCR2A |= (1<<COM2A1)|(1<<WGM21)|(1<<WGM20);
33
 
33
 
34
    // Timer/Counter 2 Control Register B
34
    // Timer/Counter 2 Control Register B
35
 
35
 
36
        // Set clock divider for timer 2 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
36
        // Set clock divider for timer 2 to SYSKLOCK/256 = 20MHz / 256 = 78.128 kHz
Line 37... Line 37...
37
        // The timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz,
37
        // The timer increments from 0x00 to 0xFF with an update rate of 78.128 kHz or 12.8 us
38
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz or 0.1024 ms
38
        // hence the timer overflow interrupt frequency is 78.128 kHz / 256 = 305.176 Hz or 3.276 ms
Line 39... Line 39...
39
 
39
 
40
    // divider 8 (Bits: CS022 = 0, CS21 = 1, CS20 = 0)
40
    // divider 256 (Bits: CS022 = 1, CS21 = 1, CS20 = 0)
Line 41... Line 41...
41
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<WGM22));
41
        TCCR2B &= ~((1<<FOC2A)|(1<<FOC2B)|(1<<CS20)|(1<<WGM22));
Line 57... Line 57...
57
 
57
 
58
 
58
 
59
/*****************************************************/
59
/*****************************************************/
60
/*              Control Servo Position               */
60
/*              Control Servo Position               */
61
/*****************************************************/
61
/*****************************************************/
62
ISR(TIMER2_COMPA_vect)  // 9.765 kHz
62
ISR(TIMER2_COMPA_vect)  // every  OCR2A * 12.8s (compare match)
Line 63... Line 63...
63
{
63
{
64
  static uint8_t timer = 10;
64
  static uint8_t timer = 10;
65
 
65
 
66
  if(!timer--)
66
  if(!timer--)
-
 
67
    {
Line 67... Line 68...
67
    {
68
                 // enable PWM on PD7 in non inverting mode
68
                 // enable PWM on PD7 in non inverting mode
69
                 TCCR2A &= ~(0<<COM2A0);
69
                 TCCR2A = (TCCR2A & 0x3F)|(1<<COM2A1)|(0<<COM2A0);
70
                 TCCR2A |= (1<<COM2A1);
70
 
71