cancel
Showing results for 
Search instead for 
Did you mean: 

How to change output frequency of timer during run time

SA.17
Associate III

I am using stm32L073Rz

I am using Tim2 channel 3 to generate different frequencies

As per an example ,i configure timer 2 channel 3 in TIM_OCMODE_TOGGLE mode

when i generate code using cubemx i get the desired frequency using below code snippet

but i want to change the frequency during runtime

so i use same code ,just change sConfigOC.Pulse value in main() but than output stops coming

so how to change output frequency at my will in the program,can i write some register directly ?

sConfigOC.Pulse = 16000;
  if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */
	HAL_Delay(500);
 if(HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_3) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }

4 REPLIES 4
Skuma.1764
Associate II

Sir

I have STM32 B-L072Z-LRWAN1 module I try to connect this board to The Thing Network(TTN) server by using example" I-CUBE_LARWAN" but unable to connect. please support as soon as possible.or provide other example code.

Thanks&Regards

santosh kumar

IIT Ropar.

> I have STM32 B-L072Z-LRWAN1 module I try to connect this board to The Thing Network(TTN) server by using example" I-CUBE_LARWAN" but unable to connect.

> please support as soon as possible.or provide other example code.

I don't know the answer, but it's unlikely anybody will answer you if you hijack others thread. Please start your own thread, tag it according to the mcu you are using, and change your username to a normal nick.

JW

> so how to change output frequency

I don't use Cube so can't help with that.

You may want to start with reading the TIM chapter in RM.

You can't change *frequency* by changing TIMx_CCR, which is probably what you tried to do when you changed Pulse. In Toggle mode, it either results in the same 50% duty and same frequency, if TIMx_CCR<TIMx_ARR; or it results in pulses disappearing if TIMx_CCR>TIMx_ARR, which is what you probably experienced.

If you would use PWM mode rather than TOGGLE mode, then changing TIMx_CCR (again within 0<CCR<ARR) would change the *duty* cycle of the pulse, i.e. its width, not frequency.

To change *frequency*, you need to change the timebase, i.e. TIMx_ARR.

JW

Piranha
Chief II

Like Jan said, change ARR register, or, depending on requirements, can also be done by changing PSC (prescaler) register. The latter approach doesn't require changing CCR to stay at the same pulse width proportion.