cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f103c8t6 how to comfigure TIM3 for a period of 5ms

Keita
Associate II

Hi community,

I am trying to port a project from old library maple leaf or older built for Arduino to to STM32cubeIDE

this function I couldn't find any ref to it, my guess it that is sets the period to 5ms

 

Timer3.setPeriod(1000 / 200);

 

browsing some threads in the community I found this formula for calculating the period

TIM Update = TIM Clock / (X * Y) Prescaler = X - 1 Period = Y - 1

according to it I put Presclaer X-1 = 59999 and Period to 5999

 

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 59999;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 5999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 

Actually the entire code compiles but in the debug session TIM3 never starts, can any knowledgeable person help me to configure this timer for 5ms. thank you in advance.

Note: complete newby here I just started learning about electronics and mcu...

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Your understanding is correct, but not sure the math checks out.

Let's say your timer clock is 72 MHz and you want an update period of 5ms (so 200 Hz update frequency).

Your need a prescaler and period to combine to (72 MHz / 200 Hz) = 360000. One particular set that will satisfy this is PSC (prescaler) = 5 and ARR (period) = 59999.

If instead you have PSC=5999, then the update frequency is 0.2 Hz, or once every 5 seconds.

> in the debug session TIM3 never starts

What does "starts" mean here? Are you watching TIM3->CNT, or are you expecting some code to be called or some event to happen such as an LED blinking?

 

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

View solution in original post

8 REPLIES 8
TDK
Guru

Your understanding is correct, but not sure the math checks out.

Let's say your timer clock is 72 MHz and you want an update period of 5ms (so 200 Hz update frequency).

Your need a prescaler and period to combine to (72 MHz / 200 Hz) = 360000. One particular set that will satisfy this is PSC (prescaler) = 5 and ARR (period) = 59999.

If instead you have PSC=5999, then the update frequency is 0.2 Hz, or once every 5 seconds.

> in the debug session TIM3 never starts

What does "starts" mean here? Are you watching TIM3->CNT, or are you expecting some code to be called or some event to happen such as an LED blinking?

 

If you feel a post has answered your question, please click "Accept as Solution".
Karl Yamashita
Lead II

Without you posting more code, don't know if you even started the timer?

If you find my answers useful, click the accept button so that way others can see the solution.
Keita
Associate II

Thanks I found the culprit

 

 

initialization happens in the TimBase class

void TimBase::init(void){
	if (_Instance == TIM1) {
		if(is_init[0]){
			return;
		}
		is_init[2] = true; //the culprit TIM1 init() call sets TIM3 FLAG
		MX_TIM1_Init();
		return;
	}else if (_Instance == TIM3) {
		if(is_init[2]){
			return;
		}
		is_init[2] = true;
		MX_TIM3_Init();
		return;
	}
}

that's one of issues solved, according to my setting can you tell me if I got it right? the timer3 runs for 5ms.

 

The code shows your are calling MX_TIM1_Init() or MX_TIM3_Init(), but I still don't see code that starts the timer?

HAL_TIM_Base_Start_IT?

If you find my answers useful, click the accept button so that way others can see the solution.
TDK
Guru
		if(is_init[0]){
			return;
		}
		is_init[2] = true; //the culprit TIM1 init() call sets TIM3 FLAG
...
		if(is_init[2]){
			return;
		}
		is_init[2] = true;

Well this seems bogus.

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

This method does the init as per MXcube configfor TIM3 wich fails

MX_TIM1_Init

As per HAL_TIM_Base_Start_IT() it is called inside the member method start() in the main.cpp

  tim3.start();
  adc1.start();

Like I said the issue with starting TIM3 was related to the init() needed by my own code where the flag for TIM1 was setting init[2] instead of init[0] so the code sets over when calling init() for TIM3 why the start method fails.

the current issue is more related to the period as I explained.

Keita
Associate II

sorry for late replay. I am now trying to solve another technical issue 🙂 regarding the modulation of the analog signal inside the PWM. kind of a hassle I am trying to make a lazer sound spot with transducers array TC40T :p I know not the best component but that's what can use for now. Thank you again

Is there an actual question anywhere in there?  🤔