TimerOne running at twice speed

I programmed a Teensy 4.1 using Timer1 at the TimerOne library. It is easy to manage and works very well bu goes 2x faster than expected.
I have seen at the TimerOne.h file at the setPeriod) function that it is multiplied the microseconds by 0.0000005f and perhaps should be 0.0000010f

Code:
   void setPeriod(unsigned long microseconds) __attribute__((always_inline)) {
	uint32_t period = (float)F_BUS_ACTUAL * (float)microseconds * 0.0000005f;
 
Please show the complete program I should copy into Arduino and run on a Teensy 4.1 to reproduce the problem. Even if the code is "trivial", even if the bug is "obvious", please reply with a complete program so I don't have to guess the code you tried.
 
Hey Paul, here is:

Code:
//In the setup menu:
    //TEST Timer1
    Serial.println("========== ini test Timer1 ========");
    Timer1.initialize(100UL);//Interrupcion cada 100us
    Timer1.attachInterrupt(timer1_funcion); // Calls function timer1_funcion() 
    delay(8000);
    Serial.println("========== end test Timer1 ========");
......


//AND here is the called function:
void timer1_funcion()
{
  static uint32_t timer1_openloop=0,loop=0; 
  timer1_openloop++;
  if (timer1_openloop>=10000) { Serial.println((String) "LOOP: " + loop);timer1_openloop=0;loop++;}
}

If you run that code it should appear 8 Serial prints between ini test and end test, but appears 16 in 8 seconds (the delay function waits 8 seconds)
 
The global variables used/declared in the interrupt routine, must also be declared volatile.
Code:
  volatile static uint32_t timer1_openloop=0,loop=0;

Pete
 
Back
Top