Timer working 20 times, then not worrking (Arduino UNO)

Hello,

I want to set an ISR every 20 ms. So I use the timer1.
At the begenning I get an interruptions every 20ms, but then it interrupts every 0 ms:

Monitor output:
19.00 ms
20.00 ms
21.00 ms
20.00 ms
20.00 ms
20.00 ms
21.00 ms
20.00 ms
21.00 ms
20.00 ms
21.00 ms
19.00 ms
21.00 ms
20.00 ms
21.00 ms
0.00 ms
0.00 ms
0.00 ms
0.00 ms

My code:
/************ static functions common to all instances ***********************/
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t;

static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t TCNTn, volatile uint16_t OCRnA)
{
Serial.println(time.GetloopTime()); // Print time between 2 interruptions
*TCNT1 = 0; // Reset timer
*OCR1A= = usToTicks(20000); // Interrupt every 20 ms
}

SIGNAL (TIMER1_COMPA_vect)
{
handle_interrupts(_timer1, &TCNT1, &OCR1A);
}

void setup() {
TCCR1A = 0; // normal counting mode
TCCR1B = _BV(CS11); // set prescaler of 8
TCNT1 = 0; // clear the timer count
TIFR1 |= _BV(OCF1A); // clear any pending interrupts;
TIMSK1 |= _BV(OCIE1A) ; // enable the output compare interrupt

// initialize serial communication
Serial.begin(1200);

time.Init();

OCR1A = usToTicks(20000);
}

void loop() {
// put your main code here, to run repeatedly:
}

Thank you,
Julien

Which Arduino are you using?

This does not compile:

sketch_apr10a:7: error: expected primary-expression before '=' token
   *OCR1A= = usToTicks(20000); // Interrupt every 20 ms

'= =' ?

Pete

P.S. It is not a good idea to use Serial in an interrupt routine. and 1200 baud is awfully slow. Try 9600 or even higher.

Pete

"'= =' ?"
Sorry, it is a mistake I did when posting on the forum.

"P.S. It is not a good idea to use Serial in an interrupt routine."
I know, it is just for debug purposes and to learn how to handle the timer.

" and 1200 baud is awfully slow. Try 9600 or even higher."
Thank you very much!! It works when serial is set to 384000 bauds!!!

Thank you very much, I was losing my mind since hours!!

Have a nice day!
Julien