Skip to main content
Zakba.1
Associate III
December 12, 2022
Solved

set timer to interrupt every 500ns(2MHZ)

  • December 12, 2022
  • 4 replies
  • 1307 views

I am using stm32F103RE with 72mhz timer clock for using timer 1. I set prescalor to 0 and ARR to 35 to make 2MHZ timer interrupt.(72/36=2) to check it I use a variable and add it every 2,000,000 like the code below...I expect it to be added every 1s but it is slower !! and setting it to 2MHZ maked systick timer stops working unless changing the interrupt priority to 15!!

can anybody help me?

here in the code in IRQ_TIM1:

void TIM1_UP_IRQHandler(void)

{

 HAL_TIM_IRQHandler(&htim1);

 /* making 1s to check interrupt routine */

t1++;

if(t1 == 2000000 )

{

t1=0;

timer_1s++;

}

 /* USER CODE END TIM1_UP_IRQn 1 */

}

This topic has been closed for replies.
Best answer by KnarfB

The IRQ handler (including HAL) is not fast enough to fininsh in the available period (36 CPU cycles).

hth

KnarfB

4 replies

KnarfB
KnarfBBest answer
Super User
December 12, 2022

The IRQ handler (including HAL) is not fast enough to fininsh in the available period (36 CPU cycles).

hth

KnarfB

Zakba.1
Zakba.1Author
Associate III
December 12, 2022

thank you for your reply. what is the min time for interrupt routine? how is it possible to make 2MHZ PWM?

KnarfB
Super User
December 12, 2022

IRQ latency depends on the ARM Cortex core, flash (code access) latencies and software overhead. Several dozend or even hundreds of cycles sum up quickly. You may test it by successively lowering the timing and watching the results. A cheap logic analyzer could also help here.

The timers do have hardware PWM support, search for blue pill timer pwm tutorials.

hth

KnarfB

Zakba.1
Zakba.1Author
Associate III
December 12, 2022

so it depends on the cortex core...thank you so much:)