0

I was struggling with timer interrupts in my project. I couldn't make it work properly. So I decided writing a simple code and I saw a very interesting case.

ISR(TIMER1_COMPB_vect)
{
 PORTB ^= (1 << PORTB5);
}
int main(void)
{
 cli(); // disable global interrupts
 TCCR1A = 0; // set entire TCCR1A register to 0
 TCCR1B = 0; // same for TCCR1B
 OCR1A = 10000;
 OCR1B = 100;
 TCCR1B |= (1 << WGM12);
 TCCR1B |= (1 << CS10);
 TCCR1B |= (1 << CS12);
 TIMSK1 |= (1 << OCIE1A);
 TIMSK1 |= (1 << OCIE1B);
 DDRB= 0xFF;
 #define F_CPU 16000000
 sei();
 while (1)
 {
 }
}

Here is my code. When I change OCR1B value, nothing happens, but if I change OCR1A value then blinking gets faster. Is there a logical explanation for this?

asked May 4, 2015 at 18:08
1
  • @Gerben No. OCR1A is 2 byte. OCR1AH and OCR1AL are single byte. Commented May 4, 2015 at 18:33

1 Answer 1

2

By setting WGM12, you set the time to CTC mode. In this mode the timer will restart when it reaches that value set to OCR1A. So the lower the value, the faster it restarts, so the higher the frequency.

The OCR1B value only changes where in this cycle the interrupt occurs. It will however still only be called once per timer1 overflow.

answered May 4, 2015 at 18:36
2
  • 1
    Short but very clear explanation. respect Commented May 4, 2015 at 18:42
  • Thanks. Timers are a bit hard to understand, but very powerful. Commented May 4, 2015 at 19:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.