cancel
Showing results for 
Search instead for 
Did you mean: 

To Convert pwm to gpio

CKANG.2
Associate

I am currently designing a program using ST32G070 chip.

As a question, I would like to create a program that will change what I was using as a PWM without using Cube Mx into a digital signal LOW or HIGH signal.

I'd like to know how to change it.

3 REPLIES 3

So you have a pin on which you have a PWM output from a timer, and you want to stop the PWM and set the pin to low or high?

The simplest way is to change the respective GPIO_MODER setting from AF to Out, and then by setting the respective but in GPIO_ODR (through GPIO_BSRR) you set it to low or high. Read the GPIO chapter in Reference Manual.

jW

TDK
Guru

Or if you want to use HAL:

  1. Call HAL_GPIO_Init to initialize as an output.
  2. Call HAL_GPIO_WritePin to set as low or high.

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

Hello,

I have started PWMs

 

  /* USER CODE BEGIN 2 */

  HAL_TIM_PWM_Start(&PWM_TIMER, PWM_CHANNEL_U);
  HAL_TIM_PWM_Start(&PWM_TIMER, PWM_CHANNEL_V);
  HAL_TIM_PWM_Start(&PWM_TIMER, PWM_CHANNEL_W);

  HAL_TIMEx_PWMN_Start(&PWM_TIMER, PWM_CHANNEL_U);
  HAL_TIMEx_PWMN_Start(&PWM_TIMER, PWM_CHANNEL_V);
  HAL_TIMEx_PWMN_Start(&PWM_TIMER, PWM_CHANNEL_W);

  /* USER CODE END 2 */

 

 

Then configured GPIOs after while loop section which is default place where CubeMX generates.

static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */


  /*Configure GPIO pin Output Level */

  /*Configure GPIO pins : PWM_CHANNEL_U|PWM_CHANNEL_V|PWM_CHANNEL_W | GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15 */
  GPIO_InitStruct.Pin = PWM_CHANNEL_U|PWM_CHANNEL_V|PWM_CHANNEL_W;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 
Then, i have stopped PWM's one channel and used that channel as a gpio pin.

/* USER CODE BEGIN 4 */

            __HAL_TIM_SET_COMPARE(&PWM_TIMER, PWM_CHANNEL_U, 26);  
            __HAL_TIM_SET_COMPARE(&PWM_TIMER, PWM_CHANNEL_V, 24);  

            HAL_TIM_PWM_Stop(&htim1, PWM_CHANNEL_W);
            HAL_TIMEx_PWMN_Stop(&htim1, PWM_CHANNEL_W);

            HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
            HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, GPIO_PIN_RESET);

/* USER CODE END 4 */

 

But, my code didn't work properly. Any idea about what i am missing?

 

Thanks a lot....