Skip to main content
SKUND.11
Associate III
April 4, 2022
Solved

How to start PWM without using HAL

  • April 4, 2022
  • 2 replies
  • 5413 views

I have initialise the code in Low layer(No HAL).So now i have config the timer channel as PWM generation output and set clock, ARR,duty cycle as per the requirement.

Earlier when I was using HAL, after initialising i can just wrote HAL_TIM_PWM_Start function and it will automatically start the timer as pwm. But now when I'm using Low layer, there is not function for that. So how can i Start the timer in pwm or any mode(output,input compare mode etc.) using the register or any low layer functions.

For ref.- There is a register (TIM1->CR1 |= TIM_CR_CEN ) this will start the timer.

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

Generally, the procedure is:

  • enable GPIOx and TIMx clock in RCC
  • set the appropriate pin's GPIOx_MODER to AF, and GPIOx_AFR to appropriate value; set also GPIOx_OSPEEDR if there's a need for higher slew rates
  • set TIMx_CCMRx to set given channel to Output Compare, and one of the PWM modes
  • set TIMx_CCRx to set duty cycle
  • enable given channel in TIMx_CCER
  • in Advanced timers, set TIMx_BDTR.MOE
  • set TIMx_PSC/TIMx_ARR to set period
  • set TIMx_CR1.CEN to start timer's counter

If there's any problem, read out and check/post GPIO and TIM registers content.

JW

2 replies

waclawek.jan
Super User
April 4, 2022

Which STM32? Which timer?

> There is a register (TIM1->CR1 |= TIM_CR_CEN ) this will start the timer.

OK, so if you know how to start the timer, what exactly is your problem?

Cube/LL is open source, so if you for some reason must use LL functions/macros, just look up which one does what you need.

JW

PS. If still no PWM output, read out and check/post the timer and relevant GPIO registers content. Note, that Advanced timers need to have TIMx_BDTR.MOE set to enable outputs.

SKUND.11
SKUND.11Author
Associate III
April 4, 2022

Thank you for replying.

Sorry there was a mistake..that register will start the counter value of the timer.

waclawek.jan
waclawek.janBest answer
Super User
April 4, 2022

Generally, the procedure is:

  • enable GPIOx and TIMx clock in RCC
  • set the appropriate pin's GPIOx_MODER to AF, and GPIOx_AFR to appropriate value; set also GPIOx_OSPEEDR if there's a need for higher slew rates
  • set TIMx_CCMRx to set given channel to Output Compare, and one of the PWM modes
  • set TIMx_CCRx to set duty cycle
  • enable given channel in TIMx_CCER
  • in Advanced timers, set TIMx_BDTR.MOE
  • set TIMx_PSC/TIMx_ARR to set period
  • set TIMx_CR1.CEN to start timer's counter

If there's any problem, read out and check/post GPIO and TIM registers content.

JW

SKUND.11
SKUND.11Author
Associate III
April 4, 2022

Thank you sir.