I was looking at Waveform Generation mode bit description in Atmega328 Datasheet. I have two doubts. 1. What is update of OCRx at (marked 1 in the attached image). OCRx is written by the programmer. Is it mean that even after writing, it gets updated only at TOP or Bottom in case of PWM modes? 2. In CTC mode, TCNT gets cleared on Compare match. MAX is always 0xFF. Then how TOV flag can generate at MAX? When TCNT=OCRA, TCNT gets cleared right? I have marked this as 2 in the figure.
Additional information for supporting the comment:
void timer0LEDBlinkTestCTCMode (void)
{
DDRB|=(1<<0);// // Set LED port as Output to toggle the LED.
DDRB|=(1<<1);// // Set LED port as Output to toggle the LED.
OCR0A = 0xff;
TIMSK0 |= (1<<OCIE0A)|(1 << TOIE0); // Output Compare Interrupt Enable for TimerCounter0 Compare Match A
TCCR0A |= 1<<(WGM01); // Mode = CTC: WGM01=1, WGM00=0 in TCCR0A and WGM02=0 in TCCR0B
sei(); // Enable global interrupt
// Timer is activated as soon as the clock source is selected
TCCR0B = (1<<CS02)|(1<<CS00); // Timer Prescaler Clock/1024, TCCR0B: CS00 - 1, CS01 - 0, CS02 - 1
}
//This is the ISR for Compare Mode A
ISR(TIMER0_COMPA_vect)
{
sei(); // Enable the interrupt because both interrupts may occur simultaneously.
PORTB ^= 1<<0;
}
ISR (TIMER0_OVF_vect) // timer0 overflow interrupt
{
sei();
PORTB^=1<<1; // Toggle the LED
}