cancel
Showing results for 
Search instead for 
Did you mean: 

Changing a PWM complementary pins to GPIO functionality and then back to PWM at runtime

Ricko
Senior

Hi,

I am running TMR1 CH1 and CH1N in complementary mode (so they are the opposite of each other).

 

Using a HAL function, how do I change each (separately) from PWM to GPIO and back to PWM?

 

Just so to explain what I am trying to do... I am driving a H-bridge, I want to be able to:

- run the PWM in complementary mode (i.e. biphase/bipolar bridge)

- fix one half of the bridge to high or low while the other side still runs in PWM

- switch both to either high or low

- go back to full PWM complementary

 

Thank you

4 REPLIES 4
TDK
Guru

Use HAL_GPIO_Init to initialize them as GPIO_MODE_OUTPUT_PP, and control their states with HAL_GPIO_WritePin.

When you want to switch back to PWM mode, call HAL_GPIO_Init again and set them up in alternate function TIM mode. If you've set the timer up in PWM mode in CubeMX, the initialization calls to set the pins in PWM mode are in HAL_TIM_MspInit, call that again or call it within your own code.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you @TDK,

 

I also just came across HAL_TIM_PWM_DeInit(). Do I need to use it or is it better not to?

 

In the documentation it just says that it deinitialises the timer and I am not sure what that means exactly... it is not clear which functionality is actually reset.

This is the API document I am looking at, is there a more comprehensive one perhaps?

 

Thank you 🙂

 

> I also just came across HAL_TIM_PWM_DeInit(). Do I need to use it or is it better not to?

I wouldn't. You can change the pins and keep the timer going in the background. That seems easiest for your use case.

> This is the API document I am looking at, is there a more comprehensive one perhaps?

The reference manual will detail exactly how the timers work. If you want the intimate details, that is the reference to look at.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you @TDK 

I just tried implementing what you suggested i.e. calling HAL_TIM_MspInit() but there is no sign of it in the generated code anywhere in the project. There are other MspInit() functions as you can see from the screenshot here but not that one.

 

Ricko_3-1722822788563.png

 

The only functions generated by CubeMX are these:

 

Ricko_4-1722822815619.png

 

I am new to STM32 so I am wondering if it could be because I am using CH1 and CH1N with TMR1 configured like in the screenshot below? The generated complementary PWM outputs are correct on the oscilloscope but perhaps I configured it incorrectly or in a less than ideal manner (although )? Maybe using different settings would generate the HAL_TIM_MspInit() you are referring to?  Or perhaps I need to #include some header?

 

Ricko_0-1722822285588.png

 

Thank you again! 🙂