Timer 2 Interrupt not working properly.

Hi,
I'm plannig to use timer2 interrupt to increase a counter, but it seems I am doing something wrong because if the marked code

                                                 /* next line  is the important one */
  Serial.print(".""\n");          /* <---  <---  <---  <---  <---  <---  <---  */
                                                    /* past line  is the important one */

is deleted the interrupt is not working, also if the "." is removed or replaced for " " the interruption does not work.

Hope you can help me :]

unsigned long Position;
byte Clkout;

void setup() {
Clkout = 9;
pinMode (Clkout, OUTPUT);
Serial.begin(19200);   
noInterrupts();  
///------------------------------------
/*Timer2 Configuration for interrupt at 400Hz in order to generate pulse for correct*/
TCCR2A = 0;               //Clear TCCR2A
TCCR2B = 0;               //clear TCCR2B
TCNT2  = 0;               //initialize counter value to 0
OCR2A = 249;              // = (16*10^6) / (400*255) - 1 (must be <256)
TCCR2A |= (1 << WGM21);   // Set CTC mode in timer2TCCR2B |= (1 << CS21); 
TCCR2B |= (1 << CS22); 
TIMSK2 = 0;               //Timer2 interrupts disabled
}
///------------------------------------
/*Timer 2 interrupt*/
ISR(TIMER2_COMPA_vect){
  TIMSK2 = 0;               //stop timer /timer2 interrupt disabled
  TCNT2  = 0;               //initialize counter value to 0
  Position += 1;
  Serial.print(Position);
  Serial.print("\n");
  digitalWrite(Clkout, !digitalRead(Clkout));
}

///------------------------------------
void loop() {
                                                    /* next line  is the important one */
  Serial.print(".""\n");          /* <---  <---  <---  <---  <---  <---  <---  */
                                                    /* past line  is the important one */
  interrupts();
  //Serial.print("\n");
  TCNT2  = 0;               //initialize counter value to 0
  TIMSK2 |= (1 << OCIE2A);  // enable timer compare interrupt
}

Thanks!

but it seems I am doing something wrong

You turned interrupts off, but never turned them back on, in setup().

Before f**king with timer 2 again in loop(), you need to disable interrupts and then enable them again.

Why ARE you diddling with timer2 on EVERY pass through loop()?

Printing . or space makes NO difference, except that you can SEE one, and not the other.

1- I thought doing this was the appropriate way to enable/disable timer2 interrupt

TIMSK2 = 0;               //stop timer /timer2 interrupt disabled
TIMSK2 |= (1 << OCIE2A);  // enable timer compare interrupt

2- In older version of the code i had:

int First = 0;

void loop() {
                                                    /* next line  is the important one */
  Serial.print(".""\n");          /* <---  <---  <---  <---  <---  <---  <---  */
                                                    /* past line  is the important one */
  interrupts();
  //Serial.print("\n");
  TCNT2  = 0;               //initialize counter value to 0
  if  (First == 0){
      First += 1;
      TIMSK2 |= (1 << OCIE2A);  // enable timer compare interrupt
  }
}

But the result was the same.

@OP

When you say Timer-2 interrupt, it is very confusing. As there are three interrupts with TC2 (see figure below), you need to be specific in your saying like:

Timer-2 Overflow Interrupt
Timer-2 Output Compare A Match Interrupt
Timer-2 Output Compare B Match Interrupt

  TIMSK2 |= (1 << OCIE2A);  // enable timer compare interrupt

i think its important to read before answer....

J050:

  TIMSK2 |= (1 << OCIE2A);  // enable timer compare interrupt

i think its important to read before answer....

//enable timer-2 compare A interrupt?

You need to seriously look at your code again. I've just spent the last 20 minutes staring at it without gaing the least bit of insight as to what your trying to do. Your loop accomplishes nothing but continually resetting the counter (why??) and printing dots on the monitor (why????).

You can't use a print statement in an ISR and (1610^6) / (400255) -1 equals 155.8627.

When you use CTC mode in a timer you should never touch TCNT as this will screw up your timing.

Grab a coffee/beer and start over. Don't try to modify this. Read the section in the datasheet on PWM and Google Arduino PWM secrets.

As I see some confusing ansers, and i think its because maybe my explanation & purpose of the code is not clear enought, I'll try to explaing again.

I have spend 4 days researching whoe to solve my problem but i did not find a working solution.

I wanted to count the pulses generated on one pin (in output mode) from the arduino using the timer2 compare match interruption to either increment the counter and switch the state of the pin.

I think counting pulses must be possible since i generate those pulses by switching the output of a pin instead of using the pwm output so i know each 2 in terruptions equal to a period and if i need X pulses, i'll have to wait untill 2X interrupts happened and while im waiting, i stay into a loop (whyle Position ! = 2X).

1- All the "Serial.print" instructions are just for debugging.

2- Is this te right configuration?
I am asking that as now i doubt from everything I made.

TCCR2A = 0;               //Clear TCCR2A
TCCR2B = 0;               //clear TCCR2B
TCNT2  = 0;               //initialize counter value to 0
OCR2A = 249;              // = (16*10^6) / (400*255) - 1 (must be <256)
TCCR2A |= (1 << WGM21);   // Set CTC mode in timer2TCCR2B |= (1 << CS21);
TCCR2B |= (1 << CS22);
TIMSK2 = 0;               //Timer2 interrupts disabled

3- When the ISR is executed, the purpose is to 1 stop timer2, so this way i can initialize it to 0 and won't start counting while doing the "test prints" and then increment a counter, switch state from LOW <-> HIGH
When the interrupt has done, in the loop i set the timer2 compare interrupt so it can happen again.

*Timer 2 interrupt*/
ISR(TIMER2_COMPA_vect){
  TIMSK2 = 0;               //stop timer /timer2 interrupt disabled
  TCNT2  = 0;               //initialize counter value to 0
  Position += 1;
  Serial.print(Position);
  Serial.print("\n");
  digitalWrite(Clkout, !digitalRead(Clkout));
}

4- The loop is just a loop checking if the variable incremented on the interrupt has reached the limit, in order to know if the interrupt has to be enabled again or it has to keep disabled as the pulses required has been done.

void loop() {
                                                    /* next line  is the important one */
  Serial.print(".""\n");          /* <---  <---  <---  <---  <---  <---  <---  */
                                                    /* past line  is the important one */
  interrupts();
  //Serial.print("\n");
  TCNT2  = 0;               //initialize counter value to 0
  if  (First == 0){
      First += 1;
      TIMSK2 |= (1 << OCIE2A);  // enable timer compare interrupt
  }
}