Timer2 interrupt not working

Hi

I’m writing a control program for something and need to check certain variables at precise intervals. I figured that timed interrupts would be ideal however nothing I’ve tried seems to work.

I wrote the code below from user manual, and found a couple examples that look exactly the same, however nothing happens.

Any suggestions as to what i’m doing wrong?

#include <avr/io.h>
#include <avr/interrupt.h>

// set pin numbers:
int ledpin = 13;
volatile int OVF_count = 0;

void setup() {
  /* 
   TIMER2 setup
   8 bit timer on 16MHz clock using 1024 precaler
   >> ~61 overflows per second
   */
  // set the prescaler for timer0 to 1024
  TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20);
  // enable overflow intterupts for timer0
  TIMSK2 |= (1<<TOIE2);
  // initailize the counter
  TCNT2 = 0;

  pinMode(ledpin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  // main code
}

ISR(TIMER2_OVF_vec){ 
  OVF_count++;
  if(OVF_count == 30){
    Serial.print(OVF_count); // should print about twice per second
    OVF_count = 0;
  }
}

Perhaps Serial.print requires interrupts, which are blocked while you're in the timer2 interrupt handler? Let the interrupt only increment your counter and do the check for >=30 and Serial.print from loop() instead. I'd also write something to TCCR2A to be sure the timer is running the proper way.

Ok, so I added a Serial print to the setup routine an noticed that the moment I enable the overflow interrupt for timer2 the setup routine is called thereby resetting all my variables.

int OVF_count = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing timerinterrupt");

  TCCR2A = 0;
  TCCR2B |= (1<<CS22)|(1<<CS21)|(1<<CS20);
  TIMSK2 |= (1<<TOIE2);
  TCNT2 = 0;

}

ISR(TIMER2_OVF_vec){ 
  OVF_count++;
}

void loop(){
  while(1)
  {
    delay(10);
    Serial.println(millis()); // should print about twice per second
  }
}

Now I really have no idea what’s going on :-/

The output looks like this

Initializing timerinterrupt 39 Initializing timerinterrupt 39 Initializing timerinterrupt 38 Initializing timerinterrupt 39 Initializing timerinterrupt 39 Initializing timerinterrupt 39 Initializing timerinterrupt 39 Initializing timerinterrupt 39

Forgot to add that

ISR(TIMER2_OVF_vec){

It’s supposed to be TIMER2_OVF_vect.
Your program resets when you activate an uninitialized interrupt handler.
Too bad this doesn’t generate a compile error!

Oh, and put the “volatile” modifier back on the declaration of OVF_count if you are going to access it from within loop().

It almost always is something stupid, isn't it.... one little letter and it works perfectly. and yes a compilation error would have been nice.

Thanks a lot :)

8 bit timer on 16MHz clock using 1024 precaler

~61 overflows per second

Now that it is working, have you wondered (as I did) why the interrupts only happen half as often as you expect? (Verify by printing the value of millisec() after each overflow.)

Well...

It happens that Arduino's init() configures timer2 in the "phase correct pwm" mode, so the counter counts down to zero and back up to max before generating another interrupt.

For a 16 MHz clock and the prescaler set to divide-by-1024, the result is that there will actually be an interrupt every 32.768 milliseconds, for an average of about 30.5 interrupts per second, not 61 as your comment indicates.

Try setting TCCR2A to zero in your setup() function to see the difference.

Regards,

Dave