Arduino Uno: Interrupt Timer2

Hello,

I have tried to implement a ten minutes Intervall with Timer Overflow Interrupt for Timer 2 (Arduino Uno), but the interrupt seems never to start. The LED at pin 13 is always low.

#include <avr/sleep.h>
#define ledPin 13
 
/* Timer2 reload value, globally available */
volatile unsigned int tcnt2;
volatile long n = 0L;  
 
/* Setup phase: configure and enable timer2 overflow interrupt */
void setup() {
  
  /* Configure the test pin as output */
  pinMode(ledPin, OUTPUT); 
 
   /* First disable the timer overflow interrupt while we're configuring */   
   TIMSK2 = 0x00;
  
   
  /* Configure timer2 in normal mode (pure increment counting, no PWM etc.) */
  TCCR2A = 0x00;
   
   
  /* Select clock source: internal I/O clock (SCHAUEN, OB ES LANGSAMEREN CLK GIBT --> ES GIBT NUR EXTERNEN COUNTER ZUSÄTZLICH) */
  ASSR = 0x20;  
  
   
  /* Now configure the prescaler to CPU clock divided by 1024 */
  // Prescaler auf 1024
  TCCR2B = 0x07;  
  
  
  /* Save value globally for later reload in ISR */
  tcnt2 = 0x01; 
 
  /* Finally load end enable the timer */
  TCNT2 = tcnt2;
  
  //Interrupt im Statusregister SREG freigeben
  SREG = 0x80;

  /* Disable Compare Match A interrupt enable (only want overflow) */
  TIMSK2 = 0x01;

  // Enable Interrupt in TIFR2-Register
  TIFR2 = 0x01;

  // activate power-save mode (auf Nomenklatur achten)
  SMCR = 0x07;

  // SLEEP instruction 
  sleep_enable();
  sleep_mode();
  
}

/*  void measurement () {
    Serial.println("starte messung");
    // during measurment IR has to be disabled
} */
 

 ISR(TIMER2_OVF_vect) {   
    // clear sleep enable
    sleep_disable();
        
       
    if (n == 937500L){
     // measurement();
      for (int i = 0; i < 3000; i++) {
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);
      digitalWrite(ledPin,HIGH);         
       } 
      n = 0;
      
    }   
  
  n = n + 1;
  
 } 

void loop() {
  
  // SLEEP instruction 
  sleep_enable();
  sleep_mode();

  digitalWrite(ledPin,LOW); 
 
}

At the moment I cannot find the error. Therefore I would be happy to get a hint to solve the problem. Moreover I'm interested to know, how to control the counter of timer 2, if I do not use a serial print.

Regards and thank you very much for your help
BeDa

Timer2 is only a 8-bit counter/timer on the 328P processor. I would use millis() function, but since seem to need sleep mode, another interrupt besides TIMER2_OVF_vect would seem appropriate.

Best to use Watchdog timer in interrupt mode. See Nick Gammon's watchdog timer interrupt example here...

  SREG = 0x80;

Don't do that. Use the interrupts() or sei() macros instead. Also, don't turn on global interrupts until after you've fully configured your hardware. Interrupt are already enabled by the Arduino core, so you should turn them off before you start fiddling around with the Timer2 registers, than turn them back on once you are done.

  1. Check the datasheet more carefully:
  /* Select clock source: internal I/O clock (SCHAUEN, OB ES LANGSAMEREN CLK GIBT --> ES GIBT NUR EXTERNEN COUNTER ZUSÄTZLICH) */
  ASSR = 0x20;

Setting the AS2 bit in ASSR makes Timer2 use the external crystal as the clock source. The crystal driver is designed for a 32 kHz watch crystal, not the 16 MHz resonator used by an Uno. It might not operate correctly.

  /* Finally load end enable the timer */
  TCNT2 = tcnt2;

This does not enable the timer. The timer was already enabled before this. Asynchronous mode has many caveats and special procedures to use it properly. Section 22.9 of the datasheet has more than a full page of bullet points about the procedure you need to go through to change Timer2's registers without corrupting its values.