0
\$\begingroup\$

In my code I use HAL_TIM_PWM_Start_IT to start a PWM generation and HAL_TIM_PWM_Stop to stop the PWM train. I need to check whether PWM is stopped or not at some part of the code.

How can that be implemented?

\$\endgroup\$
4
  • \$\begingroup\$ Which STM32 exactly? The timer peripherals vary between parts, and there are also multiple versions of the timer that can be found within a single part. \$\endgroup\$ Dec 4, 2022 at 2:22
  • \$\begingroup\$ Using now Nucleo F303K8. st.com/resource/en/data_brief/nucleo-f303k8.pdf \$\endgroup\$
    – GNZ
    Dec 4, 2022 at 2:28
  • \$\begingroup\$ And which of the timers? The STM32F303K8 has about ten of them and they all have different capabilities. \$\endgroup\$ Dec 4, 2022 at 2:31
  • \$\begingroup\$ I use TIM1 and TIM3 to generate two PWM. \$\endgroup\$
    – GNZ
    Dec 4, 2022 at 2:33

2 Answers 2

0
\$\begingroup\$

You can check the CEN bit in the timer's CR1 register. The HAL sets it when the timer is enabled in PWM mode, and clears it again when all PWM channels on that timer have been disabled (with HAL_TIM_PWM_Stop).

Reference manual for your microcontroller (see section 20, Advanced Control Timers)

\$\endgroup\$
9
  • \$\begingroup\$ By disabled do you mean HAL_TIM_PWM_Stop ? \$\endgroup\$
    – GNZ
    Dec 4, 2022 at 2:43
  • \$\begingroup\$ Yes, HAL_TIM_PWM_Stop will clear CR1->CEN. HAL_TIM_PWM_Start(_IT) will set it. \$\endgroup\$ Dec 4, 2022 at 2:44
  • \$\begingroup\$ How would I read that bit? into an integer(0 or 1)? Im a bit in a hurry. TIM_CR1_CEN is the mask and TIM1->CR1 accesses the register. Should I write TIM1->CR1 and ? I need to read CEN somehow as you said. My question was about the implementation actually. \$\endgroup\$
    – GNZ
    Dec 4, 2022 at 2:50
  • \$\begingroup\$ How can we read the CEN bit I mean? Not setting but reading it. \$\endgroup\$
    – GNZ
    Dec 4, 2022 at 3:02
  • \$\begingroup\$ is it like int x = TIM_CR1_CEN;? \$\endgroup\$
    – GNZ
    Dec 4, 2022 at 3:08
0
\$\begingroup\$

I know that there are many ways to check for hardware status, using registers and what not. But I always prefer simple straightforward solutions.

In this case, since you are the one doing starting/stopping of the PWM generation, why don't you simply declare status flag and set/reset it in your own Start() Stop() functions right after calling HAL (assuming HAL returned HAL_OK, of course)?

Then you can check this flag either directly or via another function of your own. The benefit of this approach is complete transparency and portability.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.