ATMEGA128

ATMEGA128, CodeVisonAVR, TIMER, timer_overflow_intr_1ms

CoyoteUgly 2018. 8. 30. 00:40

ATMEGA128, CodeVisonAVR, TIMER, timer_overflow_intr_1ms


0. 참고 자료


1. 모듈

2. HW 구성

3. Ports > Bit0 out

LED Port



4. Timers > Timer0

  • Clock Source : System Clock
  • Clock Value : 16000 kHz
  • Mode : Normal Mode=0xFF
  • Overflow Interrupt : check
  • Timer Value : 6
    • Timer Count가 0~ 249에서 overflow를 발생하기 위함


5. SW 구성




#include <mega128.h>

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x06;
// Place your code here
    // 16MHz / 64 = 250000Hz = 0.004ms = 4us
    // 0.004ms * 250 = 1ms
    PORTA ^= 0x01;
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0
PORTA=0x00;
DDRA=0x01;

//TCCR0=0x01;
TCCR0 = // 64 prescaler
(0 << FOC0)  | (0 << WGM00) | (0 << COM01) | (0 << COM00) |
(0 << WGM01) | (1 << CS02)  | (0 << CS01)  | (0 << CS00);

TCNT0=0x06;
OCR0=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

ETIMSK=0x00;

// Global enable interrupts
#asm("sei")

while (1)
      {
      // Place your code here

      }
}


6. 소스


7. 결과

1ms 마다 LED Port가 동작하는 것을 확인할 수 있습니다.