1

I have a program that uses Timer1 comparator A in CTC mode. I realize that I need to set OCR1C for the timer to reset at given value. However there is no such thing as an interrupt generated by timer1 counter matching OCR1C value.

Moreover, if I enable CTC and set for example OCR1A to 10, and OCR1C to 50, the interrupt handler ISR(TIMER1_COMPA_vect) will be triggered when the counter reaches OCR1C value, not OCR1A. I'm really confused...

Can anyone explain to me what's going on?

EDIT:

Here is the program that I'm using. main program:

#include "avr/interrupt.h"

ISR (TIMER1_COMPA_vect) {
  PORTB ^= (1<<PB0); //toggle pin 
}

void setup() {
  cli();
  // put your setup code here, to run once:
  //prescaler value 8192 sets one clock tick to about 1ms
  TCCR1 |= (1<<CS13)|(1<<CS12)|(1<<CS11)|(0<<CS10);
  //turn CTC mode on
  bitSet( TCCR1, CTC1 );
  //set comparatorA value
  OCR1A = 10;
  //enable compA interrupts
  bitSet(TIMSK, OCIE1A);
  //set comp C value
  OCR1C = 19;
  // initialize counter 1 on 0
  TCNT1 = 0;
  pinMode( 0, OUTPUT );
  digitalWrite( 0, HIGH );
  sei();
}

int main() {
  setup();
  // put your main code here, to run repeatedly:
  while(1) {
    }
 return 1;
}
  • You wrote “there is no such thing as an interrupt generated by timer1 counter matching OCR1C value”. Quoting the datasheet: “In PWM mode (either PWM1A=1 or PWM1B=1) the bit TOV1 is set (one) when compare match occurs between Timer/Counter1 and data value in OCR1C”. TOV1 is the flag that triggers the TIMER1_OVF interrupt. Note that you can enable PWM mode while keeping the PWM outputs disconnected from their pins (bits COM1xy cleared). – Edgar Bonet Apr 20 '17 at 9:15
  • @EdgarBonet Alright, but in non-PWM mode theoretically matchA interrupt should be called whenever TCNT1 reaches OCR1A value, right? But this does not happend. In fact it is triggered only when it reaches OCR1C – Em Ka Apr 20 '17 at 9:37
  • I uploaded your program to an ATtiny25V (essentially a slower ATtiny85 with less memory) and I get exactly the expected behavior: a square wave on pin 0 with a period of roughly 328 ms. Note that the clock runs at 1 MHz, as the CKDIV8 fuse is programmed by default. – Edgar Bonet Apr 20 '17 at 18:05
  • @EdgarBonet So you get a square wave that is high for OCR1A and low for OCR1C-OCR1A ticks? – Em Ka Apr 20 '17 at 18:23
  • I get 50% duty cycle, as expected from reading your code. – Edgar Bonet Apr 20 '17 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.