Hello,
I would like to use Timer 2 to generate an overflow interrupt, but the ISR has to be only performed after the 10th overflow. I have written this code:
#include <SoftwareSerial.h>
#define ledPin 13
/* Timer2 reload value, globally available */
unsigned int tcnt2;
int n = 0;
/* Setup phase: configure and enable timer2 overflow interrupt */
void setup() {
Serial.begin(250000);
/* Configure the test pin as output */
pinMode(ledPin, OUTPUT);
/* First disable the timer overflow interrupt while we're configuring */
TIMSK2 = 0x00;
// Serial.println("Timer interrupt mask register - disable");
// Serial.println(TIMSK2);
/* Configure timer2 in normal mode (pure increment counting, no PWM etc.) */
TCCR2A = 0x00;
// Serial.println("Control Register A and B");
// Serial.println(TCCR2A);
// Serial.println(TCCR2B);
/* Select clock source: internal I/O clock (SCHAUEN, OB ES LANGSAMEREN CLK GIBT --> ES GIBT NUR EXTERNEN COUNTER ZUSÄTZLICH) */
ASSR = 0x20;
// Serial.println("internal clock");
// Serial.println(ASSR);
/* Disable Compare Match A interrupt enable (only want overflow) */
TIMSK2 = 0x01;
/* Now configure the prescaler to CPU clock divided by 1024 */
// Prescaler auf 1024
TCCR2B = 0x07;
// Serial.println("Control Register B");
// Serial.println(TCCR2B);
/* Save value globally for later reload in ISR */
tcnt2 = 0x44;
/* Finally load end enable the timer */
TCNT2 = tcnt2;
// Serial.println("timer start");
// Serial.println(TCNT2);
}
ISR(TIMER2_OVF_vect) {
digitalWrite(ledPin,HIGH);
// delay(10000);
Serial.println("Interrupt");
// Reload the timer
// TCNT2 = tcnt2;
// Serial.println("Interrupt Status Register");
// Serial.println(TIFR2);
n = 0;
}
void loop() {
// your program here...
if (n = 10){
//Interrupt im Statusregister SREG freigeben
SREG = 0x80;
digitalWrite(ledPin, LOW);
} else {
SREG = 0x00;
digitalWrite(ledPin, LOW);
}
// delay(10000);
// digitalWrite(ledPin, LOW);
// Serial.println("n");
// Serial.println(n);
Serial.println("Timer");
// Serial.println(TCNT2);
n = n + 1;
}
For me it is not possible to control the code clearly, because the LED at pin 13 is not toggling, but in the serial output the interrupt seems to be called.
Therefore I would like to know, if this code can really solve the task.
My next step after this is to put the MC into power-safe-mode and to wake up the MC after ten minutes (I have chosen n = 10 only for experiment) by the ISR for Timer 2. Because I'm beginner with the Controller, I would like to know about further options to leave power-save-mode (e.g. writting a command to SMCR). Thank you very much for your support.
Regards
BeDa