Timer/Counter – Clear Timer on Compare Match (CTC)

TCCR0|=(1<<WGM01);  //CTC mode
  #include<avr/io.h>
int main(void)
  {
    DDRC=0xff;
    TCCR0|=(1<<WGM01);					  //CTC mode
    TCCR0|=(1<<CS02)|(1<<CS00);  			//F_CPU/1024 
    TCNT0=0;								//clear Timer
    while(1)
      {
         OCR0=97;							//100ms Timer
         PORTC^=0xff;
      }
   return 0;	
  }
  #include<avr/interrupt.h>
  sei();	//Global Interrupt Enable
  TIMSK|=(1<<OCIE0); //Timer/Counter0 Output Compare Match Interrupt Enable
  ISR(TIMER0_COMP_vect)
  {
   /******** do something at that interrupt *****************/
  }
/***********************************************************************************************
************************************************************************************************
*********************************  Digital Clock ************************************************
************************************************************************************************
***********************************************************************************************/
#include<avr/io.h>
#include<stdio.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#include"lcd.h"
uint8_t millisecond=0,second=0,minute=0,hour=0;
char str[20];
int main(void)
{
LCD_INIT();
LCD_write_string(1,1,"**Digital Clock*");
TCCR2|=(1<<WGM21)|(1<<CS22)|(1<<CS21);   //normal CTC mode with 256 
TCNT2=0;		                         //clear Timer
TIMSK|=(1<<OCIE2);		        //Timer/Counter0 Output Compare Match Interrupt Enable
sei();									 //Globel Interrupt Enable
OCR2=249;								 //F_CPU=8MHz,P=256,Delay=8ms
while(1)
{
	if(hour<=12){   
	        sprintf(str,"TIME %d:%d :%d am",hour,minute,second);
			LCD_write_string(1,2,str);
			}
	else{
			sprintf(str,"TIME %d:%d :%d pm",hour,minute,second);
			LCD_write_string(1,2,str);
			}
}
return 0;
}
ISR(TIMER2_COMP_vect)
{
millisecond++;
if(millisecond==125){
	second++;
	millisecond=0;
	if(second==60){
		minute++;
		second=0;
		}
		if(minute==60){
			hour++;
			minute=0;
			if(hour==24) hour=0;
			}
	}

}

  #include<avr/io.h>
  #include<avr/interrupt.h>
  uint8_t sample;
int main(void)
  {
    DDRB=0xFF;
    DDRD|=(1<<DDD5);							//OC1A output PIN
    TCCR1B=(1<<WGM12)|(1<<CS12);				//CTC with prescaler=256
    TCCR1A=(1<<COM1A0);						    //Toggle OC1A/OC1B on compare match
    TCNT1=0;									//Clear Timer
    OCR1A=31249;								//F_CPU=8MHz,Prescaler=256,Delay=   1s
    sei();										//Globel Interrupt Enable
    TIMSK=(1<<OCIE1A); // Timer/Counter1, Output Compare A Match Interrupt Enable
   while(1);
return 0;
}
ISR(TIMER1_COMPA_vect)
{
    sample^=1;
    sample ? (PORTB=0xFF):(PORTB=0x00);
}
  #include<avr/io.h>
  uint8_t sample;
int main(void)
  {
   DDRB=0xFF;
   DDRD|=(1<<DDD5);							//OC1A output PIN
   TCCR1A=(1<<COM1A0);						//Toggle OC1A/OC1B on compare match
   TCCR1B=(1<<WGM12)|(1<<CS12);				//CTC with prescaler=256
   TCNT1=0;									//Clear Timer
   OCR1A=31249;								//F_CPU=8MHz,Prescaler=256,Delay=   1s
  while(1)
   {
     if(TIFR&(1<<OCF1A)){
         PORTB^=0xFF;
         TIFR|=(1<<OCF1A);
         }
    }
 return 0;
 }