본문 바로가기

[AVR]/Mega8

[ATmeag88] Timer2 테스트 - 8-bit Timer/Counter2 with PWM

[ATmeag88] Timer2 테스트 - 8-bit Timer/Counter2 with PWM

 

- Single Channel Counter
- Clear Timer on Compare Match (Auto Reload)
- Glitch-free, Phase Correct Pulse Width Modulator (PWM)
- Frequency Generator
- 10-bit Clock Prescaler
- Overflow and Compare Match Interrupt Sources (TOV2, OCF2A and OCF2B)
- Allows Clocking from External 32 kHz Watch Crystal Independent of the I/O Clock

 

 

 

 

Timer2 초기화

#define TIMER2_VALUE      (256-115)

 

#define TIMER2_CLK_DIVCLK                   1
#define TIMER2_CLK_DIV8                     2
#define TIMER2_CLK_DIV32                    3
#define TIMER2_CLK_DIV64                    4
#define TIMER2_CLK_DIV128                   5
#define TIMER2_CLK_DIV256                   6
#define TIMER2_CLK_DIV1024                  7

 

#define TIMER2_DISABLE_PWM                 0

 

 

#define TIMSK        TIMSK2
#define TIMER2_INT_ENABLE     BIT0
#define Timer2Set(Value)     (TCNT2 = Value)
#define TCCR2        TCCR2B

 

//Initialize Timer2

void IntitTime2(void)
{

    // Enable Timer 2 output compare interrupt
    Sbi(TIMSK, TIMER2_INT_ENABLE);

 

    // Start timer 2 with prescaler 128

    Sbi(TCCR2, TIMER2_CLK_DIV128);

   //set 1ms
   Timer2Set(TIMER2_VALUE);
}

 

 

Timer Interrupt Handler

 SIGNAL(SIG_OUTPUT_COMPARE2A)

    gTimeTick++;

    Timer2Set(255-5);

}   

 

 

 

반응형