Skip to main content
GunkutA
Associate III
October 13, 2020
Question

Changing GPIO pin from PWM to Digital Output at run time

  • October 13, 2020
  • 5 replies
  • 2309 views

Hello, in STM32F10x, I need to use Stop mode. However, before going to stop mode, I need to stop PWM channels and leave them high (in output). How can I stop change some pins (A0 and A6) from timer PWM mode to normal output mode? I tried this method but it did not work:

HAL_TIM_PWM_Stop(&htim2,TIM_CHANNEL_1);
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);	
HAL_GPIO_DeInit(GPIOA,GPIO_PIN_0);
HAL_GPIO_DeInit(GPIOA,GPIO_PIN_6);
HAL_GPIO_Init(GPIOA,GPIO_PIN_0);
HAL_GPIO_Init(GPIOA,GPIO_PIN_6);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_0,1);
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,1);

 This way also did not work:

	//Make PWM channels outputs:
	//Reset A port
	GPIOA->CRL |= (1<<9);
	//output mode General purpose output push/pull (PA0)
	GPIOA->CRL &= ~(0b0000<<0);
	GPIOA->CRL |=(0b0010<<0);
	//output mode General purpose output push/pull (PA6)
	GPIOA->CRL &= ~(0b0000<<24);
	GPIOA->CRL |=(0b0010<<24);
	//Set PA0 high
	GPIOA->ODR |= GPIO_ODR_ODR0;
	//Set PA6 high
	GPIOA->ODR |= GPIO_ODR_ODR6;

This topic has been closed for replies.

5 replies

TDK
October 13, 2020

> HAL_GPIO_DeInit(GPIOA,GPIO_PIN_0);

> HAL_GPIO_DeInit(GPIOA,GPIO_PIN_6);

> HAL_GPIO_Init(GPIOA,GPIO_PIN_0);

> HAL_GPIO_Init(GPIOA,GPIO_PIN_6);

This should have given you error during compiling.

See the following example for how to initialize GPIO pins:

https://github.com/STMicroelectronics/STM32CubeF1/blob/441b2cbdc25aa50437a59c4bffe22b88e78942c9/Projects/STM3210C_EVAL/Examples/GPIO/GPIO_IOToggle/Src/main.c

"If you feel a post has answered your question, please click ""Accept as Solution""."
GunkutA
GunkutAAuthor
Associate III
October 13, 2020

Thank you. But why the second method does not work?

TDK
October 13, 2020
Not going to dig into it. Inspecting the registers should provide your answer. My guess is you're not properly clearing bits. These do nothing: *> GPIOA->CRL &= ~(0b0000<<0);* *> GPIOA->CRL &= ~(0b0000<<24);*
"If you feel a post has answered your question, please click ""Accept as Solution""."
GunkutA
GunkutAAuthor
Associate III
October 13, 2020

Wouldn't this clear the bits?

GPIOA->CRL &= ~(0b0000<<0);

Then how can I clear them?

GunkutA
GunkutAAuthor
Associate III
October 13, 2020

ah yes GPIOA->CRL &= ~(0b0011<<0);

Would work I believe