#include <mega128.h>

#include <delay.h>

#include <LCD.h>


Byte Temp;

Word cnt;

Byte sec, min, hour;


void Init_timer0(){

  TCCR0 |= (1<<WGM01);    //CTC mode

  OCR0 = 100;             //계산/?? -> 200으로 하면 느리게 감

  TIMSK = (1<<OCIE0);     //출력비교 인터럽트 허가

}


interrupt[TIM0_COMP] void tim0_out_comp(){

  cnt++;     //50us * 200 = 1sec

  if(cnt == 20000){

    cnt = 0;

    sec++;

    if(sec>=60){

      min++;

      sec = 0;

    } 

    if(min>=60){

      hour++;

      min = 0;

    }         

    if(hour>=24)

      hour = 0;

  }

}


void main(){

  Byte str[] = "Current Time";

  Byte str1[] = "AM 12:00:00";

  Byte AM[] = "AM";

  Byte PM[] = "PM";

  Temp = 0;

  cnt = 0;

  sec = 0; min = 0, hour = 12;

  //timer 초기화

  Init_timer0();

  sei();

  TCCR0 |= 1<<CS01;       //LCD 초기화

  PortInit();             //lcd 출력포트 실행

  LCD_Init();

  LCD_pos(0,0);           //커서 이동

  LCD_STR(str);

  LCD_pos(1,0);  

  LCD_STR(str1);

  while(1){

    if(hour > 12){

      LCD_pos(1,0);

      LCD_STR(PM);

      LCD_pos(1,3);

      LCD_CHAR((hour-12)/10 + '0');    //0을 더하는 이유는 ASCII값으로 변환을 위해서

      LCD_CHAR((hour-12)%10 + '0');

    }else{

      LCD_pos(1,0);

      LCD_STR(AM);

      LCD_pos(1,3);

      LCD_CHAR((hour/10)+'0');

      LCD_CHAR((hour%10)+'0'); 

    }

    LCD_pos(1,6);

    LCD_CHAR((min/10)+'0');

    LCD_CHAR((min%10)+'0');

    LCD_pos(1,9);         

    LCD_CHAR((sec/10)+'0');

    LCD_CHAR((sec%10)+'0');                

    

  }  

}

블로그 이미지

뭐해볼까

,