cancel
Showing results for 
Search instead for 
Did you mean: 

Modify PWM frequency on the fly (STM32F4)

quintanabarcia.pablo
Associate II

Hello, Community.

I am currently trying to control a power converter with a STM32F4 microcontroller. The thing is that the control variable is the frequency of the PWM. I have programmed a timer (TIM3) that generates an interruption every 100us. During this interruption, the MCU has to calculate the new frequency value of the PWM in order to switch the power MOSFET.

I tried two options but none of them work as I want.

case a) Modifying the ARR register on the fly.

If I do this, I have seen that many times, the PWM doesn't work and remains at high level during a certain time (400us or so). Please, kindly see the yellow pulses. It obviously has effects over the power converter.

0690X000008iQqvQAE.jpg

case b) I thought that since the register is not updated inmediately, I could reset the counter of the PWM timer. This time, the PWM no longer remains high during a certain time but it provides strange pulses.

TIM8 -> CNT = 0;			
TIM8->ARR = "calculated value";	

0690X000008iQrPQAU.jpg

I read about diferent registers to make the updates of the registers inmediately but I am not capable. I tried many things but they don't work.

Could you give me a hint? Thanks a lot.

11 REPLIES 11
quintanabarcia.pablo
Associate II

Thanks to all

Greetings ,

Can you please explain how did u achive your goal ? I am doing as below

while (1)
  {
		HAL_Delay(2000);
		//TIM5->ARR  = _arr;
		//TIM5->CCR1 = _arr / 2;	
		while(__HAL_TIM_GET_COUNTER(&htim5) < _arr);
		HAL_TIM_PWM_Stop(&htim5, TIM_CHANNEL_1);
		_pwm_freq += 10;
		_arr = _pwm_timer_freq / _pwm_freq;	
		_ccr = _arr / 2;		
		
 
		__HAL_TIM_SET_AUTORELOAD( &htim5, _arr );
		__HAL_TIM_SET_COMPARE(&htim5, TIM_CHANNEL_1, _ccr);
		HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_1);
  }