cancel
Showing results for 
Search instead for 
Did you mean: 

How to change GPIO parameters from OUTPUT to PWM, and vice versa?

OMarc.1
Associate III

Hi, everyone, I have the next problem, I am using the same pins like a simple GPIO output, and like a PWM output.

To change the output from PWM to GPIO i using this code:

   GPIO_InitTypeDef gpio;

   gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

   gpio.Mode = GPIO_MODE_OUTPUT_PP;

   HAL_GPIO_Init(GPIOB, &gpio);

But, how to return PWM initialization?

1 ACCEPTED SOLUTION

Accepted Solutions

You miss which AF mux value​ it should associate with the pin(s)

P​SC and ARR are N-1 comparators.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

9 REPLIES 9

I don't use Cube/HAL, but probably by using GPIO_MODE_AF_PP?

In reality you only need to change the bitfield which belongs to given pin in GPIO_MODERx register from 0b01 to 0b10.

JW

Hi, Jan.

It doesn't work.

But thank you for your answer.

Kind regards

What doesn't work? What did you do, and what are the results?

If PWM does not work, read out the TIM and relevant GPIO registers content and check/post.

Also, which STM32?

JW

Maybe i do something wrong, but i use the next code:

  GPIO_InitTypeDef gpio;

  gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

  gpio.Mode = GPIO_MODE_AF_PP;

  HAL_GPIO_Init(GPIOB, &gpio);

//MX_TIM8_Init();

 TIM8->ARR=1023;

 TIM8->PSC=500;

 TIM8->CCR2=x;

Maybe i need to re-write some parameters?

Because, when i use this code in one of the int:

  GPIO_InitTypeDef gpio;

  gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

  gpio.Mode = GPIO_MODE_OUTPUT_PP;

  HAL_GPIO_Init(GPIOB, &gpio);

Need to reset the board, to enable pwm output.

> GPIO_InitTypeDef gpio;

>   gpio.Pin = GPIO_PIN_0|GPIO_PIN_1;

>   gpio.Mode = GPIO_MODE_AF_PP;

>   HAL_GPIO_Init(GPIOB, &gpio);

Fill in *all* fields of the gpio struct, not just .Pin and .Mode.

If this won't help, read out and check/post content of *all* GPIOB and TIM8 registers.

JW

You miss which AF mux value​ it should associate with the pin(s)

P​SC and ARR are N-1 comparators.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thank you, for your answer, could you say how to check which AF I need to use?

Because previously never used before this function.

S.Ma
Principal

Configure the mode and af gpio field, under interrupt temporary disabled to avoid side effect. Then change dynamically. Remember to save the primask, change bit fields, restore. So the code also works under interrupts.

Found this place, thank you.