2022-08-30 07:15 AM
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?
Solved! Go to Solution.
2022-08-31 09:56 AM
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.
2022-08-30 12:05 PM
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
2022-08-30 12:39 PM
Would you mind please explaining how I can do that? Sorry, im new to STMCubeIDE
2022-08-30 03:09 PM
I don't use CubeIDE. There is a peripheral registers view there somewhere.
JW
2022-08-31 06:23 AM
@Community member No im not using interrupt for the timer
2022-08-31 07:50 AM
Are you using the PWM mode? or just using the timer interrupt to toggle some gpios
2022-08-31 08:51 AM
Ive found the TIM1 registers. This shows the values during runtime. This setup is currently just giving a HIGH signal on the output.
2022-08-31 09:56 AM
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.
2022-08-31 10:20 AM
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.