cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F303RET6 Timer PWM Output Not working as expecte

samsamm777
Associate III

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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.

View solution in original post

8 REPLIES 8

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

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

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

​JW

@Community member​ No im not using interrupt for the timer

Javier1
Principal

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

we dont need to firmware by ourselves, lets talk
samsamm777
Associate III

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

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 III

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.