cancel
Showing results for 
Search instead for 
Did you mean: 

Why toggling of Port E inside the timer 2 interrupt handler only work if the APB1 prescaler is set to /2 or /4 only?

nNg.1
Associate II

I have a Timer 2 interrupt handler  which will toggle the Port E output. My MCU is STM32F407 running at 168 Mhz. I found that the toggle code inside the interrupt handler only work if the APB1 prescaler is set to /2 or /4 only. Slower ABB1 frequency won't work.

The Timer 2 is set to toggle with the timer prescaler = 4000 & autoreload = 10000. This gives roughly 1Hz toggle frequency.

How does this happen?

5 REPLIES 5

What hardware? Which "port E output"? How do you observe it?

With increasing APB divider, TIM2 runs proportionally slower. 1Hz is very slow output toggle; if you increased divider, the output wouldn't change for several seconds. Did you wait that long?

Prepare a minimal but complete compilable code exhibiting the problem and post.

JW

TDK
Guru

Toggling functionality is not dependent on the APB1 prescaler.

Perhaps the system is overloaded with interrupts in one case while it makes progress in the other. How do you know the code is actually getting ran?

If you feel a post has answered your question, please click "Accept as Solution".
nNg.1
Associate II

I used a scope to monitor the PortE output and measure the frequency. By trial and error, I found out the problem. Yes, indeed somehow there is racing of interrupt. I have to add a DisableInterrupts at the beginning of the interrupt handler and then a EnableInterrupt before returning. Then lowering the APB1 clock has no effect now. I expect the C compiler to do this ( disable and enable of interrupt) for me. Anyway, I start with an example code and that example did not advice this insertion of DisableInterrupts & EnableInterrupt.

Thanks for yours advice anyway.

TDK
Guru

> I expect the C compiler to do this ( disable and enable of interrupt) for me.

Why would you expect the C compiler to disable/enable interrupts for you? The compiler does what you tell it to do.

If you feel a post has answered your question, please click "Accept as Solution".
nNg.1
Associate II

This is like saving of register contents on the stack, something that shall be taken care of by the compiler. Of course, there might be occasion where the programming prefer doing optimization of the code himself and would like to do it himself. But that shall be exception.