공부,일/atmega328P

timer1 , 1초 깜빡이 코드

fromnothing1 2021. 10. 5. 17:09
void setup() {
  // put your setup code here, to run once:

  DDRD = 1<<7;
  init_timer2();
}

void loop() {
  // put your main code here, to run repeatedly:

}

void init_timer2()
{
  cli();//글로벌 인터럽트 정지
  TCNT1 = 49911; // 한번에 들어감
  TCCR1A = 0x00; // 노멀모드 초기화 
  TCCR1B = 0x05; //분주비 1024
  TIMSK1 = 0x01; // overflow 인터럽트 발생가능
  sei(); // 인터럽트 사용
}

int flag = 0;


ISR(__vector_13)
{
  unsigned int temp = 49911;
  unsigned char *p = (unsigned char *)&temp;
  if(0 == flag)
  {
    PORTD  = 1<<7;
    flag = 1;
  }
  else
  {
    PORTD  = 0;
    flag = 0;
  }
  TCNT1 = 49911;
}