Skip to main content
Associate
December 4, 2023
Solved

STM32f103c8t6 how to comfigure TIM3 for a period of 5ms

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...

 

This topic has been closed for replies.
TDK
TDKBest answer
December 4, 2023

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""."

8 replies

TDK
TDKBest answer
December 4, 2023

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""."
KeitaAuthor
Associate
December 14, 2023

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

David Littell
Senior II
December 14, 2023

Is there an actual question anywhere in there?  :thinking_face:

Karl Yamashita
Principal
December 5, 2023

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

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
KeitaAuthor
Associate
December 8, 2023

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.

KeitaAuthor
Associate
December 5, 2023

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.

 

Karl Yamashita
Principal
December 5, 2023

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 a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
TDK
December 5, 2023
		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""."