본문 바로가기

Embeded/ATmega128

2010년 10월 21일 (타이머 카운터)

T/C 총 4가지.
T/C 0, 2 = 8bit
T/C 1, 3 = 16bit

T/C0 을 사용할 것임.

XTAL = 16Mhz 사용 (1초당 16만번의 클럭발생) 
1/1000로 클럭을 나눔 = 1000번을 수행해야 1초

// FND(60초) LED(60초마다 on) Count 오버플로인터럽트 사용
#include <avr/stdio.h>
#include <avr/signal.h>
#include <avr/interrupt.h>

#define CPU_CLOCK  16000000
#define TICKS_PER_SEC  1000
#define PRESCALER  64

volatile unsigned int g_elapsed_time;  // 시간변수
void sleep(unsigned int elapsed_time);  // sleep함수
SIGNAL(SIG_OVERFLOW0);      // timer0의 오버플로우 시그널함수

int main()
{
  int min=0;
  int sec=0;
  int total=0;
  DDRC = 0xFF;  // PORTC 출력모드 설정
  DDRF = 0xFF;  // PORTF 출력모드 설정
  PORTF = 0xFF;  // PORTF 초기화
  PORTC = 0x00;  // PORTC 초기화
  TCCR0 = 0x04;  // TCCR0 노말모드 설정
  TCNT0 = 0x06;  // TCNT0 초기화
  TIMSK = 0x01;  // 오버플로 인터럽트 설정
  //TIFR |= 0x01;  // TOV0 오버플로 클리어    
  SREG = (1<<7)|SREG;  // I비트 1설정
  sei();    // 인터럽트 활성화 함수

  while(1)
  {
    asm("NOP");

    if(9>=total)
    {
      sleep(1000);
      total++;
      sec++;
    }
    else if(0x50>=sec)
    {
      total=0;
      sec=0;
      min=min+0x10;
      sec=min;
    }
    else if(0>=PORTF)
    {
      PORTF=0xFF;
    }
    else
    {
      total=0;
      sec=0;
      min=0;
      PORTF=PORTF<<1;
    }
    PORTC=sec;
  }
  return 1;
}

void sleep(unsigned int elapsed_time)
{
  g_elapsed_time=0;
  while(g_elapsed_time<elapsed_time)
  {
  }
}

SIGNAL(SIG_OVERFLOW0)
{
  TCNT0 = 0x06;
  g_elapsed_time++;
}



_M#]