Skip to main content
samsamm777
Associate II
August 30, 2022
Solved

STM32F303RET6 Timer PWM Output Not working as expecte

  • August 30, 2022
  • 8 replies
  • 1862 views

I am trying to configure a PWM output pin. Ive managed to get it working, however the results are not as expected, and i cannot understand whats happening.

Currently my timer clocks are running at 64MHz.

I've setup a timer with the follow values, and it works in producing a 10kHz PWM. The prescalar being 64, should give a frequency of 1MHz. As 64/64=1. Then the Period being 100 should be freq/100 resulting in 0.01MHz. Which is 10kHz. This pwm shows up perfectly on my oscilloscope.

 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 64-1;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = 100-1;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

However if i change the Period value to 10-1, i would expect a PWM of 100KHz. However I dont get a PWM signal any more. The signal just goes HIGH, and does not change.

 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 64-1;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = 10-1;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

What am I doing wrong?

This topic has been closed for replies.
Best answer by waclawek.jan

CCR1 > ARR, i.e. CNT never reaches CCR1 thus can't "compare match" thus change output level.

JW

PS. Generally it's more useful to display registers in hexadecimal.

8 replies

waclawek.jan
Super User
August 30, 2022

Read out and check/post content of the TIM and relevant GPIO registers.

You don't use interrupt triggered by this timer, do you?

JW

samsamm777
Associate II
August 30, 2022

Would you mind please explaining how I can do that? Sorry, im new to STMCubeIDE

waclawek.jan
Super User
August 30, 2022

I don't use CubeIDE. There is a peripheral registers​ view there somewhere.

​JW

Javier1
Principal
August 31, 2022

Are you using the PWM mode? or just using the timer interrupt to toggle some gpios

hit me up in https://www.linkedin.com/in/javiermuñoz/
samsamm777
Associate II
August 31, 2022

0693W00000SuDBaQAN.pngIve found the TIM1 registers. This shows the values during runtime. This setup is currently just giving a HIGH signal on the output.

waclawek.jan
waclawek.janBest answer
Super User
August 31, 2022

CCR1 > ARR, i.e. CNT never reaches CCR1 thus can't "compare match" thus change output level.

JW

PS. Generally it's more useful to display registers in hexadecimal.

samsamm777
Associate II
August 31, 2022

Ah! so CCR isnt a percent. I was imagining it as the duty cycle percentage. But its not, its purely a count. So I need to calculate the value as a percentage of ARR.