Skip to main content
Parmin Nayani
Associate III
October 14, 2022
Question

Hi all, I am back with another problem!! I am using timer 2 in 103C8 controller for 0.1 Sec time delay. However, it is generating 5 times the calculated delay. My initialization code for this delay is given below. Timer interrupt is enabled.

  • October 14, 2022
  • 7 replies
  • 1821 views

htim2.Init.Prescaler = 1000;

htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

htim2.Init.Period = 7200;

htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

However if I change the prescaler to 200, I get the correct delay.

HAL_RCC_GetPCLK1Freq () function returns 36 MHz. Timer 2 clock source, which is APB1 will be 72 MHz. Where am I making a mistake? Thank you for your time.

This topic has been closed for replies.

7 replies

Tesla DeLorean
Guru
October 14, 2022

The values are N-1

But that doesn't explain the situation. Perhaps look at the crystal value, and those of the PLL.

Check HSE_VALUE

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Parmin Nayani
Associate III
October 14, 2022

Hi Tesla DeLorean,

Thanks for the reply. The strange thing is even the USB works, which means the 48 MHz derived from the main clock is matching. When I change the 1000 prescaler to 200 I get 0.1 Sec interval. To find this out I have global variable in which I am loading 10 and waiting for it to go to 0. In the timer interrupt, I am decrementing this count if not 0. This is generating almost 1 Sec delay. Is there anything else I can check?

waclawek.jan
Super User
October 14, 2022

Read out and check/post content of RCC registers.

What is the primary clock source? A crystal in HSE? What is its frequency?

JW

Parmin Nayani
Associate III
October 14, 2022

Hi JW,

HSE is 8MHz crystal. the problem does not look like that of the main clock as USB, which requires reasonably accurate clock, is working fine in CDC mode. Will read out the RCC registers and post. Thank you.

ONadr.1
Senior III
October 14, 2022

Is not the problem that your couner count UP? (65536-7200) count to interrupt?

Parmin Nayani
Associate III
October 14, 2022

Hi ONadr.1,

Yes I also thought of this and tried with down count. Still the timing was same. Come to think of it there must be difference between down and up counting. Will check again. Thanks.

waclawek.jan
Super User
October 15, 2022

While at it, read out and post also TIM registers content.

JW