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